Winforms应用程序在禁用然后启用计时器后不响应点击事件

4sup72z8  于 2023-01-09  发布在  其他
关注(0)|答案(1)|浏览(129)

我在C# winforms中制作了一个类似砖块破碎机的游戏,我想实现重新启动按钮,当我点击重新启动时,一切都很好(球移动和砖块破碎),除了板不移动,即使我点击左右按钮。
这是代码的代码,你输了,如果你撞到地面:

if(health < 1)
{
  winStatus.Text = "YOU HAVE LOST";
  Restart.Visible = true;
  timer1.Stop();
}

这是重启按钮逻辑:

private void Restart_Click(object sender, EventArgs e) //restart button
{
  health = 3;
  Health.Text = health.ToString();
  ball.Left = 402;
  ball.Top = 364;
  moveX = 4;
  moveY = 6;
  goUp = true;
  Restart.Visible = false;
  winStatus.Text = "";
  Board.Left = 342;
  Board.Top = 407;
  timer1.Start();
}

按钮事件的代码:

private void keyisdown(object sender, KeyEventArgs e)
{
  if (e.KeyCode == Keys.Left)
  {
    moveLeft = true;
  }
  if (e.KeyCode == Keys.Right)
  {
    moveRight = true;
  }
}

private void keyisup(object sender, KeyEventArgs e)
{
  if (e.KeyCode == Keys.Left)
  {
    moveLeft = false;
  }
  if (e.KeyCode == Keys.Right)
  {
    moveRight = false;
  }
}

下面是完整代码:

using System;
using System.Drawing;
using System.Drawing.Configuration;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;

namespace BrickBreaker
{
    public partial class Form1 : Form
    {
        private int level = 1;
        private int health = 3;

        private int moveX = 10;
        private int moveY = 10;
        private bool goRight = true;
        private bool goUp = true;

        bool moveRight, moveLeft;

        int speed = 30;

        int brickCount = 14;

        public Form1()
        {
            InitializeComponent();

            this.SetStyle(
                System.Windows.Forms.ControlStyles.UserPaint |
                System.Windows.Forms.ControlStyles.AllPaintingInWmPaint |
                System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer,
                true);
            DoubleBuffered = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (ball.Right > Width - 20)
            {
                goRight = false;
            }
            else if (ball.Left < 10)
            {
                goRight = true;
            }

            if (ball.Top < 10)
            {
                goUp = false;
            }
            else if (ball.Bottom > Height - 40)  //hitting ground
            {
                health--;
                if(health < 1)
                {
                    winStatus.Text = "YOU HAVE LOST";
                    Restart.Visible = true;
                    timer1.Stop();
                }
                else
                {
                    Thread.Sleep(2000);
                    Health.Text = health.ToString();
                    ball.Left = 402;
                    ball.Top = 364;
                    moveX = 4;
                    moveY = 6;
                    goUp = true;
                   
                }
            }
            if (goRight)
            {
                ball.Left += moveX;
            }
            else
            {
                ball.Left -= moveX;
            }
            if (goUp)
            {
                ball.Top -= moveY;
            }
            else
            {
                ball.Top += moveY;
            }
            if (moveLeft && Board.Left > 20)
            {
                Board.Left -= speed;
            }
            if (moveRight && Board.Right < Width - 30)
            {
                Board.Left += speed;
            }
            foreach (Control x in this.Controls)
            {
                if (x is PictureBox && (string)x.Tag == "board") //touches board
                {
                    if (ball.Bounds.IntersectsWith(x.Bounds))
                    {
                        if (ball.Bounds.Left - x.Bounds.Left < 40)
                        {
                            goRight = false;
                            moveX += 4;
                            moveY -= 1;
                        }
                        else if (ball.Bounds.Left - x.Bounds.Left >= 40 && ball.Bounds.Left - x.Bounds.Left < 72)
                        {
                            goRight = true;
                        }
                        else if (ball.Bounds.Left >= 72 && ball.Bounds.Left < 114)
                        {
                            moveY -= 4;
                            moveX -= 6;
                        }
                        else
                        {
                            goRight = true;
                            moveX = +4;
                            moveY -= 1;
                        }

                        goUp = true;
                    }
                }
                if (x is PictureBox && (string)x.Tag == "brick") //touches brick
                {
                    if (ball.Bounds.IntersectsWith(x.Bounds))
                    {
                        if (ball.Bottom <= x.Bottom)  //touches top side of brick
                        {
                            goUp = true;
                        }
                        else if (ball.Bottom > x.Bottom) //touches bot side of brick
                        {
                            moveY += 2;
                            goUp = false;
                        }
                        else
                        {
                            goUp = false;
                        }
                        Controls.Remove(x);
                        brickCount--;
                    }
                }

                if (brickCount == 0 && timer1.Enabled)
                {
                    winStatus.Text = "YOU HAVE WON";
                    level+=1;
                    LevelNumber.Text = level.ToString();
                    timer1.Stop();
                }
            }
        }

        private void keyisdown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left)
            {
                moveLeft = true;
            }
            if (e.KeyCode == Keys.Right)
            {
                moveRight = true;
            }
        }

        private void keyisup(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left)
            {
                moveLeft = false;
            }
            if (e.KeyCode == Keys.Right)
            {
                moveRight = false;
            }
        }

        private void Restart_Click(object sender, EventArgs e) //restart button
        {
            health = 3;
            Health.Text = health.ToString();
            ball.Left = 402;
            ball.Top = 364;
            moveX = 4;
            moveY = 6;
            goUp = true;
            Restart.Visible = false;
            winStatus.Text = "";
            Board.Left = 342;
            Board.Top = 407;
            timer1.Start();
        }
    }
}

我希望我的准则是清楚的。
我试着调试代码,它实际上进入了按钮单击方法,但不知何故,董事会不动,我不知道我错过了什么。

ctehm74n

ctehm74n1#

实际上问题不是计时器而是重启按钮,当我设置它的可见性为false时,它消失了,但它实际上阻止了Winforms对点击事件的React,在我设置了可见性和重启之后。

相关问题