• 拼图游戏


    本例创建一个简单的拼图游戏,将一个打乱的图形拼合完整,本例用数字替代图形,游戏界面如图。

    20120409200851

    程序代码如下。

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;
    namespace eg50_pintu
    {
    	public partial class MainForm : Form
    	{
    		int flag;
    		private int count;
    		private int[] numbers=new int[16];
    		public MainForm()
    		{
    			InitializeComponent();
    		}
    		
    		void T1KeyDown(object sender, KeyEventArgs e)
    		{
    			if(flag==0)
    			{
    				string keyId=e.KeyCode.ToString();
    				switch(keyId)
    				{
    						case "Right":LeftPress();
    						break;
    						case "Left":RightPress();
    						break;
    						case "Down":UpPress();
    						break;
    						case "Up":DownPress();
    						break;
    				}
    			}
    		}
    		private void assign()
    		{
    			if(numbers[0]!=-1)
    				b1.Text=numbers[0].ToString();
    			else
    			{
    				b1.Text="*";
    				b1.Visible=false;
    			}
    			if(numbers[1]!=-1)
    				b2.Text=numbers[1].ToString();
    			else 
    			{
    				b2.Text="*";
    				b2.Visible=false;
    			}
    			if(numbers[2]!=-1)
    				b3.Text=numbers[2].ToString();
    			else
    			{
    				b3.Text="*";
    				b3.Visible=false;
    			}
    			if(numbers[3]!=-1)
    				b4.Text=numbers[3].ToString();
    			else
    			{
    				b4.Text="*";
    				b4.Visible=false;
    			}
    			if(numbers[4]!=-1)
    				b5.Text=numbers[4].ToString();
    			else
    			{
    				b5.Text="*";
    				b5.Visible=false;
    			}
    			if(numbers[5]!=-1)
    				b6.Text=numbers[5].ToString();
    			else
    			{
    				b6.Text="*";
    				b6.Visible=false;
    			}
    			if(numbers[6]!=-1)
    				b7.Text=numbers[6].ToString();
    			else
    			{
    				b7.Text="*";
    				b7.Visible=false;
    			}
    			if(numbers[7]!=-1)
    				b8.Text=numbers[7].ToString();
    			else
    			{
    				b8.Text="*";
    				b8.Visible=false;
    			}
    			if(numbers[8]!=-1)
    				b9.Text=numbers[8].ToString();
    			else
    			{
    				b9.Text="*";
    				b9.Visible=false;
    			}
    			if(numbers[9]!=-1)
    				b10.Text=numbers[9].ToString();
    			else
    			{
    				b10.Text="*";
    				b10.Visible=false;
    			}
    			if(numbers[10]!=-1)
    				b11.Text=numbers[10].ToString();
    			else
    			{
    				b11.Text="*";
    				b11.Visible=false;
    			}
    			if(numbers[11]!=-1)
    				b12.Text=numbers[11].ToString();
    			else
    			{
    				b12.Text="*";
    				b12.Visible=false;
    			}
    			if(numbers[12]!=-1)
    				b13.Text=numbers[12].ToString();
    			else
    			{
    				b13.Text="*";
    				b13.Visible=false;
    			}
    			if(numbers[13]!=-1)
    				b14.Text=numbers[13].ToString();
    			else
    			{
    				b14.Text="*";
    				b14.Visible=false;
    			}
    			if(numbers[14]!=-1)
    				b15.Text=numbers[14].ToString();
    			else
    			{
    				b15.Text="*";
    				b15.Visible=false;
    			}
    			if(numbers[15]!=-1)
    				b16.Text=numbers[15].ToString();
    			else
    			{
    				b16.Text="*";
    				b16.Visible=false;
    			}
    		}
    		public void RightPress()
    		{
    			int chk;
    			int loc;
    			int swap;
    			loc=locator();
    			if(((loc+1)%4)==0)
    				return;
    			swap=numbers[loc+1];
    			numbers[loc+1]=-1;
    			numbers[loc]=swap;
    			MakeVisible();
    			assign();
    			chk=CheckForCompletion();
    			lbl3.Text=count.ToString();
    			if(chk==0)
    			{
    				count++;
    				lbl3.Text=count.ToString();
    			}
    			if(chk==1)
    			{
    				flag=1;
    				lbl1.Visible=true;
    			}
    		}
    		public void LeftPress()
    		{
    			int chk;
    			int loc;
    			int swap;
    			loc=locator();
    			if((loc%4)==0)
    				return;
    			swap=numbers[loc-1];
    			numbers[loc-1]=-1;
    			numbers[loc]=swap;
    			MakeVisible();
    			assign();
    			chk=CheckForCompletion();
    			lbl3.Text=count.ToString();
    			if(chk==0)
    			{
    				count++;
    				lbl3.Text=count.ToString();
    			}
    			if(chk==1)
    			{
    				flag=1;
    				lbl1.Visible=true;
    			}
    		}
    		public void UpPress()
    		{
    			int chk;
    			int loc;
    			int swap;
    			loc=locator();
    			if(loc<=3)
    				return;
    			swap=numbers[loc-4];
    			numbers[loc]=swap;
    			MakeVisible();
    			assign();
    			chk=CheckForCompletion();
    			lbl3.Text=count.ToString();
    			if(chk==0)
    			{
    				count++;
    				lbl3.Text=count.ToString();
    			}
    			if(chk==1)
    			{
    				flag=1;
    				lbl1.Visible=true;
    			}
    		}
    		public void DownPress()
    		{
    			int chk;
    			int loc;
    			int swap;
    			loc=locator();
    			if(loc>1)
    				return;
    			swap=numbers[loc+4];
    			numbers[loc]=swap;
    			MakeVisible();
    			assign();
    			chk=CheckForCompletion();
    			lbl3.Text=count.ToString();
    			if(chk==0)
    			{
    				count++;
    				lbl3.Text=count.ToString();
    			}
    			if(chk==1)
    			{
    				flag=1;
    				lbl1.Visible=true;
    			}
    		}
    		private int locator()
    		{
    			int i;
    			for(i=0;i<=15;i++)
    			{
    				if(numbers[i]==-1)
    					break;
    				else continue;
    			}
    			return i;
    		}
    		private int CheckForCompletion()
    		{
    			int j;
    			for(int i=0;i<=15;i++)
    			{
    				if(numbers[i]==-1)
    					numbers[i]=16;
    			}
    			for (j=0;j<=14;j++)
    			{
    				if(numbers[j]>numbers[j+1])
    					break;
    			}
    			if(j==15)
    				return 1;
    			else 
    			{
    				for(int i=0;i<=15;i++)
    				{
    					if(numbers[i]==16)
    						numbers[i]=-1;
    				}
    				return 0;
    			}
    		}
    		private void MakeVisible()
    		{
    			b1.Visible=true;
    			b2.Visible=true;
    			b3.Visible=true;
    			b4.Visible=true;
    			b5.Visible=true;
    			b6.Visible=true;
    			b7.Visible=true;
    			b8.Visible=true;
    			b9.Visible=true;
    			b10.Visible=true;
    			b11.Visible=true;
    			b12.Visible=true;
    			b13.Visible=true;
    			b14.Visible=true;
    			b15.Visible=true;
    			b16.Visible=true;
    		}
    	}
    }
    作者:codee
    文章千古事,得失寸心知。


  • 相关阅读:
    pyspark创建RDD数据、RDD转DataFrame以及保存
    pyspark将DataFrame转成table以及操作sql语句
    pyspark读取textfile形成DataFrame以及查询表的属性信息
    sql-lab闯关之20-22
    sql-lib闯关之lesson17-19
    sql-lab闯关之13-16
    sql-lab闯关之11-12
    sql-lib闯关之lesson8-10
    sql-lab闯关之7
    sql-lib闯关之5-6
  • 原文地址:https://www.cnblogs.com/bimgoo/p/2437930.html
Copyright © 2020-2023  润新知