I'm trying to get the ball to collide with all of the walls of the window. My current code works for the first 3 seconds of gameplay but then the ball will jiggle through a wall. I'll show you the code I currently have and any help in the right direction would be greatly appreciated.
// balls movement
#define b_move drand48()
// balls y velocity
double y_velocity = 2.0;
// balls x velocity
double x_velocity = 2.0;
int main(void)
while(lives > 0 && bricks > 0)
{
// ball movement up and down
move(ball, b_move, y_velocity);
// collision for top and bottom of window
if (getY(ball) + getHeight(ball) >= getHeight(window))
{
y_velocity = -y_velocity;
}
else if (getY(ball) <= 0)
{
y_velocity = -y_velocity;
}
// ball movement left and right
move(ball, x_velocity, b_move);
//collision for left and right of window
if (getX(ball) + getWidth(ball) >= getWidth(window))
{
x_velocity = -x_velocity;
}
else if (getX(ball) <= 0)
{
x_velocity = -x_velocity;
}
pause(10);
}
I just can't see what I need to change :(