附录1:主界面程序代码
在运行窗体中添加一个button控件,一个menuStrip控件,一个toolStrip控件,两个label控件,分别 用来计时和计算剩余的地雷数。 图片如下:
程序结构分布图
运行窗体的代码
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data;
19
using System.Drawing; using System.Linq; using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging; //加入图片的命名空间
namespace 扫|?§雷¤?á {
public partial class Form2 : Form {
//定义失败时,地雷图片的路径
private string shibai = System.Environment.CurrentDirectory + \; //定义右键单击时,标记旗子图片的路径
private string flag = System.Environment.CurrentDirectory + \; //定义失败时,地雷炸开的路径
private string zhakai = System.Environment.CurrentDirectory + \; //定义开始按钮的图片路径
private string xiaolian = System.Environment.CurrentDirectory + \; //定义失败时,开始按钮图片路径
private string kulian = System.Environment.CurrentDirectory + \; //定义成功时,开始按钮图片路径
private string cheng = System.Environment.CurrentDirectory + \; //计时器
private Timer timer1 = new Timer(); //所用时间
public int yongshi = 0; //定义地雷数
private static int leishu;
public int Leishu {
get { return Form2.leishu; } set { Form2.leishu = value; } }
//游戏是否结束
public static bool over = false; //生成雷的行数 private int hang;
public int Hang {
get { return hang; } set { hang = value; } }
20
//生成雷的列数 private int lie;
public int Lie {
get { return lie; } set { lie = value; } }
//游戏过程中剩余的地雷数量 private int restlie; int ci = 1,zong;
//生成个按钮数组
private LeiButton[,] button = new LeiButton[32,32];
private void button1_Click(object sender, EventArgs e) {
Fuyuan(); }
public Form2() {
InitializeComponent(); }
private void Form2_Load(object sender, EventArgs e) {
start(10,9,9);
timer1.Tick += new EventHandler(timer1_Tick); timer1.Interval = 1000; zong = 10; fuli(); }
private void Leizheng() {
for (int i = 0; i < lie; i++) {
for (int j = 0; j < hang; j++) {
button[j, i] = new LeiButton();
21
button[j, i].Location = new Point(1 + i * 20, 55 + j * 20); //button[j,i].BackgroundImage= Image.FromFile(beijing); button[j, i].X = j; button[j, i].Y = i; button[j, i].Youlei = 0;
this.Controls.Add(button[j, i]);
//定义LeiButton按钮的鼠标事件,大家共享
button[j, i].MouseUp += new MouseEventHandler(bt_MouseUp); } } }
//定义鼠标单击事件,单击按钮时触发该事件
private void bt_MouseUp(object sender, MouseEventArgs e) {
if (!over) {
int x, y;
LeiButton b = (LeiButton)sender; x = b.X; y = b.Y;
switch (e.Button) { //鼠标左键触发事件 case MouseButtons.Left:
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; over = true; }
} else {
button[x, y].BackgroundImage = Image.FromFile(zhakai);
timer1.Enabled = false;
button1.BackgroundImage = Image.FromFile(kulian); xianshi();
22
over = true; ci = 1; fuli(); } break;
//鼠标右键触发事件 case MouseButtons.Right:
button[x, y].BackgroundImage = Image.FromFile(flag); if (Convert.ToInt16(button[x, y].Tag) == 1) {
button[x, y].Tag = 0; restlie++;
button[x, y].BackgroundImage = null;
} else {
button[x, y].Tag = 1;
//button[x, y].Enabled = false; restlie--; }
lei.Text = restlie.ToString() + \颗?\; if (Win()) {
button1.BackgroundImage = Image.FromFile(cheng); timer1.Enabled = false; over = true; ci++; fuli(); } break;
} } else return;
}
//动态布置地雷,产生随机数布雷 private void Bulei() {
Random rand = new Random(); for (int i = 0; i < leishu; i++)
23