📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MineSweeping
{
//扫雷主窗体
public partial class Form1 : Form
{
//gameover表示游戏状态。值0表示游戏正在进行,1表示结束。
private int gameover = 0;
//dig表示当前已经挖开非雷点
private int dig = 0;
//time表示游戏已经进行的时间
private int time = 0;
//leftTag为TRUE表示左键按下
private bool leftTag=false;
//rightTag为TRUE表示右键按下
private bool rightTag=false;
//表示当前雷区宽度
private int Widths=9;
//当前雷区高度
private int Heights=9;
//当前雷区总雷数
private int Miles=10;
//remain指示剩下未标记的雷数(可能为负数,因为存在标记错误的情况)
private int remain = 10;
//MileState二维数组表示雷区每个点周围的地雷数
private int[,] MileState = new int[40,40];
//当释放鼠标时,用于检查是否需要恢复雷点背景颜色
private bool NeedToResume = false;
//需要恢复背景颜色的中心坐标(恢复以(middleX,middleY)为中心的9个点背景颜色
private int middleX;
private int middleY;
//扫雷英雄榜初级,中级,高级榜上大名
private string least_Name="匿名";
private string middle_Name="匿名";
private string highest_Name="匿名";
//对应初级,中级,高级扫雷时间
private int least_Time=999;
private int middle_Time=999;
private int highest_Time=999;
public Form1()
{
InitializeComponent();
//初始窗体大小
this.ClientSize -= new System.Drawing.Size(this.Width, this.Height);
this.ClientSize += new System.Drawing.Size(40 + 20 * this.Widths, 105 + 20 * this.Heights);
}
//重新开始按扭
private void button1_Click(object sender, EventArgs e)
{
int x = 0;
int y = 0;
//时间复位
time = -1;
timer1_Tick(sender, e);
timer1.Stop();
//游戏开始,dig,remain重新付值
gameover = 0;
dig = 0;
remain = Miles;
Fresh_Remain();
//初始化窗体大小
Initialize_Size(sender, e);
button1.Image = global::MineSweeping.Properties.Resources.Face1;
//以button显示雷区
for (x = 0; x < Widths; x++)
{
for (y = 0; y < Heights; y++)
{
this.buttonarray[x, y] = new System.Windows.Forms.Button();
this.buttonarray[x, y].Location = new System.Drawing.Point(20 + 20 * x, 60 + 20 * y);
this.buttonarray[x, y].Size = new System.Drawing.Size(20, 20);
this.buttonarray[x, y].UseVisualStyleBackColor = true;
this.buttonarray[x, y].Text = "";
this.buttonarray[x, y].MouseDown += new System.Windows.Forms.MouseEventHandler(Mouse_Down);
this.buttonarray[x, y].MouseUp += new System.Windows.Forms.MouseEventHandler(Mouse_Up);
this.buttonarray[x, y].MouseClick += new System.Windows.Forms.MouseEventHandler(Mouse_Click);
this.Controls.Add(buttonarray[x, y]);
}
}
//随机布雷
Initialize_Miles(sender, e);
}
//
//
//挖开雷区坐标为(x,y)雷点
private void Open_Button(int x, int y)
{
if (buttonarray[x, y].Text == "" || buttonarray[x, y].Text == " ")
{
int index = MileState[x, y];
if (index == 0)
{
buttonarray[x, y].Text = " ";
buttonarray[x, y].Image = null;
buttonarray[x, y].Enabled = false;
dig++;
GameState();
recurse(x, y);
}
else if (index > 0)
{
buttonarray[x, y].Image = Get_Image(MileState[x, y]);
buttonarray[x, y].Text = " ";
dig++;
GameState();
}
else if (index == -1)
{
buttonarray[x, y].Image = global::MineSweeping.Properties.Resources.MarkedWrong;
MileState[x, y] = 0;
Game_Over();
}
}
}
//
//单击挖开鼠标所在雷点
private void Mouse_Click(object sender, EventArgs e)
{
if (gameover == 0)
{
timer1.Start();
}
if (leftTag == true)
{
int x = this.PointToClient(MousePosition).X / 20 * 20;
int y = this.PointToClient(MousePosition).Y / 20 * 20;
x = (x - 20) / 20;
y = (y - 60) / 20;
Open_Button(x, y);
leftTag = false;
}
}
//
///鼠标按下事件
private void Mouse_Down(object sender, MouseEventArgs e)
{
if (gameover == 0)
{
button1.Image = global::MineSweeping.Properties.Resources.Face2;
}
int x = this.PointToClient(MousePosition).X / 20 * 20;
int y = this.PointToClient(MousePosition).Y / 20 * 20;
x = (x - 20) / 20;
y = (y - 60) / 20;
if (e.Button == MouseButtons.Right)
rightTag = true;
if (e.Button == MouseButtons.Left)
leftTag = true;
if (leftTag && rightTag)
{
int open=letout(x - 1, y - 1)+letout(x - 1, y)+ letout(x - 1, y + 1)+letout(x, y - 1);
open +=letout(x, y + 1)+letout(x + 1, y - 1)+letout(x + 1, y)+letout(x + 1, y + 1);
if (buttonarray[x, y].Text == " " && open == MileState[x, y])
{
if (Check(x - 1, y - 1) == true)
Open_Button(x - 1, y - 1);
if (Check(x - 1, y) == true )
Open_Button(x - 1, y);
if (Check(x - 1, y + 1) == true)
Open_Button(x - 1, y + 1);
if (Check(x, y - 1) == true)
Open_Button(x, y - 1);
if (Check(x, y + 1) == true)
Open_Button(x, y+1);
if (Check(x + 1, y - 1) == true)
Open_Button(x + 1, y - 1);
if (Check(x + 1, y) == true)
Open_Button(x + 1, y );
if (Check(x + 1, y + 1) == true)
Open_Button(x + 1, y + 1);
}
leftTag = false;
rightTag = false;
//显示周围未挖开点或问号点(改变背景颜色)
middleX = x;
middleY = y;
NeedToResume = true;
ChangeBackColor(x-1, y-1);
ChangeBackColor(x-1, y);
ChangeBackColor(x-1, y+1);
ChangeBackColor(x, y-1);
ChangeBackColor(x, y);
ChangeBackColor(x, y+1);
ChangeBackColor(x+1, y-1);
ChangeBackColor(x+1, y);
ChangeBackColor(x+1, y+1);
}
}
///
private void ChangeBackColor(int x,int y)
{
//通过改变背景颜色显示未挖开点
if(x>=0&&x<Widths&&y>=0&&y<Heights&&buttonarray[x,y].Text !=" "&&buttonarray[x,y].Text !=" ")
{
buttonarray[x, y].BackColor = Color.Chocolate;
if (buttonarray[x, y].Text == " ")
buttonarray[x, y].Image = null;
}
}
///检查当前雷点(x,y)是否可挖
private bool Check(int x, int y)
{
if (x >= 0 && y >= 0 && x < Widths && y < Heights)
{
if (buttonarray[x, y].Text == ""||buttonarray[x,y].Text==" ")
return true;
else
return false;
}
return false;
}
///
///坚持当前雷点是否已标记
private int letout(int x,int y)
{
if (x >= 0 && y >= 0 && x < Widths && y < Heights)
{
if (buttonarray[x, y].Text == " ")
return 1;
else
return 0;
}
else
return 0;
}
///
///如果(x,y)可挖,则挖开此点
private void check(int x, int y)
{
if (x >= 0 && y >= 0 && x < Widths && y < Heights)
{
if (buttonarray[x, y].Text == ""||buttonarray[x,y].Text ==" ")
{
if (MileState[x, y] == 0)
{
//此点周围无雷,则进入递归展开周围8点
buttonarray[x, y].Text = " ";
buttonarray[x, y].Image = null;
dig++;
GameState();
recurse(x, y);
}
else
{
//挖开此点
dig++;
GameState();
buttonarray[x, y].Image = Get_Image(MileState[x, y]);
buttonarray[x, y].Text = " ";
}
}
}
}
///
//根据雷数返回对应背景图片
private Image Get_Image(int i)
{
switch (i)
{
case 1:
return global::MineSweeping.Properties.Resources.button1;
case 2:
return global::MineSweeping.Properties.Resources.button2;
case 3:
return global::MineSweeping.Properties.Resources.button3;
case 4:
return global::MineSweeping.Properties.Resources.button4;
case 5:
return global::MineSweeping.Properties.Resources.button5;
case 6:
return global::MineSweeping.Properties.Resources.button6;
case 7:
return global::MineSweeping.Properties.Resources.button7;
default :
return global::MineSweeping.Properties.Resources.button8;
}
}
//
//展开周围无雷点周围8个点
private void recurse(int x, int y)
{
buttonarray[x, y].Enabled = false;
check(x - 1, y - 1);
check(x - 1, y );
check(x - 1, y + 1);
check(x , y -1);
check(x , y + 1);
check(x +1, y - 1);
check(x +1, y );
check(x +1, y + 1);
}
//扫雷难度等级:初级
private void 初级BToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.MenuItem4.Checked == false)
{
this.Widths = 9;
this.Heights = 9;
this.Miles = 10;
this.MenuItem5.Checked = false;
this.MenuItem6.Checked = false;
this.MenuItem7.Checked = false;
this.MenuItem4.Checked = true;
this.button1_Click(sender, e);
}
}
//扫雷难度等级:中级
private void 中级TToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.MenuItem5.Checked == false)
{
this.Widths = 16;
this.Heights = 16;
this.Miles = 40;
this.MenuItem4.Checked = false;
this.MenuItem6.Checked = false;
this.MenuItem7.Checked = false;
this.MenuItem5.Checked = true;
this.button1_Click(sender, e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -