Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // breakout.c
- //
- // Computer Science 50
- // Problem Set 3
- //
- // standard libraries
- #define _XOPEN_SOURCE
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- //My additions
- //#include <algorithm>
- // Stanford Portable Library
- #include <spl/gevents.h>
- #include <spl/gobjects.h>
- #include <spl/gwindow.h>
- // height and width of game's window in pixels
- #define HEIGHT 600
- #define WIDTH 400
- // number of rows of bricks
- #define ROWS 5
- // number of columns of bricks
- #define COLS 10
- // radius of ball in pixels
- #define RADIUS 10
- // lives
- #define LIVES 3
- // globals
- int paddle_width = 85;
- int paddle_height = 15;
- int elevation = 60;
- int ball_diameter = 20;
- int current_score = 0;
- // prototypes
- void initBricks(GWindow window);
- GOval initBall(GWindow window);
- GRect initPaddle(GWindow window);
- GLabel initScoreboard(GWindow window);
- void updateScoreboard(GWindow window, GLabel label, int points);
- GObject detectCollision(GWindow window, GOval ball);
- 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;
- //Velocity Constants
- double ball_xv = drand48();
- double ball_yv = .5;//Would like to add a function to avoid y-vel values that are too small
- // keep playing until game over
- while (true)//lives > 0 && bricks > 0)
- {
- // TODO
- GEvent event = getNextEvent(MOUSE_EVENT);
- if(event != NULL){
- if(getEventType(event) == MOUSE_MOVED){
- double paddle_x;
- if(getX(event)>WIDTH-paddle_width){//sets horizontal position of paddle to not let it's edges go outside of the window
- paddle_x = WIDTH-paddle_width;
- }else if(getX(event)<paddle_width/2){
- paddle_x = 0;
- }else{
- paddle_x = getX(event)-paddle_width/2;
- }
- double paddle_y = HEIGHT-elevation;
- setLocation(paddle, paddle_x, paddle_y);
- }
- }
- //ball movement
- move(ball, ball_xv, ball_yv);
- if(getX(ball)>=WIDTH-2*RADIUS || getX(ball)<=0){
- ball_xv = -ball_xv;
- }
- if(getY(ball)>=HEIGHT-2*RADIUS || getY(ball)<=0){
- ball_yv = -ball_yv;
- }
- //collision detection
- GObject collision = detectCollision(window, ball);
- if(collision == paddle){
- ball_yv = -ball_yv;
- }
- if(strcmp(getType(collision), "GRect") == 0){
- ball_yv = -ball_yv;
- current_score++;
- removeGWindow(window, collision);
- }
- updateScoreboard(window, label, current_score);
- pause(5);
- }
- // wait for click before exiting
- waitForClick();
- // game over
- closeGWindow(window);
- return 0;
- }
- /**
- * Initializes window with a grid of bricks.
- */
- void initBricks(GWindow window)
- {
- for(int i=0; i<ROWS; i++){
- for(int j=0; j<COLS; j++){
- GRoundRect brick = newGRoundRect(j*(WIDTH-2*COLS)/COLS+2*j, i*20+2*i+20, (WIDTH-2*COLS)/COLS, 20, 10);
- setFilled(brick, true);
- int brick_color = i%3;
- if(brick_color == 0){
- setColor(brick, "CYAN");
- }else if(brick_color == 1){
- setColor(brick, "magenta");
- }else{
- setColor(brick, "green");
- }
- add(window, brick);
- }
- }
- }
- /**
- * Instantiates ball in center of window. Returns ball.
- */
- GOval initBall(GWindow window)
- {
- GOval ball = newGOval(WIDTH/2-RADIUS, HEIGHT/2-RADIUS, 2*RADIUS, 2*RADIUS);
- setFilled(ball, true);
- setColor(ball, "light gray");
- add(window, ball);
- return ball;
- }
- /**
- * Instantiates paddle in bottom-middle of window.
- */
- GRect initPaddle(GWindow window)
- {
- GRect paddle = newGRect(WIDTH/2-paddle_width/2, HEIGHT-elevation, paddle_width, paddle_height);
- setFilled(paddle, true);
- setColor(paddle, "BLUE");
- add(window, paddle);
- return paddle;
- }
- /**
- * Instantiates, configures, and returns label for scoreboard.
- */
- GLabel initScoreboard(GWindow window)
- {
- GLabel score = newGLabel("Score: ");
- setFont(score, "SansSerif-18");
- setColor(score, "BLUE");
- updateScoreboard(window, score, current_score);
- add(window, score);
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement