Ball to brick collision in c#

402 views Asked by At

I am writing a program that has a ball moving around a PictureBox and I have a set of bricks at the top of the PictureBox.

I have used an array for location of the bricks that I will be using:

int[,] brickLocation = { { 0, 0 }, { 30, 0 } }

I have used the DrawRectangle to construct each brick within the picturebox. This exist within timer1_Tick event. As the ball is drawn, then the picturebox is cleared and the x and y coordinate change.

When the ball "collides" with the brick, I want some way in which the brick will disappear because there will be another brick behind it.

[brick0]

[brick1]

At the moment, my code for the collision between the ball and brick1 is:

if ((yBall > brickY) && (yBall < brickY + 25) && (xBall > brickX) && (xBall < brickX + 80))
            {
                yBallChange = -yBallChange;
            }

where xBall, yBall is the x and y coordinate of the ball and brickX and brickY are the coordinate for the bricks.

So when the ball coordinates are within the bounds of brick1 the y-direction will change.

Is there some way in which I can redraw [brick1] in a new position with no size as to eliminate it so there can then be a collision between the ball and [brick0]? Or is there something else I could do?

0

There are 0 answers