using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace saolei
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int count = 7; // 多少格
int sz = 70; // 格子的长或宽
int[,] bombArr; // 二维数组存底层数字
/// <summary>
/// 创造雷和底层的数字
/// </summary>
private void CreateLevel()
{
Random r = new Random();
int i = r.Next(count);
int j = r.Next(count);
bombArr = new int[count, count];
for (int idxI = 0; idxI < count; idxI++)
{
for (int idxJ = 0; idxJ < count; idxJ++)
{
if (i == idxI && j == idxJ)
{
bombArr[idxI, idxJ] = 0;//炸弹
}
else
{
bombArr[idxI, idxJ] = 1;
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
CreateLevel();
this.Size = new Size(sz * (count + 1), sz * (count + 1)); // 窗体大小
for (int i = 0; i < count; i++)
{
for (int j = 0; j < count; j++)
{
Button btn = new Button();
btn.Size = new Size(sz, sz);
btn.Location = new Point(i * sz, j * sz);
btn.Font = new Font("微软雅黑", 15);
btn.Tag = i.ToString() + "_" + j.ToString();//将位置信息存入
btn.MouseDown += Btn_MouseDown;//为按钮添加一个鼠标按下事件处理程序Btn_MouseDown
this.Controls.Add(btn);
}
}
}
//使小格子中显现位置信息
private void Btn_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
string info = ((Button)sender).Tag.ToString();
string[] xy = info.Split('_');
int i = int.Parse(xy[0]);
int j = int.Parse(xy[1]);
if(bombArr[i,j] == 0)
{
((Button)sender).BackgroundImage = Image.FromFile("地雷.png");
((Button)sender).BackColor = Color.Transparent;
((Button)sender).BackgroundImageLayout = ImageLayout.Zoom;
}
else
{
((Button)sender).Text = bombArr[i, j].ToString();
}
}
else if (e.Button == MouseButtons.Right)
{
((Button)sender).BackgroundImage = Image.FromFile("红旗.png");
((Button)sender).BackColor = Color.Transparent;
((Button)sender).BackgroundImageLayout = ImageLayout.Zoom;
}
}
}
}
c#仿简单扫雷游戏
最新推荐文章于 2024-07-20 22:44:11 发布