C#實(shí)現(xiàn)掃雷游戲
本文實(shí)例為大家分享了C#實(shí)現(xiàn)掃雷游戲的具體代碼,供大家參考,具體內(nèi)容如下
一、實(shí)驗(yàn)?zāi)康模?/h2>
1、掌握c#窗體和控件的常用屬性和功能
2、完成掃雷游戲的基本功能
二、實(shí)驗(yàn)要求:
1、游戲基本功能必須實(shí)現(xiàn)。鼠標(biāo)左鍵點(diǎn)非雷點(diǎn),否則游戲結(jié)束;鼠標(biāo)右鍵一次標(biāo)記雷點(diǎn),郵件兩次標(biāo)記上問(wèn)號(hào),右鍵三次取消標(biāo)記。
2、可以對(duì)游戲選擇難度,分為初級(jí)、中級(jí)和高級(jí),按笑臉按鈕重新開(kāi)始游戲
3、符合游戲邏輯。每個(gè)點(diǎn)周?chē)睦椎膫€(gè)數(shù)必須正確
4、點(diǎn)開(kāi)雷點(diǎn),顯示游戲結(jié)束,并且顯示各個(gè)點(diǎn)的情況
5、點(diǎn)開(kāi)所有非雷點(diǎn)或者標(biāo)記完所有雷點(diǎn)時(shí),能夠顯示游戲勝利
6、不接受鍵盤(pán)操作,只接受鼠標(biāo)操作
三、實(shí)驗(yàn)內(nèi)容:
1、構(gòu)建菜單欄,添加開(kāi)始欄、幫助欄,開(kāi)始欄用于游戲難度的選擇,幫助欄用于游戲規(guī)則的介紹
2、創(chuàng)建雷區(qū),使用buttonarray模擬雷區(qū),start按鈕用于重新開(kāi)始游戲
3、鼠標(biāo)左鍵時(shí),分三種情況:
(1)鼠標(biāo)點(diǎn)擊雷點(diǎn)時(shí),直接顯示游戲結(jié)束
(2)鼠標(biāo)點(diǎn)擊空白點(diǎn)時(shí),周?chē)鷽](méi)雷,則顯示周?chē)c(diǎn)的情況,周?chē)欣?,只顯示此點(diǎn)的雷數(shù)
(3)鼠標(biāo)左鍵點(diǎn)了一個(gè)大于0的數(shù)字,顯示周?chē)c(diǎn)的情況,若周?chē)c(diǎn)標(biāo)錯(cuò),直接顯示游戲結(jié)束
4、鼠標(biāo)右鍵時(shí),第一次標(biāo)記為雷點(diǎn),第二次標(biāo)記為疑問(wèn),第三次取消標(biāo)記。
5、顯示周?chē)c(diǎn)的情況時(shí),因?yàn)橹車(chē)c(diǎn)的雷點(diǎn)數(shù)也可能為0,還需要顯示此點(diǎn)周?chē)那闆r,需用遞歸,完成此項(xiàng)功能。
6、點(diǎn)擊笑臉按鈕時(shí),如果不是第一次開(kāi)始,就刪除原有按鈕,否則直接初始化長(zhǎng)度、寬度和雷數(shù),重新構(gòu)建button按鈕
7、點(diǎn)擊菜單欄的難度選擇按鈕時(shí),如果不是第一次開(kāi)始,就刪除原有按鈕,否則直接初始化長(zhǎng)度、寬度和雷數(shù),重新構(gòu)建button按鈕
四、實(shí)驗(yàn)源代碼:
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;//剩余雷數(shù) ? ? ? ? int notMiles;//剩余非雷數(shù) ? ? ? ? int isfirst = 1;//是否是第一次 ? ? ? ? int[,] sign;//表示各點(diǎn)是否輸出 ? ? ? ? private void Form1_Load(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ?? ? ? ? ? ? ? MenuStrip ms = new MenuStrip(); ? ? ? ? ? ? ToolStripMenuItem tsmione = new ToolStripMenuItem("開(kāi)始"); ? ? ? ? ? ? ToolStripMenuItem tsmi1 = new ToolStripMenuItem("初級(jí)"); ? ? ? ? ? ? ToolStripMenuItem tsmi2 = new ToolStripMenuItem("中級(jí)"); ? ? ? ? ? ? ToolStripMenuItem tsmi3 = new ToolStripMenuItem("高級(jí)"); ? ? ? ? ? ? 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("游戲規(guī)則"); ? ? ? ? ? ? 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 = "鼠標(biāo)左鍵點(diǎn)開(kāi)非雷點(diǎn)繼續(xù)游戲,點(diǎn)開(kāi)雷點(diǎn)游戲結(jié)束\r\n"; ? ? ? ? ? ? str += "鼠標(biāo)右鍵一次標(biāo)記雷點(diǎn),右鍵兩次標(biāo)記問(wèn)號(hào),右鍵三次取消標(biāo)記"; ? ? ? ? ? ? MessageBox.Show(str); ? ? ? ? } ? ? ? ? //設(shè)計(jì)掃雷界面,布雷 ? ? ? ? private void Initialize_button(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? //初始化游戲界面 ? ? ? ? ? ? //創(chuàng)建掃雷按鈕,設(shè)計(jì)游戲界面 ? ? ? ? ? ? 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; ? ? ? ? ? ? //雷區(qū)初始化,鼠標(biāo)右鍵次數(shù)初始化 ? ? ? ? ? ? for (i = 0; i < heights; i++) ? ? ? ? ? ? ? ? for (j = 0; j < widths; j++) ? ? ? ? ? ? ? ? ? ? MileState[i, j] = 0; ? ? ? ? ? ? //設(shè)置雷的位置 ? ? ? ? ? ? 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++; ? ? ? ? ? ? ? ? ? ? //雷點(diǎn)周?chē)抢椎狞c(diǎn)各加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; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? //點(diǎn)擊笑臉按鈕 ? ? ? ? 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); ? ? ? ? } ? ? ? ? //按下鼠標(biāo)鍵時(shí) ? ? ? ? private void but_MouseDown(object sender, MouseEventArgs e) ? ? ? ? { ? ? ? ? ? ? //獲取按鈕坐標(biāo) ? ? ? ? ? ? int x = (this.PointToClient(MousePosition).Y - 60) / 20; ? ? ? ? ? ? int y = (this.PointToClient(MousePosition).X - 20) / 20; ? ? ? ? ? ? sign = new int[heights, widths]; ? ? ? ? ? ? //遞歸時(shí)表示是否訪問(wèn) ? ? ? ? ? ? for (int i = 0; i < heights; i++) ? ? ? ? ? ? ? ? for (int j = 0; j < widths; j++) ? ? ? ? ? ? ? ? ? ? sign[i, j] = 0; ? ? ? ? ? ? //鼠標(biāo)左鍵點(diǎn)了一個(gè)大于0的數(shù)字 ? ? ? ? ? ? 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; ? ? ? ? ? ? ? ? //周?chē)鷺?biāo)記的地雷個(gè)數(shù) ? ? ? ? ? ? ? ? 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("哎呀,點(diǎn)錯(cuò)了,重新開(kāi)始吧"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? //鼠標(biāo)左鍵,周?chē)鷽](méi)雷 ? ? ? ? ? ? if (e.Button == MouseButtons.Left && MileState[x, y] == 0) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if (sign[x, y] == 0) print(x, y, sign); ? ? ? ? ? ? ? ? if (notMiles == 0) ? ? ? ? ? ? ? ? ? ? MessageBox.Show("恭喜你,掃雷成功,回去領(lǐng)賞吧", "成功"); ? ? ? ? ? ? } ? ? ? ? ? ? //鼠標(biāo)左鍵,周?chē)欣? ? ? ? ? ? ? if (e.Button == MouseButtons.Left && MileState[x, y] > 0) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if (sign[x, y] == 0 && --notMiles == 0) ? ? ? ? ? ? ? ? ? ? MessageBox.Show("恭喜你,掃雷成功,回去領(lǐng)賞吧", "成功"); ? ? ? ? ? ? ? ? sign[x, y] = 1; ? ? ? ? ? ? ? ? buttonarray[x, y].FlatStyle = FlatStyle.Popup; ? ? ? ? ? ? ? ? buttonarray[x, y].Text = Convert.ToString(MileState[x, y]); ? ? ? ? ? ? } ? ? ? ? ? ? //鼠標(biāo)左鍵,錯(cuò)誤 ? ? ? ? ? ? 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("哎呀,點(diǎn)錯(cuò)了,重新開(kāi)始吧!"); ? ? ? ? ? ? } ? ? ? ? ? ? //鼠標(biāo)右鍵 ? ? ? ? ? ? if (e.Button == MouseButtons.Right) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? //第一次鼠標(biāo)右鍵 ? ? ? ? ? ? ? ? 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("恭喜你,掃雷成功,回去領(lǐng)賞吧", "成功"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? //第二次鼠標(biāo)右鍵 ? ? ? ? ? ? ? ? else if (buttonarray[x, y].Text == "×") ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? buttonarray[x, y].Text = "?"; ? ? ? ? ? ? ? ? ? ? remain++; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? //第三次鼠標(biāo)右鍵 ? ? ? ? ? ? ? ? else if (buttonarray[x, y].Text == "?") ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? buttonarray[x, y].Text = ""; ? ? ? ? ? ? ? ? ? ? sign[x, y] = 0; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? //顯示周?chē)讌^(qū)的情況,遞歸 ? ? ? ? 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); ? ? ? ? } ? ? } }
五、實(shí)驗(yàn)結(jié)果:
1、用”×”標(biāo)記雷點(diǎn),用”?”標(biāo)記疑問(wèn)點(diǎn),空白點(diǎn)表示周?chē)鸁o(wú)雷點(diǎn)或者該點(diǎn)還未點(diǎn)開(kāi),根據(jù)顏色區(qū)別二者。
2、游戲勝利和游戲結(jié)束顯示messagebox。
3、第一次開(kāi)始點(diǎn)擊笑臉按鈕,重新開(kāi)始點(diǎn)擊哭臉按鈕。
4、點(diǎn)擊開(kāi)始菜單欄的子菜單欄選擇游戲難度,初級(jí)、中級(jí)、高級(jí)的雷點(diǎn)分別為10、40、99個(gè)
(初級(jí))
(中級(jí))
(高級(jí))
點(diǎn)擊雷點(diǎn),顯示游戲錯(cuò)誤,游戲結(jié)束
六、總結(jié)
通過(guò)本次實(shí)驗(yàn),對(duì)c#控件和窗體有了更深入的了解,懂得如何根據(jù)各個(gè)控件的特點(diǎn)實(shí)現(xiàn)掃雷對(duì)應(yīng)的功能,希望在以后能更加熟練地使用各個(gè)控件。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)簡(jiǎn)單獲取及設(shè)置Session類(lèi)
這篇文章主要介紹了C#實(shí)現(xiàn)簡(jiǎn)單獲取及設(shè)置Session類(lèi),涉及C#針對(duì)session的設(shè)置及獲取的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03詳解三種C#實(shí)現(xiàn)數(shù)組反轉(zhuǎn)方式
本篇文章主要介紹了詳解三種C#實(shí)現(xiàn)數(shù)組反轉(zhuǎn)方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04c# 實(shí)現(xiàn)子窗口關(guān)閉父窗口也關(guān)閉的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇c# 實(shí)現(xiàn)子窗口關(guān)閉父窗口也關(guān)閉的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02c# 如何使用結(jié)構(gòu)體實(shí)現(xiàn)共用體
這篇文章主要介紹了c# 如何使用結(jié)構(gòu)體實(shí)現(xiàn)共用體,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-04-04