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;
pause(10);`
//bouncing left-right
move(ball, velocity, 0);
if (getX(ball) + getWidth(ball) >= getWidth(window))
velocity = -velocity;
else if (getX(ball) <= 0 )
velocity = -velocity;
pause(10);
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 );
}
}