📄 form1.cs
字号:
/**************************************************************************/
/*飞天扫雷 V1.0 作者:冯东伟 2005-4-11 */
/*版权所有,代码开源,有兴趣者可以继续开发 */
/*如果本代码用于商业用途,请与作者联系hust_fdw@tom.com */
/**************************************************************************/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
namespace Mining
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components;
private System.Windows.Forms.MainMenu mainMenu;
private System.Windows.Forms.MenuItem mnuGame;
private System.Windows.Forms.MenuItem mnuHelp;
private System.Windows.Forms.MenuItem mnuBegin;
private System.Windows.Forms.MenuItem mnuBegenner;
private System.Windows.Forms.MenuItem mmuMiddle;
private System.Windows.Forms.MenuItem mmuAdvanced;
private System.Windows.Forms.MenuItem mmuHeroList;
private System.Windows.Forms.MenuItem mmuExit;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.MenuItem menuItem7;
private System.Windows.Forms.Timer timer;
private Graphics g; //程序初始时创建该对象,用以以后对矩形进行填充
private int eventX; //记录事件发生所在矩形的X坐标
private int eventY; //记录事件发生所在矩形的Y坐标
byte[,] bombs = new byte[22, 22]; //用于存放地雷信息
//-------------------------数值涵义-------------------------//
//bombs[i,j] = -1 => 该区域有地雷
//bombs[i,j] = 0 => 该区域附近无地雷
//bombs[i,j] > 0 => 该区域附近的地雷数为bombs[i,j]
byte[,] bombState = new byte[22, 22]; //用于保存雷区状态
//--------------------------状态涵义------------------------//
//bombState[i,j] = 0 => 用户未判定对的无雷区
//bombState[i,j] = 1 => 用户已判定对的无雷区
//bombState[i,j] = 2 => 用户已标记的有雷区
//bombState[i,j] = 3 => 用户标记的可能有雷的区域
//待定义
//
private int arrayX; //用于对应雷区数组的行下标
private int arrayY; //用于对应雷区数组的列下标
private int time; //用于表示所用时间
private int BombNumber = 40; //游戏开始时设置为初级玩家,地雷数量为40
private int rightCheck = 40; //实际剩余的地雷数量
private int userCheck = 40; //用户标志的剩余个数
private bool gameReady = true;
private System.Windows.Forms.Label lbBombNum;
private System.Windows.Forms.Button btnReplay;
private System.Windows.Forms.Label lbTime;
private System.Windows.Forms.Panel panel1; //标记游戏可以开始
private bool gOver = false; //标记游戏已经结束
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.mainMenu = new System.Windows.Forms.MainMenu();
this.mnuGame = new System.Windows.Forms.MenuItem();
this.mnuBegin = new System.Windows.Forms.MenuItem();
this.mnuBegenner = new System.Windows.Forms.MenuItem();
this.mmuMiddle = new System.Windows.Forms.MenuItem();
this.mmuAdvanced = new System.Windows.Forms.MenuItem();
this.mmuHeroList = new System.Windows.Forms.MenuItem();
this.mmuExit = new System.Windows.Forms.MenuItem();
this.mnuHelp = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.timer = new System.Windows.Forms.Timer(this.components);
this.lbBombNum = new System.Windows.Forms.Label();
this.btnReplay = new System.Windows.Forms.Button();
this.lbTime = new System.Windows.Forms.Label();
this.panel1 = new System.Windows.Forms.Panel();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// mainMenu
//
this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuGame,
this.mnuHelp});
//
// mnuGame
//
this.mnuGame.Index = 0;
this.mnuGame.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuBegin,
this.mnuBegenner,
this.mmuMiddle,
this.mmuAdvanced,
this.mmuHeroList,
this.mmuExit});
this.mnuGame.Text = "游戏(&G)";
//
// mnuBegin
//
this.mnuBegin.Index = 0;
this.mnuBegin.Text = "开始(&S)";
this.mnuBegin.Click += new System.EventHandler(this.mnuBegin_Click);
//
// mnuBegenner
//
this.mnuBegenner.Checked = true;
this.mnuBegenner.Index = 1;
this.mnuBegenner.Text = "初级(&B)";
this.mnuBegenner.Click += new System.EventHandler(this.mnuBegenner_Click);
//
// mmuMiddle
//
this.mmuMiddle.Index = 2;
this.mmuMiddle.Text = "中级(&M)";
this.mmuMiddle.Click += new System.EventHandler(this.mmuMiddle_Click);
//
// mmuAdvanced
//
this.mmuAdvanced.Index = 3;
this.mmuAdvanced.Text = "高级(&A)";
this.mmuAdvanced.Click += new System.EventHandler(this.mmuAdvanced_Click);
//
// mmuHeroList
//
this.mmuHeroList.Index = 4;
this.mmuHeroList.Text = "英雄榜(&H)";
//
// mmuExit
//
this.mmuExit.Index = 5;
this.mmuExit.Text = "退出(&X)";
this.mmuExit.Click += new System.EventHandler(this.mmuExit_Click);
//
// mnuHelp
//
this.mnuHelp.Index = 1;
this.mnuHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem6,
this.menuItem7});
this.mnuHelp.Text = "帮助(&H)";
//
// menuItem6
//
this.menuItem6.Index = 0;
this.menuItem6.Text = "关于“飞天扫雷”";
this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
//
// menuItem7
//
this.menuItem7.Index = 1;
this.menuItem7.Text = "关于“飞天”";
this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click);
//
// timer
//
this.timer.Interval = 1000;
this.timer.Tick += new System.EventHandler(this.timer_Tick);
//
// lbBombNum
//
this.lbBombNum.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
this.lbBombNum.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.lbBombNum.Font = new System.Drawing.Font("华文仿宋", 26.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.lbBombNum.ForeColor = System.Drawing.Color.Red;
this.lbBombNum.Location = new System.Drawing.Point(40, 3);
this.lbBombNum.Name = "lbBombNum";
this.lbBombNum.Size = new System.Drawing.Size(64, 40);
this.lbBombNum.TabIndex = 0;
this.lbBombNum.Text = "00";
//
// btnReplay
//
this.btnReplay.BackColor = System.Drawing.SystemColors.ControlLight;
this.btnReplay.Location = new System.Drawing.Point(136, 8);
this.btnReplay.Name = "btnReplay";
this.btnReplay.Size = new System.Drawing.Size(72, 32);
this.btnReplay.TabIndex = 2;
this.btnReplay.Text = "重新开始";
this.btnReplay.Click += new System.EventHandler(this.btnReplay_Click);
//
// lbTime
//
this.lbTime.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
this.lbTime.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.lbTime.Font = new System.Drawing.Font("华文仿宋", 26.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.lbTime.ForeColor = System.Drawing.Color.Red;
this.lbTime.Location = new System.Drawing.Point(272, 3);
this.lbTime.Name = "lbTime";
this.lbTime.Size = new System.Drawing.Size(80, 40);
this.lbTime.TabIndex = 1;
this.lbTime.Text = "00";
//
// panel1
//
this.panel1.BackColor = System.Drawing.SystemColors.ControlLight;
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.panel1.Controls.Add(this.lbBombNum);
this.panel1.Controls.Add(this.btnReplay);
this.panel1.Controls.Add(this.lbTime);
this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.panel1.Location = new System.Drawing.Point(0, 401);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(400, 50);
this.panel1.TabIndex = 3;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(200)));
this.ClientSize = new System.Drawing.Size(400, 451);
this.Controls.Add(this.panel1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.MaximizeBox = false;
this.Menu = this.mainMenu;
this.MinimizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "飞天扫雷";
this.TopMost = true;
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
this.Load += new System.EventHandler(this.Form1_Load);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
//对游戏进行初始化设置
private void Form1_Load(object sender, System.EventArgs e)
{
//向玩家显示地雷个数
lbBombNum.Text = BombNumber.ToString();
//产生Graphics对象,以便在框体内绘图
g = this.CreateGraphics();
//调用雷区产生函数,产生雷区
makeRandomData();
}
//重新开始游戏,对游戏界面重新绘制
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
}
//绘制网格,表示雷区
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Pen pen = new Pen( Color.Goldenrod, 4 ); //用于绘制边界
for( int i = 0; i <= 400; i += 20 )
{
//绘制行
e.Graphics.DrawLine( pen, 0, i, 400, i );
//绘制列
e.Graphics.DrawLine( pen, i, 0, i, 400 );
}
}
//处理按下鼠标产室的事件
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if( !gOver )
{
//鼠标点击雷区对应的雷区数组的行坐标
arrayX = e.X / 20 + 1;
//鼠标点击雷区对应的雷区数组的列坐标
arrayY = e.Y / 20 + 1;
//鼠标点击雷区的左上横坐标
eventX = e.X / 20 * 20 + 2;
//鼠标点击雷区的左上纵坐标
eventY = e.Y / 20 * 20 + 2;
//如果该坐标已经被玩家标志为无雷区,则不作任何处理,为空语句
if( bombState[arrayX, arrayY] == 0x01 )
{
if( !isBombLeft( arrayX, arrayY ) )
{
for( int m = -1; m < 2; m ++ )
for( int n = -1; n < 2; n++ )
{
if( bombState[ arrayX + m, arrayY +n ] == 0x00 )
switch( bombs[arrayX + m, arrayY +n] )
{
case 0:
banging( arrayX + m, arrayY +n );break;
case 0xFF:
gameOver(); break;
default:
fillBombArea( ( arrayX + m - 1 ) * 20 + 2, ( arrayY + n - 1 ) * 20 + 2, arrayX + m, arrayY +n, 2); break;
}
}
}
}
//否则用红色标记,提醒用户小心
else
{
fillBombArea( eventX, eventY, arrayX, arrayY, 6);
}
}
}
//判断该雷区附近的地雷有没有全部标志
private bool isBombLeft( int xArr, int yArr )
{
int counter = 0;
for( int m = -1; m < 2; m ++ )
for( int n = -1; n < 2; n++ )
{
//如果发现周围有雷区域,bombs[i,j]的值加1
if( bombState[ xArr + m, yArr +n ] == 0x02 )
counter++;
}
if( counter == bombs[ xArr, yArr] )
return false;
return true;
}
//该函数用于产生雷区分步
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -