当前位置:文档之家› 1C#扫雷源代码

1C#扫雷源代码

Program.form = this; restlie = Leishu; label4.Text = ""; groupBox1.Location = new Point(26, 26); groupBox1.Text = ""; groupBox1.Size = new System.Drawing.Size(908, 488);
} /// <summary> /// 窗体的 load 事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load(object sender, EventArgs e) {
//定义一个Form1类型的变量,用于保存扫雷窗体的实例 public static Form1 form;
} }
设置地雷数窗体的源代码
using System; using System.Collections.Generic; using ponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;
namespace saolei {
static class Program {
/// <summary> /// 应用程序主入口处 /// </summary> [STAThread] static void Main() {
Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } //上面这些都是建立项目时自动生成的
if (!(e.KeyChar >= '0' && e.KeyChar <= '9')) { e.Handled = true;}
}
private void shezhi_Load(object sender, EventArgs e) {
}
private void button1_Click(object sender, EventArgs e) {
C#扫雷源代码
在运行窗体中添加一个 button 控件,两个 label 控件,分别 计算剩余的地雷数。
图片如下:
用来计时和
程序中在定义一个 LeiButton 的类,继承于 Button 类
LeiButtton 类的代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms;//引入Button的命名空间
public int Y {
get { return y; } set { y = value; } }
public int Youlei {
get { return youlei; } set { youlei = value; } }
} //设置地雷数窗体
考虑到设置雷数量后,要马上刷新扫雷窗体的数据,因此定义一个全
if (textBox1.Text == "") {
textBox1.Focus(); return; } if (int.Parse(textBox1.Text) > 400 || int.Parse(textBox1.Text) < 10) { MessageBox.Show("请输入10---400之内的数字); textBox1.Text = string.Empty; } else { Program.form.Leishu= int.Parse(textBox1.Text); Program.form.Fuyuan(); this.Close();
namespace saolei {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); } //定义初始后开始按钮的图片 private string chushi = System.Environment.CurrentDirectory + "\\image\\face.bmp";
lie = value; } }
//生成个按钮数组 private LeiButton[,] button = new LeiButton[16, 30];
//菜单栏中游戏下拉菜单中的退出栏 private void 退出 ToolStripMenuItem_Click(object sender, EventArgs e) { this.Close(); } //菜单栏中游戏下拉菜单中的设置地雷数栏 private void 设置地雷数 ToolStripMenuItem_Click(object sender, EventArgs e) { shezhi shezhi1 = new shezhi(); shezhi1.ShowDialog();
//定义属性 public int Leishu {
get { return leishu; } set {
leishu= value; }
} public int Hang {
get { return hang; } set {
hang = value; } } public int Lie { get { return lie; } set {
public LeiButton() {
Tag = 0; /// 0表示该方块没有翻开,1表示该方块已经翻开 Size = new System.Drawing.Size(30, 30);//定义控件大小 } //定义属性 public int X {
get { return x; } set { x = value; } }
label4.Text = yongshi.ToString() + "秒"; else
label4.Text = (yongshi /60).ToString() + "分" + (yongshi % 60).ToString() + " 秒";
}
/// <summary> /// 生成雷阵的界面,即生成 LeiButton 控件(继承于 Button 控件) /// </summary> private void Leizheng() {
//定义炸死后开始按钮的图片 private string siqu = System.Environment.CurrentDirectory + "\\image\\sile.jpg"; //定义胜利的声音 private string shengli = System.Environment.CurrentDirectory + "\\image\\烟花.wav"; //定义爆炸声音 private string shengyin=System.Environment.CurrentDirectory+"\\image\\爆炸声.wav"; //定义失败时,雷炸开图片的路径 private string shibai = System.Environment.CurrentDirectory + "\\image\\shibai.bmp"; //定义右键单击时,标记旗子图片的路径 private string flag = System.Environment.CurrentDirectory + "\\image\\flag.bmp"; //地雷图片位置 private string dileiimage = System.Environment.CurrentDirectory + "\\imag\\lei.bmp"; //计时器 private Timer timer1 = new Timer(); //所用时间 private int yongshi = 0; //定义地雷数 private int leishu = 50; //游戏是否结束 private bool over = false; //生成雷的行数 private int hang = 16; //生成雷的列数 private int lie = 30; //游戏过程中剩余的地雷数量 private int restlie;
} }
private void button2_Click(object sender, EventArgs e) {
Program.form.Leishu = 50; Program.form.Fuyuan();
this.Close(); } } }
程序结构分布图
运行窗体的代码
using System; using System.Collections.Generic; using ponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing.Imaging;//引入图片的命名空间 using System.Media;//引入声音的命名空间
相关主题