随着期末的到来,可视化编程结课了,我们要做一个课设,我选择了《扫雷》游戏,在制作过程中也是参考网上的资源完成的,这里我做一个小总结,给我的大学留点证据。
首先第一步就是创建一个Form窗体,然后添加一个控件。这里我添加了一个Button控件和三个Lable控件,一个Timer控件,还有一个MenuStrip控件,如图所示
建立之后就要实现“方格”了,这里使用了一个二维的Button数组来生成这些方格,如图
以下是生成方块的代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int XNum; //一行方块的数目
private int YNum; //一列方块的数目
private int MineNum; //雷的总数
private int RestMine;//剩余的雷数
private int CostTime = 0; //所用时间
private int MineWidth = 40;//雷方块的大小(宽度为40)
private Button[,] Mines;
private int[,] Turn; //==-1 表示这个位置已经翻开;
//==0 表示这个位置没有翻开;
//==1 表示这个位置插上红旗;
private void Form1_Load(object sender, EventArgs e)
{
panel1.Visible=false;//游戏未开始时,panel里的控件不可见
Turn = new int[XNum, YNum];
Mines = new Button[XNum, YNum];
for (int x = 0; x < XNum; x += 1)
for (int y = 0; y < YNum; y += 1)
{
Mines[x, y] = new Button();
this.Controls.Add(Mines[x, y]);
Mines[x, y].Left = 13 + MineWidth * x;
Mines[x, y].Top = 75 + MineWidth * y;
Mines[x, y].Width = MineWidth;
Mines[x, y].Height = MineWidth;
Mines[x, y].Font = new Font("7SEG76", 18F, FontStyle.Bold, GraphicsUnit.Point,130);
Mines[x, y].BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
Mines[x, y].Name = "Mines" + (x + y * XNum).ToString();
Mines[x, y].MouseUp += new MouseEventHandler(bt_MouseUp);
Mines[x, y].Visible = false;
Mines[x, y].BackColor = Color.Gray;
}
}
然后是布雷
private void LayMines()// 布雷
{
int x, y;
Random s = new Random();
//取随机数
for (int i = 0; i < MineNum;)
{
//取随机数
x = s.Next(XNum);
y = s.Next(YNum);
if (Convert.ToInt16(Mines[x, y].Tag) != 1)//==1时,代表这个位置是地雷
{
Mines[x, y].Tag = 1;//修改属性为雷
i++;
}
}
}
获取方块周围的雷数
///
/// 获取某个小方块区域相邻8个区域的雷个数
///
private int GetAroundNum(int row, int col)
{
int i, j;
int around = 0;
int minRow = (row == 0) ? 0 : row - 1;
int maxRow = row + 2;
int minCol = (col == 0) ? 0 : col - 1;
int maxCol = col + 2;
for (i = minRow; i < maxRow; i++)
{
for (j = minCol; j < maxCol; j++)
{
if (!IsInMineArea(i, j))//判断是否在扫雷区域
continue;
if (Convert.ToInt16(Mines[i, j].Tag) == 1) around++;
}
}
return around;
}
判断是否在雷区以及无雷区的拓展
///
/// 判断是否在扫雷区域
///
private bool IsInMineArea(int row, int col)
{
return (row >= 0 && row < XNum && col >= 0 && col < YNum);
}
///
///雷方块拓展(对于周围无雷的空白区域)
///
private void ExpandMines(int row, int col)
{
int i, j;
int minRow = (row == 0) ? 0 : row - 1;
int maxRow = row + 2;
int minCol = (col == 0) ? 0 : col - 1;
int maxCol = col + 2;
int around = GetAroundNum(row, col);
//对周围一个雷都没有的空白区域拓展
if (around == 0)
{
Mines[row, col].Enabled = false;
for (i = minRow; i < maxRow; i++)
{
for (j = minCol; j < maxCol; j++)
{
//对于周围可以拓展的区域进行的规拓展
if (!IsInMineArea(i, j)) continue;
if (!(i == row && j == col) && Mines[i, j].Enabled != false)
//&& Convert.ToInt16(Mines[i,j].Tag)!= 1
{
ExpandMines(i, j);
Mines[i, j].BackColor = Color.White;
}
Mines[i, j].Enabled = false;//周围无雷的区域按钮无效
if (GetAroundNum(i, j) != 0) //周围无雷**********6--28
{ Mines[i, j].Text = GetAroundNum(i, j).ToString();
Mines[i, j].BackColor = Color.White;
}
}
}
}
}
胜利判断以及事件的处理
///
/// 胜利判断并处理
///
private bool Victory()// 检测是否胜利
{
for (int i = 0; i < XNum; i++)
for (int j = 0; j < YNum; j++)
{
//没翻开且未标示,则未成功
if (Mines[i, j].Enabled == true && Turn[i, j] != 1) return false;
//不是雷却误标示为雷,则也未成功
if (Convert.ToInt16(Mines[i, j].Tag) != 1 && Turn[i, j] == 1) return false;
}
return true;
}
接下来是最重要的一个函数,就是鼠标点击的处理事件
//
//添加按钮控件Click事件与处理方法bt_Click:
//
private void bt_MouseUp(object sender, MouseEventArgs e) //这里处理事件方法
{
String btName;
Button bClick = (Button)sender;//将被击的按钮赋给定义的bClick变量
btName = bClick.Name;//获取按钮的Name
int n = Convert.ToInt16(btName.Substring(5));
int x = n % XNum;
int y = n / XNum;
switch (e.Button)//通过按钮Name属性来判断是哪个Button被点击,并执行相应的操作
{
case MouseButtons.Left:
System.Media.SoundPlayer sp = new System.Media.SoundPlayer(Properties.Resources.knock);
sp.Play();
sp.Dispose();
if (Convert.ToInt16(Mines[x, y].Tag) != 1)
{
Mines[x, y].BackgroundImage = null;
Mines[x, y].BackColor = Color.White;
if (GetAroundNum(x, y) != 0) //周围无雷**********6--28
Mines[x, y].Text = GetAroundNum(x, y).ToString();
Mines[x, y].Enabled = false;
ExpandMines(x, y);
if (Victory())
{
System.Media.SoundPlayer spp = new System.Media.SoundPlayer(Properties.Resources.victory);
spp.Play();
spp.Dispose();
show();// 判断是否胜利,是则将地图中所有雷标识出来
timer1.Enabled = false;//停止计时
MessageBox.Show("恭喜你!排雷成功!", "游戏结束");
Abate();
}
}
else
{
System.Media.SoundPlayer spp = new System.Media.SoundPlayer(Properties.Resources.bang);
spp.Play();
spp.Dispose();
Mines[x, y].BackgroundImage = Properties.Resources.mine1;
timer1.Enabled = false;//停止计时
show();
MessageBox.Show("很遗憾!您踩雷了!", "游戏结束");
Abate();
}
break;
case MouseButtons.Right:
System.Media.SoundPlayer ssp = new System.Media.SoundPlayer(Properties.Resources.flag1);
ssp.Play();
ssp.Dispose();
Mines[x, y].BackgroundImage = Properties.Resources.flag;
if (Turn[x, y] == 1)//表示这个位置插上红旗
{
Turn[x, y] = 0;//取消红旗,表示这个位置没有翻开
RestMine++;
Mines[x, y].BackgroundImage = null;
}
else
{
Turn[x, y] = 1;//表示这个位置插上红旗
RestMine--;
}
lblMine.Text = RestMine.ToString();
if (Victory())
{
System.Media.SoundPlayer spp = new System.Media.SoundPlayer(Properties.Resources.victory);
spp.Play();
spp.Dispose();
timer1.Enabled = false;//停止计时
MessageBox.Show("恭喜你!排雷成功!", "游戏结束");
Abate();
}
break;
}
}
还有一个游戏初始化函数
private void GameInit()//游戏初始化
{
for (int x = 0; x < XNum; x += 1)
for (int y = 0; y < YNum; y += 1)
{
Mines[x, y].Text = "";
Mines[x, y].Visible = true;
Mines[x, y].Enabled = true;
Mines[x, y].Tag = null;
Mines[x, y].BackgroundImage = null;
Turn[x, y] = 0;
Mines[x, y].BackColor = Color.Gainsboro;
}
LayMines();
}
接下来就是一些比较简单的函数了,比如雷区的显示,计时器计数等
private void show()//将地图中所有雷标识出来
{
for (int i = 0; i < XNum; i++)
for (int j = 0; j < YNum; j++)
if (Convert.ToInt16(Mines[i, j].Tag) == 1)//==1时,代表这个位置是地雷
{
Mines[i, j].BackgroundImage = Properties.Resources.mine;
}
}
private void timer()//计时器计时,清零
{
lblTime.Text = "0";
CostTime = 0;
lblTime.Text = CostTime.ToString();
}
private void Abate()//游戏结束时使方块失效
{
for (int x = 0; x < XNum; x += 1)
for (int y = 0; y < YNum; y += 1)
{
Mines[x, y].Enabled = false;
}
}
private void clear() //清除方块
{
for (int x = 0; x < XNum; x += 1)
for (int y = 0; y < YNum; y += 1)
{
Mines[x, y].Enabled = false;
Mines[x, y].Visible = false;
}
}
最后就是一些按钮的Click事件
private void button2_Click(object sender, EventArgs e)
{
show();//将地图中所有雷标识出来
}
private void btnStart_Click_1(object sender, EventArgs e)
{
System.Media.SoundPlayer sp = new System.Media.SoundPlayer(Properties.Resources.start);
sp.Play();
sp.Dispose();
GameInit();//游戏初始化
lblTime.Text = "0";
timer();
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
CostTime++;
lblTime.Text = CostTime.ToString();
}
代码部分到这里就结束了,最后给大家来几张成品图
最后提醒一下,在程序中有音效的调用,如果你借用该代码的时候一到删除这些代码,否则会报错。