基于C#的winform實(shí)現(xiàn)數(shù)字華容道游戲
數(shù)字華容道游戲類似于拼圖游戲,只需將數(shù)字1~15按順序排好即可。該游戲邏輯比較簡單,易于編程實(shí)現(xiàn)。
游戲界面如圖:
編程準(zhǔn)備:
所需控件:label 用于顯示時間, 一個重新開始的button,一個panel容器來存放數(shù)字塊(按鈕),再加一個timer來計(jì)時及判斷游戲是否結(jié)束。
主要代碼:
variables類:
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類:
?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個按鈕 ? ? ? ? public void AddButtons(Panel panel,Button[,] buttons) ? ? ? ? { ? ? ? ? ? ? //數(shù)組隨機(jī)打亂順序 ? ? ? ? ? ? int[] s = NewSorting(a); ? ? ? ? ? ? //每個按鈕的寬度及高度 ? ? ? ? ? ? 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ù)組分配給每個button ? ? ? ? ? ? ? ? ? ? butt.Text = s[i * buttons.GetLength(0) + j].ToString(); ? ? ? ? ? ? ? ? ? ? if (butt.Text=="16") ? ? ? ? ? ? ? ? ? ? ?{ ? ? ? ? ? ? ? ? ? ? ? ? butt.Hide(); ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? variables.buttons[i, j] = butt; ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? //手動添加點(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; ? ? ? ? } ? ? ? ? ? //判斷兩個按鈕是否相鄰 ? 即兩個按鈕的坐標(biāo)位置是否差一個寬度或者高度 ? ? ? ? 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; ? ? ? ? } ? ? ? ? ? //交換兩個按鈕 ? 交換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類中的AddButtons方法自動生成的,數(shù)字方塊總共有16個,其中15個的visible屬性為true,1個為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"; ?//初始時間 ? ? ? ? ? ? timer1.Start(); ?//啟動計(jì)時器 ? ? ? ? } ? ? ? ? ? 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(); ? ? ? ? } ? ? }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#雙向鏈表LinkedList排序?qū)崿F(xiàn)方法
這篇文章主要介紹了C#雙向鏈表LinkedList排序?qū)崿F(xiàn)方法,涉及C#雙向鏈表的定義與排序技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08C#中HttpWebRequest、WebClient、HttpClient的使用詳解
這篇文章主要介紹了C#中HttpWebRequest、WebClient、HttpClient的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12C#實(shí)現(xiàn)給DataGrid單元行添加雙擊事件的方法
這篇文章主要介紹了C#實(shí)現(xiàn)給DataGrid單元行添加雙擊事件的方法,較為詳細(xì)的分析了C#給DataGrid單元添加雙擊事件的步驟及相關(guān)實(shí)現(xiàn)代碼,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07C#在Unity游戲開發(fā)中進(jìn)行多線程編程的方法
這篇文章主要介紹了C#在Unity游戲開發(fā)中進(jìn)行多線程編程的方法,文中總結(jié)了Unity中使用多線程的幾種方式以及一款多線程插件的介紹,需要的朋友可以參考下2016-04-04分享我在工作中遇到的多線程下導(dǎo)致RCW無法釋放的問題
最近在做項(xiàng)目中遇到一個問題,在調(diào)用一個類庫中的方法時,出現(xiàn)如下異常信息:嘗試釋放正在使用的RCW,活動線程或其他線程上正在使用該 RCW,釋放正在使用的 RCW 的嘗試會導(dǎo)致?lián)p壞或數(shù)據(jù)丟失2015-12-12C#內(nèi)置隊(duì)列類Queue用法實(shí)例
這篇文章主要介紹了C#內(nèi)置隊(duì)列類Queue用法,實(shí)例分析了C#內(nèi)置隊(duì)列的添加、移除等相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04C#實(shí)現(xiàn)簡單的RSA非對稱加密算法示例
這篇文章主要介紹了C#實(shí)現(xiàn)簡單的RSA非對稱加密算法,結(jié)合實(shí)例形式分析了C#實(shí)現(xiàn)RSA加密的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-08-08C# 實(shí)現(xiàn)簡單打印的實(shí)例代碼
C# 實(shí)現(xiàn)簡單打印的實(shí)例代碼,需要的朋友可以參考一下2013-03-03