码迷,mamicode.com
首页 > Windows程序 > 详细

C# 实现扫雷

时间:2020-11-11 16:12:15      阅读:25      评论:0      收藏:0      [点我收藏+]

标签:init   游戏   oid   创建   集合   using   mamicode   鼠标   span   

这次写的是小游戏扫雷,由于时间问题,扫雷的成功失败判断还没有写的很好,只是把大体的思路写出来了。

这次最主要的是有两个类

第一个是地雷类 :lanmine

技术图片
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Drawing;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 using System.Windows.Forms;
 8 using 扫雷.Properties;
 9 
10 namespace 扫雷
11 {
12     class landmine
13     {
14 
15         #region 地雷属性
16         //创建button按钮作为地雷
17         public Button btnLandmine { get; set; }
18         //地雷的位置
19         public int X { get; set; }
20         public int Y { get; set; }
21         //地雷的大小
22         public int width { get; set; }
23         public int height { get; set; }
24         //按钮状态
25         public bool isClick { get; set; }
26         //按钮分数
27         public int number { get; set; }
28         //是否为地雷
29         public bool isLandmine { get; set; } 
30         #endregion
31        
32         #region 构造函数
33         public landmine(Button btnLandmine, int width, int height, int x, int y, bool isClick, int number, bool isLandmine)
34         {
35             this.btnLandmine = btnLandmine;
36             this.X = x;
37             this.Y = y;
38             this.width = width;
39             this.height = height;
40             this.isClick = isClick;
41             this.number = number;
42             this.isLandmine = isLandmine;
43         } 
44         #endregion
45         
46         #region 控件添加函数
47         public void addLandmine(Control control)
48         {
49             //初始化按钮属性
50             this.btnLandmine.Text = "";
51             this.btnLandmine.Font = new Font("宋体",15,FontStyle.Bold);
52             this.btnLandmine.Location = new Point(this.X+10, this.Y);
53             this.btnLandmine.Size = new Size(this.width, this.height);
54             //给按钮添加点击事件
55             this.btnLandmine.MouseDown += btnLandmine_MouseDown;
56             //将控件添加到父控件上(父控件为函数参数传入)
57             this.btnLandmine.Parent = control;
58         } 
59         #endregion
60        
61         #region 鼠标点击事件
62         void btnLandmine_MouseDown(object sender, MouseEventArgs e)
63         {
64             //当鼠标左击
65             if (e.Button == MouseButtons.Left)
66             {
67                 //如果是地雷
68                 if (this.isLandmine)
69                 {
70                     this.btnLandmine.BackgroundImage = Resources.bomb;
71                     MessageBox.Show("踩雷了");
72                 }
73                 if (!this.isLandmine)
74                 {
75                     this.btnLandmine.Text = this.number.ToString();
76                     this.btnLandmine.Enabled = false;
77                 }
78             }
79             //鼠标右击
80             else if (e.Button == MouseButtons.Right)
81             {
82                 Image flag = Resources.flag;
83                 if (this.btnLandmine.BackgroundImage != null)
84                 {
85                     this.btnLandmine.BackgroundImage = null;
86                 }
87                 else
88                 {
89                     this.btnLandmine.BackgroundImage = flag;
90                 }
91 
92             }
93         } 
94         #endregion
95         
96 
97     }
98 }
View Code

 第二个是地雷集合类:lanmineList

技术图片
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Drawing;
  4 using System.Linq;
  5 using System.Text;
  6 using System.Threading.Tasks;
  7 using System.Windows.Forms;
  8 using 扫雷.Properties;
  9 
 10 namespace 扫雷
 11 {
 12     class landmineList
 13     {
 14         //创建地雷数组
 15         public landmine[,] landmineArr;        
 16                
 17         //初始化地雷数组
 18         #region 初始化地雷数组
 19         public void landmineArrSet()
 20         {
 21             this.landmineArr = new landmine[10, 10];
 22             for (int i = 0; i < 10; i++)
 23             {
 24                 for (int j = 0; j < 10; j++)
 25                 {
 26                     landmine item = new landmine(new Button(),30,30,i*35,j*35,false,0,false);
 27                     this.landmineArr[i, j] = item;                       
 28                 }
 29             }
 30         } 
 31         #endregion
 32 
 33         //添加地雷数组到父控件
 34         #region 将地雷数组添加到父控件
 35         public void addControl(Control control)
 36         {
 37             for (int i = 0; i < 10; i++)
 38             {
 39                 for (int j = 0; j < 10; j++)
 40                 {
 41                     this.landmineArr[i, j].addLandmine(control);
 42                 }
 43             }
 44         } 
 45         #endregion
 46 
 47         //地雷生成存储
 48         public List<randomList> landmineNumber { get; set; }
 49 
 50         //随机产生地雷函数
 51         #region 随机产生地雷函数
 52         public List<randomList> randomLandmine(int number)
 53         {
 54             List<randomList> result = new List<randomList>();
 55             Random r = new Random();
 56             for (int i = 0; i < number; i++)
 57             {
 58                 randomList item = new randomList();
 59                 item.i = r.Next(0, 9);
 60                 item.j = r.Next(0, 9);
 61                 result.Add(item);
 62             }
 63             return result;
 64         }
 65 
 66         #endregion
 67 
 68         //将地雷的周围控件的number值加1
 69         #region 将地雷控件周围的number值加1
 70         public void numberAdd()
 71         {
 72             foreach (randomList item in this.landmineNumber)
 73             {
 74                 landmineArr[item.i, item.j].isLandmine = true;
 75                 //左上角                        
 76                 if (item.i - 1 >= 0 && item.j - 1 >= 0)
 77                 {
 78                     landmineArr[item.i-1, item.j-1].number++;
 79                 }
 80                 //中上位置
 81                 if (item.i - 1 >= 0)
 82                 {
 83                     landmineArr[item.i - 1, item.j].number++;
 84                 }
 85                 //右上角
 86                 if (item.i - 1 >= 0 && item.j + 1 < 10)
 87                 {
 88                     landmineArr[item.i - 1, item.j + 1].number++;
 89                 }
 90                 //左中位置
 91                 if (item.j - 1 >= 0)
 92                 {
 93                     this.landmineArr[item.i, item.j - 1].number++;
 94                 }
 95                 //右中位置
 96                 if (item.j + 1 < 10)
 97                 {
 98                     landmineArr[item.i, item.j + 1].number++;
 99                 }
100                 //左下角
101                 if (item.i + 1 < 10 && item.j - 1 >= 0)
102                 {
103                     landmineArr[item.i + 1, item.j - 1].number++;
104                 }
105                 //中下位置
106                 if (item.i + 1 < 10)
107                 {
108                     landmineArr[item.i + 1, item.j].number++;
109                 }
110                 //右下位置
111                 if (item.i + 1 < 10 && item.j + 1 < 10)
112                 {
113                     landmineArr[item.i + 1, item.j + 1].number++;
114                 }
115             }
116         }
117 
118         #endregion
119 
120         //判断红旗是否全部插在地雷上
121         #region 判断红旗是否全部插在地雷上
122         public bool insterFlag()
123         {
124             foreach (randomList item in this.landmineNumber)
125             {
126                 if (this.landmineArr[item.i, item.j].btnLandmine.BackgroundImage != Resources.flag)
127                 {
128                     return false;
129                 }
130             }
131             return true;
132         } 
133         #endregion        
134     }
135 
136 }
View Code

第三是from窗体:from1

技术图片
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 using 扫雷;
11 
12 namespace 扫雷
13 {
14     public partial class Form1 : Form
15     {
16         public Form1()
17         {
18             InitializeComponent();
19         }
20 
21 
22         landmineList landmine = new landmineList(); 
23         private void Form1_Load(object sender, EventArgs e)
24         {
25             Control control=this.panel1;          
26             landmine.landmineArrSet();
27             landmine.landmineNumber = landmine.randomLandmine(10);
28             landmine.numberAdd();
29             landmine.addControl(control);
30         }
31 
32         private void panel1_Paint(object sender, PaintEventArgs e)
33         {
34 
35         }
36 
37         private void timer1_Tick(object sender, EventArgs e)
38         {
39             if (landmine.insterFlag())
40             {
41                 MessageBox.Show("通过");
42                 this.timer1.Enabled = false;
43             }
44         }
45     }
46 }
View Code

技术图片

 

第四是随机数类:randomList

技术图片
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 扫雷
 8 {
 9     class randomList
10     {
11         //随机 i值
12         public int i { get; set; }
13         //随机 j值
14         public int j { get; set; }
15     }
16 }
View Code

最主要的思路是

1.地雷类中有number属性,在地雷集合类中,如果有一个地雷,那么地雷周围的lanmine的number属性值加1,这样就可以实现地图的布置

2.判断扫雷的输赢方法是看是否所有的地雷都插上红旗,或者所有不是地雷的方块都被左键单击。如果地雷被左击,那么久结束游戏。

 

C# 实现扫雷

标签:init   游戏   oid   创建   集合   using   mamicode   鼠标   span   

原文地址:https://www.cnblogs.com/niaofei123/p/13765599.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!