I'm stuck in step 6. I have understood how to make the ball bounce on the window's edges but I cannot figure out how to make the ball move in an angle. I assume that it has to be some combination of Y-axis and X-axis movement, but I cannot figure it out. To be more specific, the ball is moving in an angle but I don't think this is the right way. Here's a part of my code:

                    //bouncing up-down
                    move(ball, 0, velocity);
                    if (getY(ball) + getHeight(ball) >= getHeight(window))
                        velocity = -velocity;
                    else if (getY(ball) <= 0 )
                        velocity = -velocity;
                   //bouncing left-right
                    move(ball, velocity, 0);
                    if (getX(ball) + getWidth(ball) >= getWidth(window))
                        velocity = -velocity;
                    else if (getX(ball) <= 0 )
                        velocity = -velocity;

I also don't get why I have to use drand48. Some pseudo code and a little bit of explanation would be really useful!

Here is some of my code.When this block is executed, I am able to move the ball as long as I move the paddle.Else (in the while (true) loop) the ball wont move. while (lives > 0 && bricks > 0) { while (true) {
GEvent event = getNextEvent(MOUSE_EVENT);
if (event != NULL) { if (getEventType(event) == MOUSE_MOVED) { double x = getX(event) - getWidth(paddle) / 2; double y = HEIGHT - PADDLE_HEIGHT - getHeight(ball); if (x < 0) x = 0; else if (x >= (WIDTH - PADDLE_WIDTH)) x = WIDTH - PADDLE_WIDTH; setLocation(paddle, x, y); move( ball, vx, vy ); } }

2 Answers 2


I think you may need a separate velocity variable for each dimension (e.g., Vx and Vy). I think this will clear up the angle problem.

  • I cannot understand where to define the vx and vy speed. I do it outside both while loops, but then the ball moves only when I move the mouse (which means the paddle as well). I can't get the ball to move when there is a mouse click.
    – PanosVl
    Oct 5, 2014 at 21:31
  • I do not think you need two while loops to do this and I suspect that solving your problem may involve getting everything into one while loop. You should probably post more of your code. I do not see any while loops in what is posted. Oct 6, 2014 at 23:43
  • I've edited my post. If you could give some advice I'd appreciate it.I a little stuck here...
    – PanosVl
    Oct 10, 2014 at 5:54

Pseudo code should be something like this:
While (Lives and Blocks are both greater than 0)
If (x<0 or x>WIDTH) then reverse velocity on x-axis
If (y<0 or y>HEIGHT) then reverse velocity on y-axis

If you are having trouble moving the paddle independently of the ball moving then you probably have more than one While Loop and you should only be using one.

Another thing you need to is have the ball moving in both the x and y directions when the program is initiated.

  • So the ball's movement and the bouncing is implemented outside forever loop ? Because both move ball and move paddle should be inside a loop (at least this is what I understood).
    – PanosVl
    Oct 18, 2014 at 21:02
  • I think the only thing that should be outside the while loop is the initial movement - set the ball moving and then enter the While loop where paddle movement and bouncing can be checked for. Oct 19, 2014 at 0:35

You must log in to answer this question.

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