I'm working through pset3 and noticed that when I declare my ball's velocities outside the while loop, my collisions against the wall's edges work, i.e. the ball bounces away, but when the velocities are declared within the while loop, my ball just floats right past the end of the window. Can anyone help me understand why this is the case since I think I'm getting confused with variable scope here..
// velocities declared outside while loop
//ball bounces as expected
int main (void)
{....
double x_velocity = drand48();
double y_velocity = 0.2;
while (lives > 0 && bricks > 0)
{
GEvent event = getNextEvent(MOUSE_EVENT);
....
move(ball, x_velocity, y_velocity);
if(getX(ball) + (2*RADIUS) >= WIDTH)
{
x_velocity = -x_velocity;
}
vs
// velocities declared in while loop
//ball goes past window's edge
int main (void)
{....
while (lives > 0 && bricks > 0)
{
GEvent event = getNextEvent(MOUSE_EVENT);
....
double x_velocity = drand48();
double y_velocity = 0.2;
move(ball, x_velocity, y_velocity);
if(getX(ball) + (2*RADIUS) >= WIDTH)
{
x_velocity = -x_velocity;
}