I have been having serious issues with this particular project. Somehow I followed all the instructions in the bounce video and I have written all my codes but my ball is not bouncing off ANYTHING T____T... can someone please help tell me what I'm doing wrong?
while (lives > 0 && bricks > 0)
{
// TODO
while (true)
{
GEvent event = getNextEvent(MOUSE_EVENT);
if (event != NULL)
{
if (getEventType(event) == MOUSE_MOVED)
{
double x = getX(event) - getWidth(paddle);
double y = getY(event) - getWidth(paddle);
setLocation(paddle,x,530);
}
}
double x_velocity = 2.0;
double y_velocity = 1.5;
move(ball, x_velocity,y_velocity);
pause(10);
if (getX(ball) + getWidth(ball) >= getWidth(window))
{
x_velocity = -x_velocity;
}
else if (getX(ball) <=0)
{
x_velocity = -x_velocity;
}
else if (getY(ball) <=0)
{
y_velocity = -y_velocity;
}
GObject object = detectCollision(window,ball);
if(object != NULL)
{
if (object == paddle)
{
y_velocity= -y_velocity;
}
if (strcmp(getType(object),"GRect") ==0)
{
y_velocity= -y_velocity;
bricks--;
}
}
}
}
// wait for click before exiting
waitForClick();
// game over
closeGWindow(window);
return 0;
}
/**
* Initializes window with a grid of bricks.
*/
void initBricks(GWindow window)
{
// TODO
int setRectx = 5;
int setRecty = 37;
for ( int i = 0; i < ROWS; i++)
{
for ( int n = 0; n < COLS; n++)
{
GRect bricks = newGRect(setRectx,setRecty,36,10);
setColor(bricks,"BLUE");
setFilled(bricks,true);
setRectx = setRectx + 38;
add (window, bricks);
}
setRectx = 5;
setRecty = setRecty + 20;
}
}
/**
* Instantiates ball in center of window. Returns ball.
*/
GOval initBall(GWindow window)
{
// TODO
GOval ball = newGOval(200,300,20,20);
add (window, ball);
return ball;
}
/**
* Instantiates paddle in bottom-middle of window.
*/
GRect initPaddle(GWindow window)
{
// TODO
GRect paddle = newGRect(200,530,36,10);
setColor(paddle,"BLACK");
setFilled(paddle,true);
add(window, paddle);
return paddle;
}
/**
* Instantiates, configures, and returns label for scoreboard.
*/
GLabel initScoreboard(GWindow window)
{
// TODO
return NULL;
}
/**
* Updates scoreboard's label, keeping it centered in window.
*/
void updateScoreboard(GWindow window, GLabel label, int points)
{
// update label
char s[12];
sprintf(s, "%i", points);
setLabel(label, s);
// center label in window
double x = (getWidth(window) - getWidth(label)) / 2;
double y = (getHeight(window) - getHeight(label)) / 2;
setLocation(label, x, y);
}
/**
* Detects whether ball has collided with some object in window
* by checking the four corners of its bounding box (which are
* outside the ball's GOval, and so the ball can't collide with
* itself). Returns object if so, else NULL.
*/
GObject detectCollision(GWindow window, GOval ball)
{
// ball's location
double x = getX(ball);
double y = getY(ball);
// for checking for collisions
GObject object;
// check for collision at ball's top-left corner
object = getGObjectAt(window, x, y);
if (object != NULL)
{
return object;
}
// check for collision at ball's top-right corner
object = getGObjectAt(window, x + 2 * RADIUS, y);
if (object != NULL)
{
return object;
}
// check for collision at ball's bottom-left corner
object = getGObjectAt(window, x, y + 2 * RADIUS);
if (object != NULL)
{
return object;
}
// check for collision at ball's bottom-right corner
object = getGObjectAt(window, x + 2 * RADIUS, y + 2 * RADIUS);
if (object != NULL)
{
return object;
}
// no collision
return NULL;