C# 实现扫雷
2020-12-18 12:35
标签:init 游戏 oid 创建 集合 using mamicode 鼠标 span 这次写的是小游戏扫雷,由于时间问题,扫雷的成功失败判断还没有写的很好,只是把大体的思路写出来了。 这次最主要的是有两个类 第一个是地雷类 :lanmine 第二个是地雷集合类:lanmineList 第三是from窗体:from1 第四是随机数类:randomList 最主要的思路是 1.地雷类中有number属性,在地雷集合类中,如果有一个地雷,那么地雷周围的lanmine的number属性值加1,这样就可以实现地图的布置 2.判断扫雷的输赢方法是看是否所有的地雷都插上红旗,或者所有不是地雷的方块都被左键单击。如果地雷被左击,那么久结束游戏。 C# 实现扫雷 标签:init 游戏 oid 创建 集合 using mamicode 鼠标 span 原文地址:https://www.cnblogs.com/niaofei123/p/13765599.html 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 }
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
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 }
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 }