`

自编扫雷游戏

阅读更多
    有段时间没有更新自己的博客了,一是学习紧,二是忙着工作的事。好了,现在拿出在学习C#过程中所做的扫雷小程序,在代码优化方面还有很多不足之处,希望大家多多拍砖,小弟还等着回家盖房呢,呵呵...

BlockView部分:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QueryMainSample1
{
    public partial class BlockView : UserControl
    {
        public BlockView()
        {
            InitializeComponent();
            this.isMarked = false;
            this.isMine = false;
        }
        //属性
        public const int Block_Width = 13;
        public const int Block_Height = 13;
        public int row;//行
        public int col;//列
        public bool isOpen { set; get; }//是否被打开
        public bool isMine { set; get; }//是否有雷
        public bool isMarked { set; get; }//是否被标记
        //事件
        public event MouseEventHandler Open;
        //方法
        private void BlockView_MouseClick(object sender, MouseEventArgs e)
        {
            if (this.Open!=null)
            {
                this.Open.Invoke(this,e);
            }
        }
    }
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
扫雷form部分:

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 QueryMainSample1
{
    public partial class SaoLei : Form
    {
        //Propertity
        public const int ROWS = 5;//行
        public const int COLS = 5;//列
        public const int ALLMAINS = 2;//雷数
        public int Width1;//宽度
        public int Height1;//高度
        public bool GameOver = false;//游戏结束与否
        public bool Success = false;//成功
        public int MarkedCount  = ALLMAINS;//雷显示数
        //public int MineCount { set; get; }
        public int StartTime;//开始时间
        List<BlockView> Lblock = new List<BlockView>();
        //初始化
        public SaoLei()
        {
            InitializeComponent();
            this.initializeBlockValue();
        }
        //小块信息初始化方法
        public void initializeBlockValue()
        {
            //随机排列小块
            for (int i = 0; i < ROWS; i++)
            {
                for (int j = 0; j < COLS; j++)
                {
                    BlockView block = new BlockView();
                    block.row = i;
                    block.col = j;
                    block.label.Text = string.Empty;
                    block.Left = 10 + i * (BlockView.Block_Width + 2);
                    block.Top = 50 + j * (BlockView.Block_Height + 2);
                    block.Open += new MouseEventHandler(block_Open);
                    Lblock.Add(block);
                    this.Controls.Add(block);
                }
            }

            //初始化版面
            this.Width1 = 25 + ROWS * (BlockView.Block_Width +2);
            this.Height1 = 85 + COLS * (BlockView.Block_Height +2);
            if(this.Width1>325 || this.Height1>385){
                this.Width = this.Width1;
                this.Height = this.Height1;
            }
           
            //显示雷数目
            this.textBoxMineCount.Text = "雷数:"+MarkedCount.ToString();

            //随机排列雷
            this.Mine();
            //开始时timer1停止运行
            this.timer1.Stop();
        }

        //随机排列雷
        public void Mine()
        {
            Random r = new Random(DateTime.Now.Millisecond);
            for (int i = 0; i < ALLMAINS; i++)
            {
                int n = r.Next(0, ROWS * COLS);
                if (!Lblock.ElementAt(n).isMine)
                {
                    Lblock.ElementAt(n).isMine = true;
                }
                else
                {
                    i--;
                }
            }
        }

       //小块的Open事件
        void block_Open(object sender, MouseEventArgs e)
        {
            if (this.Success || this.GameOver)
            {
                timer1.Stop();
                return;
            }
            timer1.Start();
            this.buttonReset.Text = "Reset";
            BlockView block = sender as BlockView;
            if (block.isOpen)
            {
                return;
            }
            if (e.Button == MouseButtons.Left)
            {
                if (this.Success || this.GameOver)
                {
                    timer1.Stop();
                    this.buttonReset.Text = "Reset";
                    return;
                }
                //如果已被用户标记,则不可再点击
                if (block.isMarked)
                {
                    return;
                }
                if (block.isMine)
                {
                    this.GameOver = true;
                    Lblock.Where(s => s.isMine).ToList().ForEach(s => s.BackColor = Color.Red);
                    timer1.Stop();
                    this.buttonReset.Text = "Failed";
                    MessageBox.Show("游戏失败!");
                    this.buttonReset.Text = "Reset";
                    return;
                }
                CheckMine(block);
                if (Lblock.Count(s => s.isMarked && s.isMine) == ALLMAINS)
                {
                    if (MarkedCount == 0)
                    {
                        this.textBoxMineCount.Text = "雷数:"+this.MarkedCount.ToString();
                        this.Success = true;
                        timer1.Stop();
                        this.buttonReset.Text = "Ok";
                        MessageBox.Show("恭喜您过关,你共" + this.textBoxTime.Text + "!");
                        this.buttonReset.Text = "Reset";
                    }
                }
            }
            else if (e.Button == MouseButtons.Right)
            {
                if (block.isMarked)
                {
                    this.MarkedCount++;
                    this.textBoxMineCount.Text = "雷数:" + this.MarkedCount.ToString();
                    block.isMarked = false;
                    block.BackColor = Color.DarkGray;
                    return;
                }
                else
                {
                    block.isMarked = true;
                    this.MarkedCount--;
                    block.BackColor = Color.Black;
                    this.textBoxMineCount.Text = "雷数:"+this.MarkedCount.ToString();
                    if (this.MarkedCount == 0)
                    {
                        if (Lblock.Count(s => s.isMarked && s.isMine) == ALLMAINS)
                        {
                            this.Success = true;
                            timer1.Stop();
                            this.buttonReset.Text = "Ok";
                            MessageBox.Show("恭喜您过关,你共" + this.textBoxTime.Text +"!");
                            this.buttonReset.Text = "Reset";
                        }
                    }
                }
            }
        }
        //检查小块周围的块(雷)
        public void CheckMine(BlockView block)
        {
            List<BlockView> Lblock2 = new List<BlockView>();
            block.BackColor = Color.White;
            block.isOpen = true;
            foreach (BlockView s in Lblock)
            {
                if (((Math.Abs(s.row - block.row) == 1 && Math.Abs(s.col - block.col) == 1) || (Math.Abs(s.row - block.row) == 0 && Math.Abs(s.col - block.col) == 1) || (Math.Abs(s.row- block.row) == 1 && Math.Abs(s.col - block.col) == 0)) && s.BackColor != Color.White)
                {
                    if (!s.isMarked)
                    {
                        Lblock2.Add(s);
                    }
                }
            }
            if (Lblock2.Any(s => s.isMine))
            {
                string mineNumber = Lblock2.Count(s => s.isMine).ToString();
                block.BackColor = Color.LightCyan;
                block.label.Text = mineNumber;
                return;
            }
            foreach (BlockView ss in Lblock2)
            {
                CheckMine(ss);
            }
        }
        //ButtonResult的点击事件
        private void buttonReset_Click(object sender, EventArgs e)
        {
            if (this.buttonReset.Text == "Reset")
            {
                Lblock.ForEach(s =>
                {
                    s.isOpen = false;
                    s.isMine = false;
                    s.isMarked = false;
                    s.label.Text = string.Empty;
                    s.BackColor = Color.DarkGray;
                });
                this.Mine();
                this.buttonReset.Text = "Welcome";
                this.StartTime = 0;
                this.textBoxTime.Text = "用时:0秒";
                timer1.Stop();
                this.textBoxMineCount.Text = "雷数:"+ALLMAINS.ToString();
                this.MarkedCount = ALLMAINS;
               
                if (this.Success)
                {
                    this.Success = false;
                }
                if (this.GameOver)
                {
                    this.GameOver = false;
                }
            }
        } 
       
        //timer的tick事件,在textbox中显示时间
        private void timer1_Tick(object sender, EventArgs e)
        {
            this.StartTime++;
            this.textBoxTime.Text = "用时:" + StartTime.ToString() + "秒";
        }
       
    }
}
分享到:
评论

相关推荐

    自编立体扫雷游戏源代码

    自己用JAVA语言在手机IDE上编写的立体扫雷游戏,游戏有平面模式和立体模式两种玩法。压缩包内含有源代码文件和生成的apk文件。源代码可作为游戏编程学习资料,也可内置广告平台SDK获取广告收益。本人非专业,自学...

    自编扫雷代码MFC.docx

    以MFC编写扫雷,其中包含布局、计算每颗按钮周围雷数、加载位图,播放音乐等相关代码,为word文档,格式,便于大家参考。

    自编简单扫雷程序源码

    多年前自己编写的简单扫雷程序源码,供有兴趣学习者参考。 语言:VC++ MFC 环境:vs2010

    C#编的扫雷游戏源码

    自己用C#编的一个扫雷游戏,功能跟微软的基本相同,调用递归来完成自动排雷,第一次做游戏,大家可以交流交流

    MFC扫雷游戏源码 自己做的 适合初学者

    自己在游戏设计课上动手编的一个扫雷游戏,有音效,而且音效是仿照win7扫雷的,适合初学者学习参考。

    浙农林大一新牲自编扫雷

    大一牲不懂事,编着玩的,有大佬看到也请指正

    自己写的扫雷

    自己编的一个扫雷游戏,可以自选择雷数

    c++ 编的扫雷小游戏 vs2022测试通过

    另附自编的CE一键扫雷脚本文件。

    用c++编的扫雷小游戏

    用c++编写的扫雷小游戏哦,全部源代码都在这里,换几张图片和文字就成了自己的程序了哦!

    易语言Ex_DirectUI自绘扫雷游戏

    今天小编就为大家分享一篇关于易语言Ex_DirectUI自绘扫雷游戏源码,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

    扫雷qt源代码.zip

    自编小游戏扫雷qt源代码,qt5。

    扫雷

    我编的第一个游戏,跟windows自带的扫雷游戏很相似

    JAVA写的扫雷小游戏

    这是我自己编的扫雷小游戏(带源代码); 我自学JAVA2个月;到这种程度不知行不行??? 希望大家多多指教; 还有我这是在JDK6上编译的; 在JDK4前不知能不能运行?? 谢谢大家... 希望大家多提意见

    扫雷的VC小程序,和windows自带的游戏一样

    这是我以前用VC6编的扫雷小游戏的源代码,效果不错,有有游戏后门,按f2,可以直接通关,呵呵,只是自己娱乐一下。

    扫雷 (含源码)

    今年暑假,我脑子一热,决定要用Delphi 编出自己的扫雷出来,于是就着手开始构思,想在扫雷里加入一些新增的功能,前思后想,还是觉得不如完全模仿微软操作系统自带的扫雷游戏,给自己定个目标,于是就一切以 ...

    简易扫雷游戏程序(VB)

    这是我自己做的小程序,今天上课前在机房里玩扫雷,被老师看见,他说以为我做怎么那么认真,原来是在玩游戏啊!惭愧!编一个啊,玩有怎么意思。这样我今天特意编了一个,不过对有些人来说太简单了,有很多缺陷,希望...

    扫雷小游戏

    自己编的小游戏。。。界面不是很好看 仅供参考

    C# 扫雷v1.0.1

    学习C#时自己编的扫雷游戏,源代码加.exe 文件 有简单的windows from 的运用, 鼠标event 的改写 简单的声音运用 动态图片的绘制。 resource handler的使用 VS 2003 C#.net framework 1.1

    C#扫雷窗体应用程序

    自己编的一个小程序——扫雷,背景是优美的风景,游戏设置三个关卡。

Global site tag (gtag.js) - Google Analytics