溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2022-06-08 09:15:43 來(lái)源:億速云 閱讀:127 作者:zzz 欄目:開發(fā)技術(shù)

今天小編給大家分享一下C#如何實(shí)現(xiàn)掃雷游戲的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來(lái)了解一下吧。

一、實(shí)驗(yàn)?zāi)康模?/h3>

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í),按笑臉按鈕重新開始游戲
3、符合游戲邏輯。每個(gè)點(diǎn)周圍的雷的個(gè)數(shù)必須正確
4、點(diǎn)開雷點(diǎn),顯示游戲結(jié)束,并且顯示各個(gè)點(diǎn)的情況
5、點(diǎn)開所有非雷點(diǎn)或者標(biāo)記完所有雷點(diǎn)時(shí),能夠顯示游戲勝利
6、不接受鍵盤操作,只接受鼠標(biāo)操作

三、實(shí)驗(yàn)內(nèi)容:

1、構(gòu)建菜單欄,添加開始欄、幫助欄,開始欄用于游戲難度的選擇,幫助欄用于游戲規(guī)則的介紹
2、創(chuàng)建雷區(qū),使用buttonarray模擬雷區(qū),start按鈕用于重新開始游戲
3、鼠標(biāo)左鍵時(shí),分三種情況:
    (1)鼠標(biāo)點(diǎn)擊雷點(diǎn)時(shí),直接顯示游戲結(jié)束
    (2)鼠標(biāo)點(diǎn)擊空白點(diǎn)時(shí),周圍沒雷,則顯示周圍點(diǎn)的情況,周圍有雷,只顯示此點(diǎn)的雷數(shù)
    (3)鼠標(biāo)左鍵點(diǎn)了一個(gè)大于0的數(shù)字,顯示周圍雷點(diǎn)的情況,若周圍雷點(diǎn)標(biāo)錯(cuò),直接顯示游戲結(jié)束
4、鼠標(biāo)右鍵時(shí),第一次標(biāo)記為雷點(diǎn),第二次標(biāo)記為疑問(wèn),第三次取消標(biāo)記。
5、顯示周圍點(diǎn)的情況時(shí),因?yàn)橹車c(diǎn)的雷點(diǎn)數(shù)也可能為0,還需要顯示此點(diǎn)周圍的情況,需用遞歸,完成此項(xiàng)功能。
6、點(diǎn)擊笑臉按鈕時(shí),如果不是第一次開始,就刪除原有按鈕,否則直接初始化長(zhǎng)度、寬度和雷數(shù),重新構(gòu)建button按鈕
7、點(diǎn)擊菜單欄的難度選擇按鈕時(shí),如果不是第一次開始,就刪除原有按鈕,否則直接初始化長(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("開始");
            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)開非雷點(diǎn)繼續(xù)游戲,點(diǎn)開雷點(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)周圍非雷的點(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;
                //周圍標(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ò)了,重新開始吧");
                }
            }
            //鼠標(biāo)左鍵,周圍沒雷
            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)左鍵,周圍有雷
            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ò)了,重新開始吧!");
            }
            //鼠標(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;
                }
            }
        }

        //顯示周圍雷區(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、用”&times;”標(biāo)記雷點(diǎn),用”?”標(biāo)記疑問(wèn)點(diǎn),空白點(diǎn)表示周圍無(wú)雷點(diǎn)或者該點(diǎn)還未點(diǎn)開,根據(jù)顏色區(qū)別二者。

2、游戲勝利和游戲結(jié)束顯示messagebox。

3、第一次開始點(diǎn)擊笑臉按鈕,重新開始點(diǎn)擊哭臉按鈕。

4、點(diǎn)擊開始菜單欄的子菜單欄選擇游戲難度,初級(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è)控件。

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

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

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

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

以上就是“C#如何實(shí)現(xiàn)掃雷游戲”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI