I have been going through the project and I am kind of stuck in one step. When ever my ball bounces and breaks a brick, the scoreboard somehow doesn't add. I have gone through my code a few times and everything seems to make sense to me, but it's just not functioning. I think the error might be do to the ordering of my code but I just can't find the error. Can someone please help and see if they can spot anything wrong?? Thanks!!!!!
int main(void)
{
// seed pseudorandom number generator
srand48(time(NULL));
// instantiate window
GWindow window = newGWindow(WIDTH, HEIGHT);
// instantiate bricks
initBricks(window);
// instantiate ball, centered in middle of window
GOval ball = initBall(window);
// instantiate paddle, centered at bottom of window
GRect paddle = initPaddle(window);
// instantiate scoreboard, centered in middle of window, just above ball
GLabel label = initScoreboard(window);
// number of bricks initially
int bricks = COLS * ROWS;
// number of lives initially
int lives = LIVES;
// number of points initially
int points = 0;
// keep playing until game over
double x_velocity = 1.5;
double y_velocity = 1.0;
while (lives > 0 && bricks > 0)
{
// TODO
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);
}
}
move(ball, x_velocity,y_velocity);
pause(10);
if (getX(ball) + getWidth(ball) >= getWidth(window))
{
x_velocity = -x_velocity;
}
if (getX(ball) <=0)
{
x_velocity = -x_velocity;
}
if (getY(ball) <=0)
{
y_velocity = -y_velocity;
}
if (getY(ball) >= 600)
{
lives--;
setLocation(ball,200,300);
}
GObject object = detectCollision(window,ball);
if(object != NULL)
{
if (object == paddle)
{
y_velocity= -y_velocity;
}
else if (strcmp(getType(object),"GRect") ==0)
{
removeGWindow(window, object);
y_velocity= -y_velocity;
points++;
bricks--;
}
}
}
// wait for click before exiting
waitForClick();
// game over
closeGWindow(window);
printf ("Please try again!");
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
GLabel label = newGLabel("0");
setFont (label, "SansSerif-36");
setColor(label,"BLACK");
setFilled(label,true);
setLocation(label, 200, 300);
add(window, label);
return label;
}
/**
* Updates scoreboard's label, keeping it centered in window.
*/
void updateScoreboard(GWindow window, GLabel label, int points)
{
// update label
points = 0;
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;
}