0
\$\begingroup\$

this game isn't completed yet, i've created the part where the ball hits the board, but there's problem in playing and that's the ball stucking in the board. For a better comprehension and understaning i've attached a image down below , plz watch it and lemme know of your take on this bug.

    public partial class PingPong : Form
{
    public PingPong()
    {
        InitializeComponent();
        timer1.Enabled = true;
        timer1.Interval = 30;
    }


    int mx = 5;
    int my = 5;
    private void timer1_Tick(object sender, EventArgs e)
    {

        if (ball.Bounds.IntersectsWith(boardCenter.Bounds))
        {
            mx = mx * -1;
            my = my * 1;
        }
        else if (ball.Location.Y >= this.ClientSize.Height - ball.Height)
        {
            mx = mx * 1;
            my = my * -1;
        }
        else if (ball.Location.Y <= 0)
        {
            mx = mx *1;
            my = my * -1;
        }

        else if (ball.Location.X >= this.ClientSize.Width - ball.Width)
        {
            mx = mx * -1;
            my = my * 1;
        }


        ball.Location = new Point(ball.Location.X + mx, ball.Location.Y + my);

    }

    private void PingPong_KeyDown(object sender, KeyEventArgs e)
    {

        if (e.KeyCode == Keys.Up)
        {
            boardCenter.Location = new Point(boardCenter.Location.X, boardCenter.Location.Y - 4);

        }
        else if (e.KeyCode == Keys.Down)
        {
            boardCenter.Location = new Point(boardCenter.Location.X, boardCenter.Location.Y + 4);
        }

    }

}

enter image description here

\$\endgroup\$
  • \$\begingroup\$ For a better comprehension and understaning i've I put a video youtu.be/WSZxw42qR-E \$\endgroup\$ – similar Apr 15 '16 at 8:53
1
\$\begingroup\$

I've had this exact same issue back when I made a version of PingPong.

Here's a walkthrough of your logic, to illustrate what it's actually doing. I'll point out the flaw afterwards, though you'll probably see it:

  • Move on the X and Y axis
  • Hits the paddle? Change X direction!!!
    • Move again!
  • Hits the paddle? Change X direction!!!
    • Move again!
  • Hits the paddle? Change X direction!!!
    • Move again!

Essentially, every frame it will change direction, if it keeps intersecting with the paddles Bounds. Which it will, if all you do it change the direction when you hit the paddle. You also need to move it out of the paddles Bounds.

Your best solution is to place it at the paddles Right side (Ball.X must be equal to Paddle.X + Paddle.Width (and maybe +1)). That way, no matter where you hit, it will always start out at somewhere along the right side of the paddle. Which will prevent something like this from being able to happen.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ This problem exists in the form (pingpong form) of the game,In fact, the only problem with not paddle Check out this video as well.youtu.be/hGiUTOufUWk \$\endgroup\$ – similar Apr 15 '16 at 11:01
  • \$\begingroup\$ Well I can tell from your code that this is definitely an issue. Whether it's what actually causes the behavior you're seeing, is a different story. \$\endgroup\$ – Falgantil Apr 15 '16 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.