Home > Blockchain >  Winforms app doesn't respond to click events after disabling and then enabling timer
Winforms app doesn't respond to click events after disabling and then enabling timer

Time:01-06

I was making game like brick breaker in C# winforms and I wanted to implement restart button, when I click restart everything works fine(ball moves and bricks break) except board which doesn't move even though I'm clicking left and right buttons.

This is code the code where you lose if you hit the ground:

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

This is restart button logic:

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

The code for the button events:

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

here is whole code:

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

I hope my code is clear.

I have tried debugging the code and it actually goes into the button click methods but somehow the board doesn't move and I don't know what I'm missing.

CodePudding user response:

Actually problem was not the timer but the Restart button, when I set it's visibility to false it disappeared but it actually blocked Winforms from reacting to click events, after I set visibility and also Restart.Enabled to false then everything worked perfectly.

  • Related