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;
// ball velocity
double x_velocity = drand48() - 0.2;
double y_velocity = 0.2;
// keep playing until game over
while (lives > 0 && bricks > 0)
{
// mouse movement
GEvent event = getNextEvent(MOUSE_EVENT);
// if we heard an event
if(event != NULL)
{
//if the event was movement
if(getEventType(event) == MOUSE_MOVED)
{
//make paddle follow mouse
double x = getX(event) - getWidth(paddle);
double y = getY(paddle);
setLocation(paddle, x, y);
}
}
// move the ball
move(ball, x_velocity, y_velocity);
// make sure the ball bounces off walls
// left wall
if(getX(ball) < 0)
{
x_velocity = -x_velocity;
}
//right wall
else if(getX(ball) + BALL_WIDTH > WIDTH)
{
x_velocity = -x_velocity;
}
// top edge
else if(getY(ball) < 0)
{
y_velocity = -y_velocity;
}
// bottom edge
else if(getY(ball) > HEIGHT)
{
lives--;
setLocation(ball, getWidth(window)/2 - BALL_WIDTH, getHeight(window)/2);
setLocation(paddle, PADDLE_X, PADDLE_Y);
waitForClick();
}
// paddle and brick collisions
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--;
}
}
}
if(bricks == 0 && lives > 0)
{
//create new GLabel --> you win!
pause(3);
GLabel game_over = newGLabel("YOU WIN!");
setFont(game_over, "Verdana-60");
setColor(game_over, "RED");
setLocation(game_over, 45, 300);
add(window, game_over);
}
if(lives == 0)
{
GLabel game_over = newGLabel("YOU LOSE!");
setFont(game_over, "Verdana-60");
setColor(game_over, "RED");
setLocation(game_over, 40, 300);
add(window, game_over);
}
// wait for click before exiting
waitForClick();
// game over
closeGWindow(window);
return 0;
}
}
}
/**
* InstantiatiesInitializes window with a grid of bricks.
*/
void initBricks(GWindow window)
{
// TODO
//for each row
int starting_y = SPACE;
for(int i = 0; i < ROWS; i++)
{
int starting_x = SPACE;
//each column
for(int j = 0; j < COLS; j++)
{
// draw a brick
GRect brick = newGRect(starting_x, starting_y, BRICK_WIDTH, BRICK_HEIGHT);
add(window, brick);
setFilled(brick, true);
setColor(brick, "BLUE");
starting_x = starting_x + BRICK_WIDTH + SPACE;
}
starting_y = starting_y + BRICK_HEIGHT + SPACE;
}
}
/**
* Instantiates ball in center of window. Returns ball.
*/
GOval initBall(GWindow window)
{
GOval ball = newGOval(BALL_X, BALL_Y, BALL_WIDTH, BALL_HEIGHT);
add(window, ball);
setFilled(ball, true);
setColor(ball, "BLACK");
return ball;
}
/**
* Instantiates paddle in bottom-middle of window.
*/
GRect initPaddle(GWindow window)
{
// TODO
GRect paddle = newGRect(PADDLE_X, PADDLE_Y, PADDLE_WIDTH, PADDLE_HEIGHT);
add(window, paddle);
setFilled(paddle, true);
setColor(paddle, "RED");
return paddle;
}
/**
* Instantiates, configures, and returns label for scorebaordscoreboard.
*/
GblabelGLabel initScoreboard(GWindow, window)
{
int label_x = (WIDTH / 2);
int label_y = (HEIGHT / 2);
GLabel label = newGLabel(" ""0");
setFont(label, "Verdana-50");
setColor(label, "C0C0C0");
add(window, label);
setLocation(label, label_x, label_y );
return label;
}
/**
* 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""%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;
}
/**
* Instantiaties, configures, and returns label for scorebaord.
*/
Gblabel initScoreboard(GWindow, window)
{
GLabel label = newGLabel(" ");
setFont(label, "Verdana-50");
setColor(label, "C0C0C0");
add(window, label);
return label;
}
/**
* 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);
}
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;
// ball velocity
double x_velocity = drand48() - 0.2;
double y_velocity = 0.2;
// keep playing until game over
while (lives > 0 && bricks > 0)
{
// mouse movement
GEvent event = getNextEvent(MOUSE_EVENT);
// if we heard an event
if(event != NULL)
{
//if the event was movement
if(getEventType(event) == MOUSE_MOVED)
{
//make paddle follow mouse
double x = getX(event) - getWidth(paddle);
double y = getY(paddle);
setLocation(paddle, x, y);
}
}
// move the ball
move(ball, x_velocity, y_velocity);
// make sure the ball bounces off walls
// left wall
if(getX(ball) < 0)
{
x_velocity = -x_velocity;
}
//right wall
else if(getX(ball) + BALL_WIDTH > WIDTH)
{
x_velocity = -x_velocity;
}
// top edge
else if(getY(ball) < 0)
{
y_velocity = -y_velocity;
}
// bottom edge
else if(getY(ball) > HEIGHT)
{
lives--;
setLocation(ball, getWidth(window)/2 - BALL_WIDTH, getHeight(window)/2);
setLocation(paddle, PADDLE_X, PADDLE_Y);
waitForClick();
}
// paddle and brick collisions
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--;
}
}
}
if(bricks == 0 && lives > 0)
{
//create new GLabel --> you win!
pause(3);
GLabel game_over = newGLabel("YOU WIN!");
setFont(game_over, "Verdana-60");
setColor(game_over, "RED");
setLocation(game_over, 45, 300);
add(window, game_over);
}
if(lives == 0)
{
GLabel game_over = newGLabel("YOU LOSE!");
setFont(game_over, "Verdana-60");
setColor(game_over, "RED");
setLocation(game_over, 40, 300);
add(window, game_over);
}
// wait for click before exiting
waitForClick();
// game over
closeGWindow(window);
return 0;
}
}
}
/**
* Initializes window with a grid of bricks.
*/
void initBricks(GWindow window)
{
// TODO
//for each row
int starting_y = SPACE;
for(int i = 0; i < ROWS; i++)
{
int starting_x = SPACE;
//each column
for(int j = 0; j < COLS; j++)
{
// draw a brick
GRect brick = newGRect(starting_x, starting_y, BRICK_WIDTH, BRICK_HEIGHT);
add(window, brick);
setFilled(brick, true);
setColor(brick, "BLUE");
starting_x = starting_x + BRICK_WIDTH + SPACE;
}
starting_y = starting_y + BRICK_HEIGHT + SPACE;
}
}
/**
* Instantiates ball in center of window. Returns ball.
*/
GOval initBall(GWindow window)
{
GOval ball = newGOval(BALL_X, BALL_Y, BALL_WIDTH, BALL_HEIGHT);
add(window, ball);
setFilled(ball, true);
setColor(ball, "BLACK");
return ball;
}
/**
* Instantiates paddle in bottom-middle of window.
*/
GRect initPaddle(GWindow window)
{
// TODO
GRect paddle = newGRect(PADDLE_X, PADDLE_Y, PADDLE_WIDTH, PADDLE_HEIGHT);
add(window, paddle);
setFilled(paddle, true);
setColor(paddle, "RED");
return paddle;
}
/**
* Instantiates, configures, and returns label for scoreboard.
*/
GLabel initScoreboard(GWindow window)
{
int label_x = (WIDTH / 2);
int label_y = (HEIGHT / 2);
GLabel label = newGLabel("0");
setFont(label, "Verdana-50");
setColor(label, "C0C0C0");
add(window, label);
setLocation(label, label_x, label_y );
return label;
}
/**
* 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;
}
Pset3 (breakout) -- problems initiating score label
Below is the code I'm using to initiate the label showing the score. Unfortunately, the label doesn't appear on the screen, and I'm clueless as to why that is the case. I'd very much appreciate any advice on how to tackle this problem :)
/**
* Instantiaties, configures, and returns label for scorebaord.
*/
Gblabel initScoreboard(GWindow, window)
{
GLabel label = newGLabel(" ");
setFont(label, "Verdana-50");
setColor(label, "C0C0C0");
add(window, label);
return label;
}
/**
* 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);
}
default