I have breakout mostly working, but I can't figure one issue out. Sometimes, my ball will break through multiple bricks. Instead of hitting one and bouncing back towards the bottom, it rips through a bunch.
// move ball along at angle
move(ball, velocityX, velocityY);
// bounce off right edge of window
if (getX(ball) + getWidth(ball) >= getWidth(window))
{
velocityX = -velocityX;
}
// bounce off left edge of window
else if (getX(ball) <= 0)
{
velocityX = -velocityX;
}
// bounce off top edge of window
else if (getY(ball) <= 0)
{
velocityY = -velocityY;
}
// hits the bottom edge of window
else if (getY(ball) + getHeight(ball) >= (getHeight(window) - getHeight(paddle) - 5))
{
lives = lives - 1;
printf("Lives remaining: %i\n",lives);
setLocation(ball, (WIDTH / 2) - (RADIUS / 2), (HEIGHT / 2) - (RADIUS / 2));
waitForClick();
}
// linger before moving again
pause(10);
// detect ball collision
GObject object = detectCollision(window, ball);
if (object != NULL)
{
// if ball collides with a rectangle
if (strcmp(getType(object), "GRect") == 0)
{
// reverse y velocity if ball collides with paddle
if (object == paddle)
{
velocityY = -velocityY;
}
// remove brick, decrease brick count, increase points count
else
{
velocityY = -velocityY;
removeGWindow(window, object);
points = points + 1;
updateScoreboard(window, label, points);
bricks = bricks - 1;
}
}
}
}