Something is going on here that I don't understand. The ball disappears when it hits a wall. The live counter seems to be working because the game is done after 3 tries even though the ball is not disappearing off the bottom of the window. Also below this section of code is the code to move the paddle, which is also working.

while (lives > 0 && bricks > 0)

        double xVelocity = drand48();
        double yVelocity = drand48();
        move(ball, xVelocity, yVelocity);

        if ((getX(ball) <= 0) || (getX(ball) + getWidth(ball) >= getWidth(window)))
            xVelocity *= -1;

        if (getY(ball)  <= 0) 
            yVelocity *= -1;

        if (getY(ball) + getHeight(ball) >= getHeight(window))
            lives -= 1;
            setLocation(ball, WIDTH / 2, HEIGHT / 2);


1 Answer 1


I'll bet that your ball is moving somewhat erratically too. The problem lies in the first two lines of code inside the while loop. You have declared the velocity vars inside the loop, so every time it loops, it reinitializes the vars to a new random number (assuming a new seed). It doesn't matter what changes you make to the vars after detecting a hit to the wall, the changes are abandoned when the vars reinitialize.

Move the two declarations to just before the start of the while loop.

If this answers your question, please click on the check mark to accept this answer. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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