I’m having some problem with the movement and collisions of the ball. I’ve searched for answers here but I still cannot figure out how to do what they suggested.
The ball gets stuck in the paddle when the paddle hits the side of the ball. Someone suggested that I should do:
if (ball moving down) { bounce ball up }
I did try with velocity,
getY(ball)
,gety(paddle)
with many different combinations, but either the ball still gets stuck or the paddle disappears when the ball hits it.The ball does pause sometimes when it hits the bottom edge of the screen, waits for my click, then starts over again.
However, if I move the paddle (without any clicks, I’m sure), whilst the ball is going down in the gap between the paddle and the bottom edge, then, the ball just starts over and falls right away,
waitForClick
doesn't work in that case.Also, I cannot decrease lives. I’ve tried
lives = lives - 1
, andlives--
when ball hits the bottom, but I still can play more than 3 times.lives
is set with 3, and I haven’t changed anything about it.
Any suggestion will be much appreciated!
Thank you so much for your time!
Update: 08/04/2015 Please help. Since I posted the question here, I've been attempting to fix the problems but I'm still stuck.
Update: 03/04/2015 Thank you so much for your answers, guys! Here's my original code, it complies and runs, I've removed parts that don't work but it still has the problems mentioned above:
// click to start game
waitForClick();
// instantiate balls' velocities
double velocityX = drand48() + 0.5, velocityY = drand48() + 2.5;
// START PADDLE MOVE ----------------------------------
// paddle follows mouse forever
while (true)
{
// 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 middle of slider follows cursor
double x = getX(event) - getWidth(paddle) / 2;
double y = 540;
setLocation(paddle, x, y);
// stop at right edge of window
if (getX(paddle) + getWidth(paddle) >= getWidth(window))
{
x = getWidth(window) - getWidth(paddle);
setLocation(paddle, x, y);
}
// stop at left edge of window
else if (getX(paddle) <= 0)
{
x = 0;
setLocation(paddle, x, y);
}
}
}
// END PADDLE MOVE -------------------------------------
// START BALL MOVE ----------------------------------------
// move ball
move(ball, velocityX, velocityY);
// bounce off right edge of window
if (getX(ball) + getWidth(ball) >= getWidth(window))
{
velocityX = - velocityX;
}
// bounce off left edge of window
else if (getX(ball) <= 0)
{
velocityX = - velocityX;
}
// bounce off top edge of window
else if (getY(ball) <= 0)
{
velocityY = - velocityY;
}
// hits the bottom edge of window
else if (getY(ball) + getHeight(ball) >= getHeight(window))
{
waitForClick();
setLocation(ball, 190, 290);
}
// linger before moving again
pause(10);
// END BALL MOVE --------------------------------
// DETECT COLLISION -----------------------------------
GObject object = detectCollision(window, ball);
if (object != NULL)
{
// ball hits paddle
if (object == paddle)
{
velocityY = - velocityY;
}
// ball hits bricks
else if (strcmp(getType(object), "GRect") == 0)
{
velocityY = - velocityY;
GRect bricks = object;
removeGWindow(window, bricks);
}
}
// END DETECT COLLISION -------------------------------------
}
return 0;
// END TODO