c#仿简单扫雷游戏

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;
            }

        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值