C# 如何打开和关闭生成的按钮?

C# 如何打开和关闭生成的按钮?,c#,C#,我想在C#做一个古怪的鼹鼠游戏。到目前为止,我所做的是让一个按钮生成一个按钮网格作为游戏场。游戏的要点是点击被激活的按钮获得一分。现在,我所坚持的是(正如标题所说)这样做,一旦我点击“开始”,一个按钮将保持活动状态,而所有其他按钮将被禁用 以下是我目前的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; u

我想在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 Whack_a_mole
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            label1.Visible = false;
            label1.Text = "Max size is 5";

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int Area = Convert.ToInt32(textBox1.Text);
            if (Area < 6)
            {
                GenerateGrid(20, 20, Area, Area, 75, 75);
            } else
            {
                label1.Visible = true;
            }

        }

        private void GenerateGrid(int gridleft, int gridtop, int horizontalCount, int verticalcount, int width, int height)
        {
            for (int i = 0; i < verticalcount; i++)
            {
                GenerateRowOfButtons(gridleft, gridtop+height*i, horizontalCount, width, height);
            }
        }

        private void GenerateRowOfButtons (int startX, int startY, int count, int width, int height)
        {
            for (int i = 0; i < count; i++)
            {
                GenerateButton(startX+i*width, startY, width, height);
            }
        }

        private void GenerateButton (int x, int y, int width, int height)
        {
            Button button = new Button();
            button.Width = width;
            button.Height = height;
            button.Left = x;
            button.Top = y;
            this.Controls.Add(button);
        }


    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间重击摩尔
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
标签1.可见=假;
label1.Text=“最大尺寸为5”;
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
int Area=Convert.ToInt32(textBox1.Text);
如果(面积<6)
{
GenerateGrid(20,20,面积,面积,75,75);
}否则
{
标签1.可见=真;
}
}
私有void GenerateGrid(int-gridleft、int-gridtop、int-horizontalCount、int-verticalcount、int-width、int-height)
{
对于(int i=0;i
您可以使用来决定哪个
按钮打开,哪个
按钮关闭

如果一次只需要激活一个
按钮
,一种简单的方法是向
Form1
类添加几个字段

  • 一个
    System.Collections.Generic.List
    来存储在
    Form1.GenerateButton
    中创建的所有按钮。例如,我们可以称之为
    \u按钮
  • 用于存储当前选定按钮的
    按钮。例如,我们可以称之为
    \u enabledButton
  • A
    Random
    生成随机数,以便您可以随机选择按钮。例如,我们可以称之为
    \u rng
GenerateButton
的内部,确保设置了
button.Enabled=false
,然后还将
按钮添加到
\u buttons
字段

然后将一个方法添加到
表单1
ActivateRandomButton
,例如,该方法执行以下操作

private void ActivateRandomButton()
{
如果(_enabledButton!=null)
{
//如果其中一个按钮已启用,请将其禁用。
_enabledButton.Enabled=false;
}
//随机选择您创建的按钮并启用它。
_enabledButton=_buttons[_rng.Next(0,_buttons.Count)];
_enabledButton.Enabled=true;
}
现在,在生成所有
按钮后,可以调用
ActivateRandomButton
打开其中一个按钮。您可能最终会添加一个
计时器
,您可以在
勾选
事件中调用
ActivateRandomButton


请注意,这是一种非常简单的方法。它有一些问题,例如一次总是有一个且只有一个按钮处于活动状态。我已经很久没有玩过打鼹鼠的游戏了,但我觉得有可能一次有多个鼹鼠“活跃”,甚至一次有零个鼹鼠“活跃”。不过,希望这个答案能给你一些开始和构建的东西。

根据我的评论-请注意,这段代码完全没有经过测试,甚至可能无法编译,因为我在手机上:

namespace Whack_a_mole
{
    public partial class Form1 : Form
    {
        private Random _rng = new Random(); //used to determine mole sleep and wake times
        private Timer _t = new Timer(); //ticks ever 0.1 s
        private List<Button> _buttons = new List<Button>(); //track our mole buttons

        public Form1()
        {
            InitializeComponent();
            label1.Visible = false;
            label1.Text = "Max size is 5";
            _t.Interval = 100;
            _t.Click += Timer_Tick;
        }

        private void StartButton_Click(object sender, EventArgs e)
        {
            _t.Start(); //start the timer

        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            //every 0.1 s we scan the board poke every mole
            foreach(Button b in _buttons){
              b.Enabled = (b.Tag as Mole).IsAwake(); //isawake will sometimes wake a mole up or put it to sleep if we didnt whack it soon enough
              if(b.Enabled) //if the mole is awake
                b.BackColor = Color.Red; //red tbuton = awake mole
              else
                b.BackColor = Color.Gray; //sleeping mole
            }
        }

        private void MoleButton_Click(object sender, EventArgs e)
        {
            Button b = (sender as Button); //many buttons share this handler; which button was clicked?
            Mole m = (b.Tag as Mole); //get the mole for that button
            m.Whack(); //whack it - puts to sleep and resets the sleep timer
            b.Enabled = false; //turn this button off
            b.BackColor = Color.Gray;
            b.Text = m.WhackCounter.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int Area = Convert.ToInt32(textBox1.Text);
            if (Area < 6)
            {
                GenerateGrid(20, 20, Area, Area, 75, 75);
            } else
            {
                label1.Visible = true;
            }

        }

        private void GenerateGrid(int gridleft, int gridtop, int horizontalCount, int verticalcount, int width, int height)
        {
            for (int i = 0; i < verticalcount; i++)
            {
                GenerateRowOfButtons(gridleft, gridtop+height*i, horizontalCount, width, height);
            }
        }

        private void GenerateRowOfButtons (int startX, int startY, int count, int width, int height)
        {
            for (int i = 0; i < count; i++)
            {
                GenerateButton(startX+i*width, startY, width, height);
            }
        }

        private void GenerateButton (int x, int y, int width, int height)
        {
            Button button = new Button();
            button.Width = width;
            button.Height = height;
            button.Left = x;
            button.Top = y;
            button.Tag = new Mole(_rng); //associate a mole with the button
            button.BackColor = Color.Gray;
            button.Enabled = false;
            button.Click += MoleButton_Click; //all mole buttons handled by the smae click handler
            this.Controls.Add(button);
        }


    }
}

  //mole is a thing that has a counter, we decrement every 0.1 seconds by asking IsAwake()
 // the mole may wake up, and it is awake for another random time then sleeps again
  class Mole{

    //count the whacks. will put this on a button text hence public
    public int WhackCounter{get; set;}=0;
    private int _counter = 0; //this tracks how long the mole sleeps / wakes for
    private Random _r;
    private bool _awake = false; //this toggles every time the counter hits 0

    public Mole(Random r){
      _r = r;
      _counter = r.Next(1,100) //init to a random sleep time
    }

    public void Whack(){
      WhackCounter++; //up the whacks
      _counter = r.Next(10,100); //at tenths of a second it sleeps for 1 to 10 seconds
      _awake = false; //put to sleep
    }

    public bool IsAwake(){ //this method decrements the counter then returns if the mole is awake
      _counter--;
      if(_counter < 1){ //did we hit 0? cycle the mole state and re-randomize
        //toggle state
        _awake = !_awake;
        if(_awake)
          _counter = r.Next(20,50); //moles wake for between 2 and 5 seconds
        else
          _counter = r.Next(10,100);
      }
      return _awake;
    }
  }
namespace重击摩尔
{
公共部分类Form1:Form
{
private Random _rng=new Random();//用于确定鼹鼠睡眠和唤醒时间
专用计时器_t=new Timer();//每0.1秒滴答一次
私有列表_buttons=new List();//跟踪我们的按钮
公共表格1()
{
初始化组件();
标签1.可见=假;
label1.Text=“最大尺寸为5”;
_t、 间隔=100;
_t、 单击+=计时器勾号;
}
私有无效开始按钮单击(对象发送者,事件参数e)
{
_t、 Start();//启动计时器
}
私有无效计时器(对象发送方、事件参数)
{
//每0.1秒,我们扫描一次电路板,每一个鼹鼠
foreach(按钮b在_按钮中){
b、 Enabled=(b.Tag as Mole).IsAwake();//如果我们没有足够快地把鼹鼠打醒,IsAwake有时会把鼹鼠叫醒或让它睡觉
if(b.Enabled)//如果鼹鼠醒着
b、 BackColor=Color.Red;//红色tbuton=awake mole
其他的
b、 BackColor=Color.Gray;//睡眠痣
}
}
私有按钮单击(对象发送者,事件参数e)
{
按钮b=(发送者为按钮);//许多按钮共享此处理程序;单击了哪个按钮?
摩尔m=(b.标记为摩尔);//获取该按钮的摩尔
m、 敲打();//敲打它-使其进入睡眠状态并重置睡眠计时器
b、 Enabled=false;//关闭此按钮
b、 背景色=颜色。灰色;
b、 Text=m.WhackCounter.ToString();