0

I am trying to create the while lives > 0 && bricks > 0 loop but either the ball doesn't bounce off the right wall or the paddle doesn't follow the mouse. My code is below. Please help been stuck on this for hours and any other tips on my code are always appreciated.

// keep playing until game over
while (lives > 0 && bricks > 0)
{

    // check for mouse event
    GEvent event = getNextEvent(MOUSE_EVENT);

    // if we heard one
    if (event != NULL)
    {
        // if the event was movement
        if (getEventType(event) == MOUSE_MOVED)
        {
            // ensure circle follows top cursor
            double x = getX(event);

            setLocation(paddle, x, 535);
        }
    }


  // initial velocity
double velocity = 2.0;

// bounce forever

    // move circle along x-axis

     move(ball, velocity, 0);

    // bounce off right edge of window
    if (getX(ball) + getWidth(ball) >= WIDTH)
    {
        velocity = -velocity;
    }

    // bounce off left edge of window
    else if (getX(ball) <= 0)
    {
        velocity = -velocity;
    }

    // linger before moving again
    pause(10);
}
4
  • No answer, unfortunately, but I'm having the exact same problem (using the bounce code but having the ball not bounce). Have you tried printf statements inside your if/else if to make sure it's actually recognizing the collision? I did that and mine were (it was actually getting stuck in an infinite loop there), but just something to check out. Update me if you try anything that works, and I'll let you know if I find a solution May 31, 2015 at 0:34
  • it bounces off the wall if the code for the paddle is not in the while loop. Guessing I have the while loop setup incorrectly but I can't figure out what it is.
    – JV305
    May 31, 2015 at 15:25
  • Do you mean just setLocation? Or the whole detect code segment? That is weird, I don't know why that would be. May 31, 2015 at 16:05
  • ETA: for me deleting the paddle code (the whole block of code above the section that moves the ball) did nothing, the code still functions the same way (the ball doesn't bounce, just disappears off the edge of the window) May 31, 2015 at 16:10

1 Answer 1

2

Try initializing your velocity outside the loop. Right now you are reinitializing it every iteration of the loop, so your velocity is always 2.0, and is never changing.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .