I am working on a assignment for uni where I need to create a Breakout game in Visual Studio 2010 using C # Win Forms. At the moment, I am concentrating on the fact that there is only one brick that needs to be destroyed, so I have a mechanic before expanding it.
To clarify my current program: I use the image field as a Graphics object and a timer to create an animation effect. A ball can skip between 1 and 10 pixels in each frame - this is part of creating a random starting vector for the ball.
This works great until it comes to checking whether the ball hit the βbrickβ that I drew. I have an if statement that checks if the ball is in any of the coordinates on the image field corresponding to the brick outline. I know that logic is fine, because it has been working for a while. However, due to the change in the βjumpβ of the ballβs position, I need to add a buffer area of ββ+/- 5 pixels to the if if statement.
This raises a problem because my if (two, really) is really complicated as it is:
// Checks if ball hits left side or top of brick if (((x >= brickX) && (x <= (brickX + 50)) && (y == brickY)) || ((y >= brickY) && (y <= (brickY + 20)) && (x == brickX))) { brickHit = true; } // Check if ball hits right side or bottom of brick else if ((((x >= brickX) && (x <= brickX + 50)) && (y == (brickY + 20))) || (((y >= brickY) && (y <= brickY + 20)) && (x == brickX + 50))) { brickHit = true; }
To clarify: x and y are the coordinates of the ball, and brickX and brickY are the coordinates of the upper left corner of the rectangle brick (width 50 pixels, height 10 pixels).
Is there any way to simplify the above if statements? If I can make them simpler, I know that it will be much easier to add in the buffer (these are just 5 pixels on both sides of the brick 'outline to reposition the ball in position).
If further clarification is required, please ask - I am writing this question at 5:12, so I know that it can be a little obscure.
source share