在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#游戏开发 → C# 扫雷小游戏源码

C# 扫雷小游戏源码

C#游戏开发

下载此实例
  • 开发语言:C#
  • 实例大小:0.60M
  • 下载次数:37
  • 浏览次数:315
  • 发布时间:2020-01-04
  • 实例类别:C#游戏开发
  • 发 布 人:dfhfdh
  • 文件格式:.rar
  • 所需积分:3
 相关标签: 小游戏 扫雷 游戏

实例介绍

【实例简介】

【实例截图】

from clipboard


from clipboard

【核心代码】

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;
using System.Drawing.Imaging;
using System.Media;

namespace saolei
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //定义问号图片路径
        private string wenhao = System.Environment.CurrentDirectory   "\\image\\ask.bmp";
        //定义初始后开始按钮的图片
        private string chushi = System.Environment.CurrentDirectory   "\\image\\face.bmp";
        //定义炸死后开始按钮的图片
        private string siqu = System.Environment.CurrentDirectory   "\\image\\sile.jpg";
        //定义胜利的声音
        private string shengli = System.Environment.CurrentDirectory   "\\image\\烟花.wav";
        //定义爆炸声音
        private string shengyin=System.Environment.CurrentDirectory "\\image\\爆炸声.wav";
        //定义失败时,雷炸开图片的路径
        private string shibai = System.Environment.CurrentDirectory   "\\image\\shibai.bmp";
        //定义右键单击时,标记旗子图片的路径
        private string flag = System.Environment.CurrentDirectory   "\\image\\flag.bmp";
        //地雷图片位置
        private string dileiimage = System.Environment.CurrentDirectory   "\\imag\\lei.bmp";
        //计时器
        private Timer timer1 = new Timer();
        //所用时间
        private int yongshi = 0;
        //定义地雷数
        private  int leishu = 50;
        //游戏是否结束
        private  bool over = false;
        //生成雷的行数
        private int hang = 16;
        //生成雷的列数
        private  int lie = 30;
        //游戏过程中剩余的地雷数量
        private int restlie; 

        //定义属性
        public int Leishu
        {
            get
            { return leishu; }
            set
            {
                leishu= value;
            }
        
        }
        public int Hang
        {
            get
            { return hang; }
            set
            {
                hang = value;
            }
        }
        public int Lie
        {
            get
            { return lie; }
            set
            {
                lie = value;
            }
        }


        //生成个按钮数组        
        private LeiButton[,] button = new LeiButton[16, 30];


        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 设置地雷数ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            shezhi shezhi1 = new shezhi();
            shezhi1.ShowDialog();
            

        }
        /// <summary>
        /// 窗体的load事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
           
            
            Program.form = this; restlie = Leishu;
            label4.Text = "";
            groupBox1.Location = new Point(26, 26);
            groupBox1.Text = "";
            groupBox1.Size = new System.Drawing.Size(908, 488);
            groupBox1.FlatStyle = FlatStyle.Standard;
            lei.Text = "  " restlie.ToString()   "颗";
            this.Location = new Point(20, 20);
            timer1.Enabled = true;
            Leizheng();
            Bulei();
            this.StartPosition = FormStartPosition.Manual;
            timer1.Tick  = new EventHandler(timer1_Tick);
            timer1.Interval = 1000;
        }
        /// <summary>
        /// 定义timer组件的Tick事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            yongshi  ;
            if (yongshi < 60)
                  label4.Text = yongshi.ToString()   "秒";
            else
                label4.Text = (yongshi /60).ToString()   "分"   (yongshi % 60).ToString()   "秒";

        }


        /// <summary>
        /// 生成雷阵的界面,即生成LeiButton控件(继承于Button控件)
        /// </summary>
        private void Leizheng()
        {
            for (int i = 0; i < lie; i  )
            {
                for (int j = 0; j < hang; j  )
                {
                    button[j, i] = new LeiButton();
                    button[j, i].Location = new Point( 3 i * 30, 6  j * 30);
                    button[j, i].X = j;
                    button[j, i].Y = i;
                    button[j, i].Youlei = 0;
                    button[j, i].Font = new System.Drawing.Font("宋体",button[j,i].Font.Size,button[j,i].Font.Style);
                    
                   
                    groupBox1.Controls.Add(button[j, i]);
                    button[j, i].MouseUp  = new MouseEventHandler(bt_MouseUp);
                    
                    //   .Location = new Point(30 i * 30, 30 j * 30);
                    // bt.X = j; bt.Y = i; bt.Youlei = 0;
                    // button[j, i] = bt;
                    // //bt.Text = i.ToString()   ""   j.ToString();
                    //bt.MouseUp =new MouseEventHandler(bt_MouseUp);
                    // this.Controls.Add(bt);

                }
            }
        }
        /// <summary>
        /// 开始按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
                timer1.Enabled = true;
                
                Fuyuan();
               
            
        }
        /// <summary>
        /// 定义鼠标单击事件,单击按钮时触发该事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_MouseUp(object sender, MouseEventArgs e)
        {
            if (!over)
            {
                int x, y;
                //获取被点击的Button按钮
                LeiButton b = (LeiButton)sender;
                x = b.X;//x代表button数组的第一个索引
                y = b.Y;//y表示Button数组的第二个索引
                //判断按下的鼠标键是哪个
                switch (e.Button)
                {
                    //按下鼠标左键
                    case MouseButtons.Left:
                        //判断该方格是否被翻开,Tag=0表示方格未被翻开
                        if (Convert.ToInt16(button[x, y].Tag) == 0)
                        {
                            if (button[x, y].Youlei == 0)
                            {
                                button[x, y].Enabled = false;
                                button[x, y].Text = Getdilei(x, y).ToString();
                                Saolei(x, y);
                                if (Win())
                                {

                                    Showlei();
                                    timer1.Enabled = false;
                                    SoundPlayer sound = new SoundPlayer(shengli);
                                    sound.Play();
                                    MessageBox.Show("恭喜你扫雷成功!游戏结束!", "扫雷完成");
                                    over = true;
                                }

                            }
                            else
                            {
                                button[x, y].BackgroundImage = Image.FromFile(shibai);
                                SoundPlayer sound = new SoundPlayer(shengyin);
                                sound.Play();
                                timer1.Enabled = false;
                                b.Enabled = false;
                                b.BackgroundImage = Image.FromFile(shibai);
                                xianshi(); 
                                //button1.Image = Image.FromFile(siqu);
                                //button1.ImageAlign = ContentAlignment.MiddleRight;
                                MessageBox.Show("回家练练运气再来!", "游戏失败");
                              
                                over = true;

                            }
                        }
                        break;
                    case MouseButtons.Right:
                       
                        if (Convert.ToInt16(button[x, y].Tag) == 1)
                        {
                            button[x, y].Tag = 2;
                            button[x, y].BackgroundImage = Image.FromFile(wenhao);

                        }
                        else if (Convert.ToInt16(button[x, y].Tag) == 2)
                        {
                            button[x, y].Tag = 0;
                            restlie  ;
                            button[x, y].BackgroundImage = null;
                        }
                        else
                        {
                            button[x, y].Tag = 1;
                            button[x, y].BackgroundImage = Image.FromFile(flag);
                            restlie--;
                        
                        }
                        lei.Text ="  "  restlie.ToString()   "颗";
                        if (Win())
                        {
                            SoundPlayer sound = new SoundPlayer(shengli);
                            sound.Play();
                            MessageBox.Show("恭喜你!你太有才了,扫雷成功","扫雷完成");
                            timer1.Enabled = false;
                            over = true;
                        }
                        break;

                }
            }
            else
                return;


        }
        /// <summary>
        ///  //动态布置地雷,产生随机数布雷
        /// </summary>
        private void Bulei()
        {
            Random rand = new Random();
            for (int i = 0; i < leishu; i  )
            {

                int position_x = rand.Next(hang);
                int position_y = rand.Next(lie);
                if (button[position_x, position_y].Youlei == 0)
                {
                    button[position_x, position_y].Youlei = 1;

                }
                else
                    i = i - 1;
            }

        }
        /// <summary>
        /// /判断点开的这个按钮周围8个中有几个地雷
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        private int Getdilei(int row, int col)//x代表行,y代表列
        {
            int i, j;
            int around = 0;
            int minRow = (row == 0) ? 0 : row - 1;
            int maxRow = row   2;
            int minCol = (col == 0) ? 0 : col - 1;
            int maxCol = col   2;
            for (i = minRow; i < maxRow; i  )
            {
                for (j = minCol; j < maxCol; j  )
                {
                    if (!(i>= 0 && i < hang && j >= 0 && j < lie))//判断是否在扫雷区域
                        continue;
                    if (button[i, j].Youlei == 1) around  ;
                }
            }
            return around;
        }
      
        /// <summary>
        ///以下递归扫雷,向周围八个发散的递归查找
        /// </summary>
        /// <param name="row"></param>
        /// <param name="col"></param>
        private void Saolei(int row, int col)

        {

            int minrow1 = (row == 0) ? 0 : row - 1;
            int mincol1 = (col == 0) ? 0 : col - 1;
            int maxrow1 = row   2;
            int maxcol1 = col   2;
            int leishuliang = Getdilei(row, col);
            if (leishuliang == 0)
            {
                button[row, col].Enabled = false;
                for (int m = minrow1; m < maxrow1; m  )
                {
                    for (int n = mincol1; n < maxcol1; n  )
                    {
                        if (!(m >= 0 && m < hang && n >= 0 && n < lie))
                            continue;
                        if (!(m == row && n == col) && button[m, n].Enabled == true&&Convert.ToInt16(button[m, n].Tag) == 0)
                            Saolei(m, n);
                        //判断该处是否标记为有雷,有雷该处不作任何变化,无雷控件Enable属性变为false
                        if (Convert.ToInt16(button[m, n].Tag) == 0)
                          button[m, n].Enabled = false;
                        button[m, n].Text = Getdilei(m, n).ToString();
                        if (button[m, n].Text == "0")
                            button[m, n].Text = string.Empty;
                    }

                }
            }

        }
        /// <summary>
        /// 判断是否扫完地雷
        /// </summary>
        /// <returns></returns>
        private bool Win()
        {   int zongshu=0;
            for (int i = 0; i < hang; i  )
            {
                for (int j = 0; j < lie; j  )
                {
                    if (button[i, j].Youlei == 1 && Convert.ToInt16(button[i, j].Tag) == 1)
                        zongshu  ;

                }

            }
            if (zongshu == leishu&&restlie==0)
                return true;
            else
                return false;             

        }

        private void Showlei()
        {
            for (int i = 0; i < hang; i  )
            {
                for (int j = 0; j < lie; j  )
                    if (button[i, j].Youlei == 1)
                    {
                        
                            button[i, j].BackgroundImage = Image.FromFile(dileiimage);
                     
                    }

            }

        }

        private void xianshi()
        {
            int l = 0;
            for (int i = 0; i < hang; i  )
            {
                for (int j = 0; j < lie; j  )
                {
                    if (button[i, j].Youlei == 1)
                    {
                        button[i, j].BackgroundImage = Image.FromFile(shibai);
                        l  ; 
                        //MessageBox.Show(button[i,j].Youlei.ToString());
                    }
                }
            }
           
        }

        /// <summary>
        /// 每次按开始按钮时,所有方块复原
        /// </summary>
        public void Fuyuan()
        {
            for (int i = 0; i < hang; i  )
            {
                for (int j = 0; j < lie; j  )
                {
                    button[i, j].Tag = 0;
                    button[i, j].Enabled = true;
                    button[i, j].Text = string.Empty;
                    button[i, j].BackgroundImage = null;
                    if (button[i, j].Youlei == 1)
                        button[i, j].Youlei = 0;
                }
            
            }
            Bulei();
            yongshi = 0;
            over = false;
            restlie = leishu;
            lei.Text = "  " restlie.ToString()   "颗";
            
            
           
        }

        private void 关于此游戏ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("此游戏由XXX制作!","关于此游戏");
        }

        private void 游戏规则ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string messagestring = "扫雷是一种具有迷惑性的对记忆和推理能力的简单测试,它是长久以来最受欢迎的 Windows 游戏之一。游戏目标:找出空方块并避免触雷。听起来很容易,那就试试吧!";
            messagestring  = "\n玩法\n";
            messagestring  = "1、挖开地雷,游戏即告结束。\n";
            messagestring  = "2、挖开空方块,可以继续玩。\n";
            messagestring  = "3、挖开数字,则表示在其周围的八个方块中共有多少个雷,可以使用该信息推断能够安全单击附近的哪些方块。";
            MessageBox.Show(messagestring,"查看帮助");


        }

        private void 联机帮助ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("此功能陆续开发中,请耐心等待!","联机帮助");
        }


      
    }
}

标签: 小游戏 扫雷 游戏

实例下载地址

C# 扫雷小游戏源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警