⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form2.cs

📁 C#写了一个扫雷小游戏
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace winmine
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        Graphics G;
        Random Rd = new Random();
       
        private void Form2_Load(object sender, EventArgs e)
        {           
            G = pictureBox1.CreateGraphics();
            BlockGraphics.GG = G;
            Map.RowCount = 9;
            Map.ColumnCount = 9;
            Map.MineNumber = 10;
            Map.Init();
            BlockGraphics.MemImage = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            GG = Graphics.FromImage(BlockGraphics.MemImage);
            BlockGraphics.G = GG;
            Restart();

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
             
        }
       
        Graphics GG; //Image in memery  
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
                
            GG.Clear(this.BackColor);         
            Map.DrawBlocks();            
            e.Graphics.DrawImage(BlockGraphics.MemImage, 0, 0);
            base.OnPaint(e);
        }

         
        private void button1_Click(object sender, EventArgs e)
        {            
            Restart();
        }

        private void Restart()
        {
            BlockGraphics.GG.Clear(this.BackColor);   
            timer1.Enabled = true;
            timer1.Interval=1000;
            Map.GameOver = false;
            Map.Time = 0;
            Map.UserMineCount = Map.MineNumber;
            textBox1.Text = Map.UserMineCount.ToString();
            this.Width = Map.Width + 15;
            this.Height = Map.Height + 120;
            //pictureBox1.Width = Map.Width;
            //pictureBox1.Height = Map.Height;
            List<BlockData> TBDList = new List<BlockData>();
            Map.BlockDataList = new List<BlockData>();
            int n = Map.RowCount;
            int m = Map.ColumnCount;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    BlockData bd = new BlockData(j * Map.BlockWidth, i * Map.BlockWidth, 0);
                    bd.ID = i * Map.ColumnCount + j;
                    Map.BlockDataList.Add(bd);
                    TBDList.Add(bd);
                }
            }

            // 初始化雷             
            for (int j = 0; j < Map.MineNumber; j++)
            {
                int i1 = Rd.Next(TBDList.Count);
                TBDList[i1].IsMine = true;
                //TBDList[i1].State = BlockState.UserMine;
                TBDList[i1].MineCount = -1;
                TBDList.RemoveAt(i1);
            }
            //
            for (int i = 0; i < Map.BlockDataList.Count; i++)
            {
                #region InitBlock
                int row = i / Map.ColumnCount;
                int col = i % Map.ColumnCount;
                if (row > 0)
                {
                    //Add mine to  up                     
                    Map.BlockDataList[i - Map.ColumnCount].BlockList.Add(Map.BlockDataList[i]);
                    if (col > 0)
                    {
                        //Add mine to  left-up                          
                        Map.BlockDataList[i - Map.ColumnCount - 1].BlockList.Add(Map.BlockDataList[i]);
                    }
                    if (col < Map.ColumnCount - 1)
                    {
                        //Add mine to  right-up                        
                        Map.BlockDataList[i - Map.ColumnCount + 1].BlockList.Add(Map.BlockDataList[i]);
                    }
                }
                if (row < Map.RowCount - 1)
                {
                    //Add mine to  down                     
                    Map.BlockDataList[i + Map.ColumnCount].BlockList.Add(Map.BlockDataList[i]);
                    if (col > 0)
                    {
                        //Add mine to  left-down                         
                        Map.BlockDataList[i + Map.ColumnCount - 1].BlockList.Add(Map.BlockDataList[i]);
                    }
                    if (col < Map.ColumnCount - 1)
                    {
                        //Add mine to  right-down                        
                        Map.BlockDataList[i + Map.ColumnCount + 1].BlockList.Add(Map.BlockDataList[i]);
                    }
                }
                if (col > 0)
                {
                    //Add mine to  left                        
                    Map.BlockDataList[i - 1].BlockList.Add(Map.BlockDataList[i]);
                }
                if (col < Map.ColumnCount - 1)
                {
                    //Add mine to  right-up                        
                    Map.BlockDataList[i + 1].BlockList.Add(Map.BlockDataList[i]);
                }
                #endregion
                if (Map.BlockDataList[i].IsMine)
                {

                    #region InieMine
                    if (row > 0)
                    {
                        //Add mine to  up 
                        Map.BlockDataList[i - Map.ColumnCount].MineCount++;
                        Map.BlockDataList[i - Map.ColumnCount].MineList.Add(Map.BlockDataList[i]);
                        if (col > 0)
                        {
                            //Add mine to  left-up 
                            Map.BlockDataList[i - Map.ColumnCount - 1].MineCount++;
                            Map.BlockDataList[i - Map.ColumnCount - 1].MineList.Add(Map.BlockDataList[i]);


                        }
                        if (col < Map.ColumnCount - 1)
                        {
                            //Add mine to  right-up 
                            Map.BlockDataList[i - Map.ColumnCount + 1].MineCount++;
                            Map.BlockDataList[i - Map.ColumnCount + 1].MineList.Add(Map.BlockDataList[i]);

                        }
                    }
                    if (row < Map.RowCount - 1)
                    {
                        //Add mine to  down 
                        Map.BlockDataList[i + Map.ColumnCount].MineCount++;
                        Map.BlockDataList[i + Map.ColumnCount].MineList.Add(Map.BlockDataList[i]);
                        if (col > 0)
                        {
                            //Add mine to  left-down 
                            Map.BlockDataList[i + Map.ColumnCount - 1].MineCount++;
                            Map.BlockDataList[i + Map.ColumnCount - 1].MineList.Add(Map.BlockDataList[i]);
                        }
                        if (col < Map.ColumnCount - 1)
                        {
                            //Add mine to  right-down 
                            Map.BlockDataList[i + Map.ColumnCount + 1].MineCount++;
                            Map.BlockDataList[i + Map.ColumnCount + 1].MineList.Add(Map.BlockDataList[i]);
                        }
                    }
                    if (col > 0)
                    {
                        //Add mine to  left 
                        Map.BlockDataList[i - 1].MineCount++;
                        Map.BlockDataList[i - 1].MineList.Add(Map.BlockDataList[i]);
                    }
                    if (col < Map.ColumnCount - 1)
                    {
                        //Add mine to  right
                        Map.BlockDataList[i + 1].MineCount++;
                        Map.BlockDataList[i + 1].MineList.Add(Map.BlockDataList[i]);
                    }
                    #endregion
                }
            }
            for (int i = 1; i < Map.BlockDataList.Count; i++)
            {
                if (Map.BlockDataList[i].IsMine)
                {
                    Map.BlockDataList[i].MineCount = -1;
                }
            }
            Map.DrawBlocks();
        }
        int CurrentX=0;
        int CurrentY=0;
        bool LeftMouseDown = false;
        bool RightMouseDown = false;
        private void WinMine_MouseDown(object sender, MouseEventArgs e)
        {
            if (Map.GameOver)
            {
                Map.Showall();
                Map.DrawBlocks();
                return;
            }
            CurrentX = e.X;
            CurrentY = e.Y;
            CurrentX = CurrentX / Map.BlockWidth * Map.BlockWidth;
            CurrentY = CurrentY / Map.BlockWidth * Map.BlockWidth;
            BlockData bd = Map.GetBlock(CurrentX, CurrentY);
            if (bd == null)
            {
                return;
            }
            //if pressed left-button
            if (e.Button == MouseButtons.Left)
            {
                //if pressed left-button only
                if (!RightMouseDown)
                {
                    if (bd.State == BlockState.UserMine)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -