欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于C#的winform實(shí)現(xiàn)數(shù)字華容道游戲

 更新時(shí)間:2022年02月17日 16:01:25   作者:Iawfy_  
這篇文章主要為大家詳細(xì)介紹了基于C#的winform實(shí)現(xiàn)數(shù)字華容道游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

數(shù)字華容道游戲類(lèi)似于拼圖游戲,只需將數(shù)字1~15按順序排好即可。該游戲邏輯比較簡(jiǎn)單,易于編程實(shí)現(xiàn)。

游戲界面如圖:

編程準(zhǔn)備:

所需控件:label 用于顯示時(shí)間, 一個(gè)重新開(kāi)始的button,一個(gè)panel容器來(lái)存放數(shù)字塊(按鈕),再加一個(gè)timer來(lái)計(jì)時(shí)及判斷游戲是否結(jié)束。

主要代碼:

variables類(lèi):

class variables
? ? {
? ? ? ? public static int[] a = new int[16] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
? ? ? ? ? ? ?14, 15,16 };
? ? ? ? public static Button[,] buttons = new Button[4, 4];
? ? }

數(shù)組a用于存放數(shù)字,隨機(jī)打亂順序并分配給buttons。buttons即游戲中的方塊。

Methods類(lèi):

?class Method
? ? {
? ? ? ? //數(shù)組打亂順序
? ? ? ? public int[] NewSorting(int[]a)
? ? ? ? {
? ? ? ? ? ? Random r = new Random();
? ? ? ? ? ? for(int i=0;i<a.Length;i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? int temp = a[i];
? ? ? ? ? ? ? ? int randomindex = r.Next(0, a.Length);
? ? ? ? ? ? ? ? a[i] = a[randomindex];
? ? ? ? ? ? ? ? a[randomindex] = temp;
? ? ? ? ? ? }
? ? ? ? ? ? return a;
? ? ? ? }
?
? ? ? ? //向容器中添加16個(gè)按鈕
? ? ? ? public void AddButtons(Panel panel,Button[,] buttons)
? ? ? ? {
? ? ? ? ? ? //數(shù)組隨機(jī)打亂順序
? ? ? ? ? ? int[] s = NewSorting(a);
? ? ? ? ? ? //每個(gè)按鈕的寬度及高度
? ? ? ? ? ? int width = 32;
? ? ? ? ? ? int height = 32;
? ? ? ? ? ? int x0 = panel.Location.X;
? ? ? ? ? ? int y0 = panel.Location.Y;
? ? ? ? ? ? for(int i=0;i<buttons.GetLength(0);i++)
? ? ? ? ? ? ? ? for(int j=0;j<buttons.GetLength(1);j++)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? Button butt = new Button();
? ? ? ? ? ? ? ? ? ? //設(shè)置按鈕相關(guān)屬性
? ? ? ? ? ? ? ? ? ? butt.Size = new System.Drawing.Size(width, height);
? ? ? ? ? ? ? ? ? ? butt.Location = new System.Drawing.Point(x0 + (j + 1) * width, y0 + (i + 1) * height);
? ? ? ? ? ? ? ? ? ? butt.Visible = true;
? ? ? ? ? ? ? ? ? ? //打亂順序的數(shù)組分配給每個(gè)button
? ? ? ? ? ? ? ? ? ? butt.Text = s[i * buttons.GetLength(0) + j].ToString();
? ? ? ? ? ? ? ? ? ? if (butt.Text=="16")
? ? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? butt.Hide();
? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? variables.buttons[i, j] = butt;
? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? //手動(dòng)添加點(diǎn)擊事件
? ? ? ? ? ? ? ? ? ? butt.Click += new EventHandler(butt_Click);
? ? ? ? ? ? ? ? ? ? //按鈕添加到容器
? ? ? ? ? ? ? ? ? ? panel.Controls.Add(butt);
? ? ? ? ? ? ? ? }
? ? ? ? }
?
? ? ? ? //自定義點(diǎn)擊事件
? ? ? ? public void butt_Click(Object sender,EventArgs e)
? ? ? ? {
? ? ? ? ? ? Button butt = sender as Button;
? ? ? ? ? ? Button butt_16 = Find_Button16();
?
? ? ? ? ? ? //判斷是否相鄰,如果相鄰則交換
? ? ? ? ? ? if(Neighboor(butt,butt_16))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? swap(butt, butt_16);
? ? ? ? ? ? ? ? butt_16.Focus();
? ? ? ? ? ? }
? ? ? ? }
?
? ? ? ? //找出隱藏的按鈕 即16所在的按鈕
? ? ? ? public Button Find_Button16()
? ? ? ? {
? ? ? ? ? ? for(int i=0;i<buttons.GetLength(0);i++)
? ? ? ? ? ? ? ? for(int j=0;j<buttons.GetLength(1);j++)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? if (buttons[i, j].Visible == false)
? ? ? ? ? ? ? ? ? ? ? ? return buttons[i, j];
? ? ? ? ? ? ? ? }
? ? ? ? ? ? return null;
? ? ? ? }
?
? ? ? ? //判斷兩個(gè)按鈕是否相鄰 ? 即兩個(gè)按鈕的坐標(biāo)位置是否差一個(gè)寬度或者高度
? ? ? ? public bool Neighboor(Button butt1, Button butt2)
? ? ? ? {
? ? ? ? ? ? int x1 = butt1.Location.X;
? ? ? ? ? ? int y1 = butt1.Location.Y;
?
? ? ? ? ? ? int x2 = butt2.Location.X;
? ? ? ? ? ? int y2 = butt2.Location.Y;
?
? ? ? ? ? ? if(((x1==x2)&&(Math.Abs(y1-y2)==butt1.Height))||((y1==y2)&&(Math.Abs(x1-x2)==butt1.Width)))
? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? }
? ? ? ? ? ? return false;
? ? ? ? }
?
? ? ? ? //交換兩個(gè)按鈕 ? 交換text 與visible
? ? ? ? public void swap(Button butt1,Button butt2)
? ? ? ? {
? ? ? ? ? ? string s = butt1.Text;
? ? ? ? ? ? butt1.Text = butt2.Text;
? ? ? ? ? ? butt2.Text = s;
?
? ? ? ? ? ? bool a = butt1.Visible;
? ? ? ? ? ? butt1.Visible = butt2.Visible;
? ? ? ? ? ? butt2.Visible = a;
? ? ? ? }
?
? ? ? ? //判斷游戲是否完成
? ? ? ? public bool GameoverOrNot()
? ? ? ? {
? ? ? ? ? ? for (int i = 0; i < buttons.GetLength(1); i++)
? ? ? ? ? ? ? ? for (int j = 0; j < buttons.GetLength(0); j++)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? if (int.Parse(variables.buttons[i, j].Text) != (i * buttons.GetLength(0) + j + 1))
? ? ? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? return true;
? ? ? ? }
? ? }

游戲中的數(shù)字方塊為Methods類(lèi)中的AddButtons方法自動(dòng)生成的,數(shù)字方塊總共有16個(gè),其中15個(gè)的visible屬性為true,1個(gè)為false。

窗體界面代碼:

public partial class Form1 : Form
? ? {
? ? ? ? public Form1()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? }
? ? ? ? ? ? ? ? ? ?
? ? ? ? Method method = new Method();
? ? ? ? int count;
? ? ? ? private void Form1_Load(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? method.AddButtons(panel1, buttons);
? ? ? ? ? ? label2.Text = "0"+"S"; ?//初始時(shí)間
? ? ? ? ? ? timer1.Start(); ?//啟動(dòng)計(jì)時(shí)器
? ? ? ? }
?
? ? ? ? private void timer1_Tick(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? //默認(rèn)100毫秒刷新一次
? ? ? ? ? ? count += 1;
? ? ? ? ? ? label2.Text = (count/10).ToString()+"S";
? ? ? ? ? ? if (method.GameoverOrNot())
? ? ? ? ? ? {
? ? ? ? ? ? ? ? timer1.Stop();
? ? ? ? ? ? ? ? MessageBox.Show("挑戰(zhàn)成功!");
? ? ? ? ? ? }
? ? ? ? }
?
? ? ? ? private void ButtonR_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? timer1.Stop();
? ? ? ? ? ? for (int i = 0; i < buttons.GetLength(0); i++)
? ? ? ? ? ? ? ? for (int j = 0; j < buttons.GetLength(1); j++)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? buttons[i, j].Hide();
? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? method.AddButtons(panel1, buttons);
? ? ? ? ? ? count = 0;
? ? ? ? ? ? timer1.Start();
? ? ? ? }
? ? }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論