C#實現掃雷遊戲
本文實例為大傢分享瞭C#實現掃雷遊戲的具體代碼,供大傢參考,具體內容如下
一、實驗目的:
1、掌握c#窗體和控件的常用屬性和功能
2、完成掃雷遊戲的基本功能
二、實驗要求:
1、遊戲基本功能必須實現。鼠標左鍵點非雷點,否則遊戲結束;鼠標右鍵一次標記雷點,郵件兩次標記上問號,右鍵三次取消標記。
2、可以對遊戲選擇難度,分為初級、中級和高級,按笑臉按鈕重新開始遊戲
3、符合遊戲邏輯。每個點周圍的雷的個數必須正確
4、點開雷點,顯示遊戲結束,並且顯示各個點的情況
5、點開所有非雷點或者標記完所有雷點時,能夠顯示遊戲勝利
6、不接受鍵盤操作,隻接受鼠標操作
三、實驗內容:
1、構建菜單欄,添加開始欄、幫助欄,開始欄用於遊戲難度的選擇,幫助欄用於遊戲規則的介紹
2、創建雷區,使用buttonarray模擬雷區,start按鈕用於重新開始遊戲
3、鼠標左鍵時,分三種情況:
(1)鼠標點擊雷點時,直接顯示遊戲結束
(2)鼠標點擊空白點時,周圍沒雷,則顯示周圍點的情況,周圍有雷,隻顯示此點的雷數
(3)鼠標左鍵點瞭一個大於0的數字,顯示周圍雷點的情況,若周圍雷點標錯,直接顯示遊戲結束
4、鼠標右鍵時,第一次標記為雷點,第二次標記為疑問,第三次取消標記。
5、顯示周圍點的情況時,因為周圍點的雷點數也可能為0,還需要顯示此點周圍的情況,需用遞歸,完成此項功能。
6、點擊笑臉按鈕時,如果不是第一次開始,就刪除原有按鈕,否則直接初始化長度、寬度和雷數,重新構建button按鈕
7、點擊菜單欄的難度選擇按鈕時,如果不是第一次開始,就刪除原有按鈕,否則直接初始化長度、寬度和雷數,重新構建button按鈕
四、實驗源代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); Size s = new Size(250,300); this.MaximumSize = this.MinimumSize = s; this.Size = s; } Button start = new Button(); Button[,] buttonarray; int[,] MileState; int Miles = 10; int widths = 9, heights = 9; int remain;//剩餘雷數 int notMiles;//剩餘非雷數 int isfirst = 1;//是否是第一次 int[,] sign;//表示各點是否輸出 private void Form1_Load(object sender, EventArgs e) { MenuStrip ms = new MenuStrip(); ToolStripMenuItem tsmione = new ToolStripMenuItem("開始"); ToolStripMenuItem tsmi1 = new ToolStripMenuItem("初級"); ToolStripMenuItem tsmi2 = new ToolStripMenuItem("中級"); ToolStripMenuItem tsmi3 = new ToolStripMenuItem("高級"); ToolStripMenuItem tsmi4 = new ToolStripMenuItem("退出"); tsmione.DropDownItems.Add(tsmi1); tsmione.DropDownItems.Add(tsmi2); tsmione.DropDownItems.Add(tsmi3); tsmione.DropDownItems.Add(tsmi4); ms.Items.Add(tsmione); tsmi1.Click += new EventHandler(tsmi1_Click); tsmi2.Click += new EventHandler(tsmi2_Click); tsmi3.Click += new EventHandler(tsmi3_Click); tsmi4.Click += new EventHandler(tsmi4_Click); ToolStripMenuItem tsmitwo = new ToolStripMenuItem("幫助"); ToolStripMenuItem tsmi5 = new ToolStripMenuItem("遊戲規則"); tsmi5.Click += new EventHandler(tsmi5_Click); tsmitwo.DropDownItems.Add(tsmi5); ms.Items.Add(tsmitwo); this.Controls.Add(ms); //笑臉按鈕 start.Text = "😊"; start.Location = new Point(75, 25); start.Click += new EventHandler(start_Click); this.Controls.Add(start); } private void tsmi1_Click(object sender, EventArgs e) { Size s = new Size(250, 300); this.MaximumSize = this.MinimumSize = s; this.Size = s; if (isfirst == 1) { widths = 9; heights = 9; Miles = 10; Initialize_button(sender, e); start.Location = new Point((buttonarray[0, 0].Location.X + buttonarray[0, widths - 1].Location.X + 20 - start.Size.Width) / 2, 25); isfirst = 0; return; } delete(); widths = 9; heights = 9; Miles = 10; Initialize_button(sender, e); start.Location = new Point((buttonarray[0, 0].Location.X + buttonarray[0, widths - 1].Location.X + 20 - start.Size.Width) / 2, 25); } private void tsmi2_Click(object sender, EventArgs e) { Size s = new Size(400, 450); this.MaximumSize = this.MinimumSize = s; this.Size = s; if (isfirst == 1) { widths = 16; heights = 16; Miles = 40; Initialize_button(sender, e); start.Location = new Point((buttonarray[0, 0].Location.X + buttonarray[0, widths - 1].Location.X + 20 - start.Size.Width) / 2, 25); isfirst = 0; return; } delete(); widths = 16; heights = 16; Miles = 40; Initialize_button(sender, e); start.Location = new Point((buttonarray[0, 0].Location.X + buttonarray[0, widths - 1].Location.X + 20 - start.Size.Width) / 2, 25); } private void tsmi3_Click(object sender, EventArgs e) { Size s = new Size(650, 450); this.MaximumSize = this.MinimumSize = s; this.Size = s; if (isfirst == 1) { widths = 30; heights = 16; Miles = 99; Initialize_button(sender, e); start.Location = new Point((buttonarray[0, 0].Location.X + buttonarray[0, widths - 1].Location.X + 20 - start.Size.Width) / 2, 25); isfirst = 0; return; } delete(); widths = 30; heights = 16; Miles = 99; Initialize_button(sender, e); start.Location = new Point((buttonarray[0, 0].Location.X + buttonarray[0, widths - 1].Location.X + 20 - start.Size.Width) / 2, 25); } //刪除控件 public void delete() { int i, j; for (i = 0; i < heights; i++) for (j = 0; j < widths; j++) this.Controls.Remove(buttonarray[i, j]); } private void tsmi4_Click(object sender, EventArgs e) { Close(); } private void tsmi5_Click(object sender, EventArgs e) { string str = "鼠標左鍵點開非雷點繼續遊戲,點開雷點遊戲結束\r\n"; str += "鼠標右鍵一次標記雷點,右鍵兩次標記問號,右鍵三次取消標記"; MessageBox.Show(str); } //設計掃雷界面,佈雷 private void Initialize_button(object sender, EventArgs e) { //初始化遊戲界面 //創建掃雷按鈕,設計遊戲界面 buttonarray = new Button[heights, widths]; MileState = new int[heights, widths]; notMiles = widths * heights - Miles; remain = Miles; int i, j; for (i = 0; i < heights; i++) { for (j = 0; j < widths; j++) { buttonarray[i, j] = new System.Windows.Forms.Button(); buttonarray[i, j].Location = new System.Drawing.Point(20 + 20 * j, 60 + 20 * i); buttonarray[i, j].Size = new System.Drawing.Size(20, 20); buttonarray[i, j].UseVisualStyleBackColor = true; buttonarray[i, j].Text = ""; buttonarray[i, j].MouseDown += new MouseEventHandler(but_MouseDown); this.Controls.Add(buttonarray[i, j]); } } int count = 0; //雷區初始化,鼠標右鍵次數初始化 for (i = 0; i < heights; i++) for (j = 0; j < widths; j++) MileState[i, j] = 0; //設置雷的位置 while (count < Miles) { Random r = new Random(); i = r.Next(heights); j = r.Next(widths); if (MileState[i, j] != -1) { MileState[i, j] = -1; count++; //雷點周圍非雷的點各加1 if (i - 1 >= 0 && j - 1 >= 0 && MileState[i - 1, j - 1] != -1) MileState[i - 1, j - 1] += 1; if (i - 1 >= 0 && MileState[i - 1, j] != -1) MileState[i - 1, j] += 1; if (i - 1 >= 0 && j + 1 < widths && MileState[i - 1, j + 1] != -1) MileState[i - 1, j + 1] += 1; if (j - 1 >= 0 && MileState[i, j - 1] != -1) MileState[i, j - 1] += 1; if (j + 1 < widths && MileState[i, j + 1] != -1) MileState[i, j + 1] += 1; if (i + 1 < heights && j - 1 >= 0 && MileState[i + 1, j - 1] != -1) MileState[i + 1, j - 1] += 1; if (i + 1 < heights && MileState[i + 1, j] != -1) MileState[i + 1, j] += 1; if (i + 1 < heights && j + 1 < widths && MileState[i + 1, j + 1] != -1) MileState[i + 1, j + 1] += 1; } } } //點擊笑臉按鈕 private void start_Click(object sender, EventArgs e) { if (isfirst == 1) { Initialize_button(sender, e); start.Location = new Point((buttonarray[0, 0].Location.X + buttonarray[0, widths - 1].Location.X + 20 - start.Size.Width) / 2, 25); isfirst = 0; return; } delete(); remain = Miles; notMiles = widths * heights - Miles; start.Text = "😊"; Initialize_button(sender, e); start.Location = new Point((buttonarray[0, 0].Location.X + buttonarray[0, widths - 1].Location.X + 20 - start.Size.Width) / 2, 25); } //按下鼠標鍵時 private void but_MouseDown(object sender, MouseEventArgs e) { //獲取按鈕坐標 int x = (this.PointToClient(MousePosition).Y - 60) / 20; int y = (this.PointToClient(MousePosition).X - 20) / 20; sign = new int[heights, widths]; //遞歸時表示是否訪問 for (int i = 0; i < heights; i++) for (int j = 0; j < widths; j++) sign[i, j] = 0; //鼠標左鍵點瞭一個大於0的數字 if (e.Button == MouseButtons.Left && buttonarray[x, y].Text != "" && buttonarray[x, y].Text != "×" && buttonarray[x, y].Text != "?" && buttonarray[x, y].Text != "-1") { int num = 0; bool issigned = false; //周圍標記的地雷個數 if (x - 1 >= 0 && y - 1 >= 0) { if (buttonarray[x - 1, y - 1].Text == "×") num++; else if (buttonarray[x - 1, y - 1].Text == "-1") issigned = true; } if (x - 1 >= 0) { if (buttonarray[x - 1, y].Text == "×") num++; else if (buttonarray[x - 1, y].Text == "-1") issigned = true; } if (x - 1 >= 0 && y + 1 < widths) { if (buttonarray[x - 1, y + 1].Text == "×") num++; else if (buttonarray[x - 1, y + 1].Text == "-1") issigned = true; } if (y - 1 >= 0) { if (buttonarray[x, y - 1].Text == "×") num++; else if (buttonarray[x, y - 1].Text == "-1") issigned = true; } if (y + 1 < widths) { if (buttonarray[x, y + 1].Text == "×") num++; else if (buttonarray[x, y + 1].Text == "-1") issigned = true; } if (x + 1 < heights && y - 1 >= 0) { if (buttonarray[x + 1, y - 1].Text == "×") num++; else if (buttonarray[x + 1, y - 1].Text == "-1") issigned = true; } if (x + 1 < heights) { if (buttonarray[x + 1, y].Text == "×") num++; else if (buttonarray[x + 1, y].Text == "-1") issigned = true; } if (x + 1 < heights && y + 1 < widths) { if (buttonarray[x + 1, y + 1].Text == "×") num++; else if (buttonarray[x + 1, y + 1].Text == "-1") issigned = true; } if (buttonarray[x, y].Text == Convert.ToString(num)) { if (issigned == false) { print(x, y, sign); notMiles++; } else MessageBox.Show("哎呀,點錯瞭,重新開始吧"); } } //鼠標左鍵,周圍沒雷 if (e.Button == MouseButtons.Left && MileState[x, y] == 0) { if (sign[x, y] == 0) print(x, y, sign); if (notMiles == 0) MessageBox.Show("恭喜你,掃雷成功,回去領賞吧", "成功"); } //鼠標左鍵,周圍有雷 if (e.Button == MouseButtons.Left && MileState[x, y] > 0) { if (sign[x, y] == 0 && --notMiles == 0) MessageBox.Show("恭喜你,掃雷成功,回去領賞吧", "成功"); sign[x, y] = 1; buttonarray[x, y].FlatStyle = FlatStyle.Popup; buttonarray[x, y].Text = Convert.ToString(MileState[x, y]); } //鼠標左鍵,錯誤 if (e.Button == MouseButtons.Left && MileState[x, y] == -1) { for (x = 0; x < heights; x++) for (y = 0; y < widths; y++) { if (MileState[x, y] != 0) buttonarray[x, y].Text = Convert.ToString(MileState[x, y]); else buttonarray[x, y].FlatStyle = FlatStyle.Popup; } start.Text = "😭"; MessageBox.Show("哎呀,點錯瞭,重新開始吧!"); } //鼠標右鍵 if (e.Button == MouseButtons.Right) { //第一次鼠標右鍵 if (buttonarray[x, y].Text == "" && sign[x, y] == 0) { //用X表示雷 buttonarray[x, y].Text = "×"; sign[x, y] = 1; if (MileState[x, y] == -1) { if (--remain == 0) MessageBox.Show("恭喜你,掃雷成功,回去領賞吧", "成功"); } } //第二次鼠標右鍵 else if (buttonarray[x, y].Text == "×") { buttonarray[x, y].Text = "?"; remain++; } //第三次鼠標右鍵 else if (buttonarray[x, y].Text == "?") { buttonarray[x, y].Text = ""; sign[x, y] = 0; } } } //顯示周圍雷區的情況,遞歸 private void print(int x, int y, int[,] sign) { buttonarray[x, y].FlatStyle = FlatStyle.Popup; sign[x, y] = 1; notMiles--; if (x - 1 >= 0 && y - 1 >= 0 && sign[x - 1, y - 1] == 0) if (MileState[x - 1, y - 1] > 0) { buttonarray[x - 1, y - 1].Text = Convert.ToString(MileState[x - 1, y - 1]); buttonarray[x - 1, y - 1].FlatStyle = FlatStyle.Popup; sign[x - 1, y - 1] = 1; } else if (MileState[x - 1, y - 1] == 0) print(x - 1, y - 1, sign); if (x - 1 >= 0 && sign[x - 1, y] == 0) if (MileState[x - 1, y] > 0) { buttonarray[x - 1, y].Text = Convert.ToString(MileState[x - 1, y]); buttonarray[x - 1, y].FlatStyle = FlatStyle.Popup; sign[x - 1, y] = 1; } else if (MileState[x - 1, y] == 0) print(x - 1, y, sign); if (x - 1 >= 0 && y + 1 < widths && sign[x - 1, y + 1] == 0) if (MileState[x - 1, y + 1] > 0) { buttonarray[x - 1, y + 1].Text = Convert.ToString(MileState[x - 1, y + 1]); buttonarray[x - 1, y + 1].FlatStyle = FlatStyle.Popup; sign[x - 1, y + 1] = 1; } else if (MileState[x - 1, y + 1] == 0) print(x - 1, y + 1, sign); if (y - 1 >= 0 && sign[x, y - 1] == 0) if (MileState[x, y - 1] > 0) { buttonarray[x, y - 1].Text = Convert.ToString(MileState[x, y - 1]); buttonarray[x, y - 1].FlatStyle = FlatStyle.Popup; sign[x, y - 1] = 1; } else if (MileState[x, y - 1] == 0) print(x, y - 1, sign); if (y + 1 < widths && sign[x, y + 1] == 0) if (MileState[x, y + 1] > 0) { buttonarray[x, y + 1].Text = Convert.ToString(MileState[x, y + 1]); buttonarray[x, y + 1].FlatStyle = FlatStyle.Popup; sign[x, y + 1] = 1; } else if (MileState[x, y + 1] == 0) print(x, y + 1, sign); if (x + 1 < heights && y - 1 >= 0 && sign[x + 1, y - 1] == 0) if (MileState[x + 1, y - 1] > 0) { buttonarray[x + 1, y - 1].Text = Convert.ToString(MileState[x + 1, y - 1]); buttonarray[x + 1, y - 1].FlatStyle = FlatStyle.Popup; sign[x + 1, y - 1] = 1; } else if (MileState[x + 1, y - 1] == 0) print(x + 1, y - 1, sign); if (x + 1 < heights && sign[x + 1, y] == 0) if (MileState[x + 1, y] > 0) { buttonarray[x + 1, y].Text = Convert.ToString(MileState[x + 1, y]); buttonarray[x + 1, y].FlatStyle = FlatStyle.Popup; sign[x + 1, y] = 1; } else if (MileState[x + 1, y] == 0) print(x + 1, y, sign); if (x + 1 < heights && y + 1 < widths && sign[x + 1, y + 1] == 0) if (MileState[x + 1, y + 1] > 0) { buttonarray[x + 1, y + 1].Text = Convert.ToString(MileState[x + 1, y + 1]); buttonarray[x + 1, y + 1].FlatStyle = FlatStyle.Popup; sign[x + 1, y + 1] = 1; } else if (MileState[x + 1, y + 1] == 0) print(x + 1, y + 1, sign); } } }
五、實驗結果:
1、用”×”標記雷點,用”?”標記疑問點,空白點表示周圍無雷點或者該點還未點開,根據顏色區別二者。
2、遊戲勝利和遊戲結束顯示messagebox。
3、第一次開始點擊笑臉按鈕,重新開始點擊哭臉按鈕。
4、點擊開始菜單欄的子菜單欄選擇遊戲難度,初級、中級、高級的雷點分別為10、40、99個
(初級)
(中級)
(高級)
點擊雷點,顯示遊戲錯誤,遊戲結束
六、總結
通過本次實驗,對c#控件和窗體有瞭更深入的瞭解,懂得如何根據各個控件的特點實現掃雷對應的功能,希望在以後能更加熟練地使用各個控件。
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。