0
\$\begingroup\$

I am creating a breakout game in c#. Everything is working fine excepting one feature
My blocks are destroyed after the second hit ,but if the ball hits the side (left or right) of the block , the block is destroyed instantly..

Example here

What's the feature needed to avoid this problem ? Thanks in advance ! Here is the code where the "action" is happening :

foreach(Control x in this.Controls)
        {

            if (x is PictureBox && x.Tag == "block")
            {
                if (ball1.Bounds.IntersectsWith(x.Bounds))
                {        
                    x.Tag = "Hit";
                    bally = -bally;
                }
            }
            else
            {
                if (x is PictureBox && x.Tag == "Hit")
                    if (ball1.Bounds.IntersectsWith(x.Bounds))
                    {
                       bally = -bally;
                        this.Controls.Remove(x);
                        if(score<=5)
                            speed = speed + 1;
                    }
            }
        }

x - my blocks

Point pt = ball1.Location;
if (pt.X < 0 || pt.X > ClientRectangle.Width - ball1.Width)
            ballx = -ballx;
\$\endgroup\$
  • 1
    \$\begingroup\$ Maybe I'm missing something but I don't see where your code for bouncing the ball in the x direction is? I only see bally = -bally; where is the reflection for the ball's x movement on a side-block collision (presumably ballx = -ballx;)? \$\endgroup\$ – Jack Aidley Apr 7 '19 at 12:01
  • \$\begingroup\$ I edited my post. That's my only reflection on a side collision. Should I add one more case ? What should the condition be ? \$\endgroup\$ – Alin00 Apr 7 '19 at 12:08
  • 1
    \$\begingroup\$ That code appears only to account for edge collision with the frame of the window, not with collision with the side of any block? \$\endgroup\$ – Jack Aidley Apr 7 '19 at 12:18
  • \$\begingroup\$ And how do I check the collision with a side of a block ? \$\endgroup\$ – Alin00 Apr 7 '19 at 12:29
  • \$\begingroup\$ Given that you're not checking which side of the block you're colliding with, I think your question is now a duplicate of this one: gamedev.stackexchange.com/questions/22609/… (although that's in Java, it makes no material difference) \$\endgroup\$ – Jack Aidley Apr 7 '19 at 16:04

Your Answer

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

Browse other questions tagged or ask your own question.