-2
namespace Breakout
{
    public partial class Form1 : Form
    {
        //declaring variables
        private int x, y;
        private int xChange, yChange;
        private Graphics canvas;
        private SolidBrush brush;
        private Random randNum;

        public Form1()
        {
            InitializeComponent();

            //creating variables
            canvas = picBoxCanvas.CreateGraphics();
            brush = new SolidBrush(Color.Red);

            randNum = new Random();
            picBoxCanvas.BackColor = Color.Black;

        }

        private void picBoxCanvas_Click(object sender, EventArgs e)
        {

        }

        private void btnPlay_Click(object sender, EventArgs e)
        {
            timer1.Interval = 50;
            timer1.Enabled = true;

            x = randNum.Next(1, picBoxCanvas.Height);
            y = randNum.Next(1, picBoxCanvas.Width);
            xChange = randNum.Next(1, 10); yChange = randNum.Next(1, 10);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int brushSize;
            x = x + xChange;
            y = y + yChange;
            if (x >= picBoxCanvas.Width)
                xChange = -xChange;

            if (y >= picBoxCanvas.Height)
                yChange = -yChange;

            if (x <= 0)
                xChange = -xChange;

            if (y <= 0)
                yChange = -yChange;

            canvas.Clear(Color.Black);
            brushSize = 15;
            canvas.FillEllipse(brush,  x, y, brushSize, brushSize);


        }

        private void btnCoin_Click(object sender, EventArgs e)
        {
            int CoinClick;

            CoinClick =  btnPlay_Click(object sender, EventArgs e)

            for (int count = 0; count <= 5; count++)
            {

            }

        }
    }
}

我需要程序运行,所以当按下 coinClick 按钮时开始计数播放按钮,该按钮在 5 次将产生错误(不太在意那个位),努力委派按钮播放任何帮助将不胜感激谢谢你

4

2 回答 2

0

在方法之外声明你的计数器。每次通过单击按钮调用该方法时,它都会添加。检查按钮计数是否为5,然后进行相应处理。

private int clickCount;
private void btnCoin_Click(object sender, EventArgs e)
{
    if (++clickCount >= 5)
    {
        // Throw error
        clickCount = 0; // reset click count?
    }

}
于 2013-11-12T20:01:52.127 回答
0

好的,所以首先您需要将该coin方法用作字段的重置:private

int _playCount;

...

private void btnCoin_Click(object sender, EventArgs e)
{
    _playCount = 5;
}

然后,每次单击播放按钮时,减少该计数:

private void btnPlay_Click(object sender, EventArgs e)
{
    if (_playCount == 0)
    {
        // maybe alert them; maybe disable the button; whatever...
        return;
    }

    _playCount--;
}

或者您可能只想主动禁用该按钮,如下所示:

private void btnPlay_Click(object sender, EventArgs e)
{
    _playCount--;
    btnPlay.Enabled = (_playCount > 0);
}

现在,如果您禁用该按钮,请确保在计数重置处理程序中启用它。

于 2013-11-12T20:06:17.457 回答