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

C#實(shí)現(xiàn)掃雷游戲

 更新時(shí)間:2022年06月07日 16:43:55   作者:tazi11  
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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ù)的方法

    C#獲取字符串后幾位數(shù)的方法

    這篇文章主要介紹了C#獲取字符串后幾位數(shù)的方法,實(shí)例分析了C#操作字符串的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-03-03
  • C#實(shí)現(xiàn)簡(jiǎn)單獲取及設(shè)置Session類(lèi)

    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)方式

    本篇文章主要介紹了詳解三種C#實(shí)現(xiàn)數(shù)組反轉(zhuǎn)方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-04-04
  • Unity快速生成常用文件夾的方法

    Unity快速生成常用文件夾的方法

    這篇文章主要介紹了Unity快速生成常用文件夾的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • c# 實(shí)現(xiàn)子窗口關(guān)閉父窗口也關(guān)閉的簡(jiǎn)單實(shí)例

    c# 實(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-02
  • C#中的委托delegate用法的示例詳解

    C#中的委托delegate用法的示例詳解

    這篇文章主要介紹了C#中的委托用法的示例詳解。本章將由淺入深地講述什么是委托、為什么要使用委托、事件的由來(lái)、.NET Framework 中的委托和事件、委托中方法異常和超時(shí)的處理、委托與異步編程、委托和事件對(duì)Observer 設(shè)計(jì)模式的意義。感興趣的可以了解一下
    2020-07-07
  • 如何實(shí)現(xiàn)定時(shí)推送的具體方案

    如何實(shí)現(xiàn)定時(shí)推送的具體方案

    在工作當(dāng)中遇到了一個(gè)需要定時(shí)向客戶端推送新聞、文章等內(nèi)容。小項(xiàng)目又用不了大框架,這個(gè)時(shí)候在網(wǎng)上搜了很久沒(méi)有找到合適的解決方案,直到看到了一位大佬寫(xiě)的文章提供了一個(gè)非常不錯(cuò)的思路本篇文章也是受到他的啟發(fā)實(shí)現(xiàn)了之后這里分享給大家
    2021-04-04
  • 詳解LINQ入門(mén)(下篇)

    詳解LINQ入門(mén)(下篇)

    這篇文章主要介紹了詳解LINQ入門(mén)(下篇),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • c# 如何使用結(jié)構(gòu)體實(shí)現(xiàn)共用體

    c# 如何使用結(jié)構(gòu)體實(shí)現(xiàn)共用體

    這篇文章主要介紹了c# 如何使用結(jié)構(gòu)體實(shí)現(xiàn)共用體,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-04-04
  • C# 使用 log4net 日志組件的方法

    C# 使用 log4net 日志組件的方法

    這篇文章主要介紹了C# 使用 log4net 日志組件的方法,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2020-12-12

最新評(píng)論