Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // breakout.c
- //
- // Computer Science 50
- // Problem Set 4
- //
- // standard libraries
- #define _XOPEN_SOURCE
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- // Stanford Portable Library
- #include "gevents.h"
- #include "gobjects.h"
- #include "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 20
- // lives
- #define LIVES 3
- // Paddle width
- #define PWIDTH 70
- // Paddle height
- #define PHEIGHT 10
- // 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(int argc,char* argv[])
- {
- // 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 velocityX =-0.7 ;
- if(drand48() > 0.5)
- velocityX = - velocityX; //Random direction velocity
- double velocityY = 2.6;
- char s[3];
- sprintf(s, "%i", points);
- setLabel(label,s);
- waitForClick();
- while (lives > 0 && bricks > 0)
- {
- // Check mouse events
- GEvent mevent = getNextEvent(MOUSE_EVENT) ;
- if(argc > 1)
- {
- if(strcmp(argv[1], "GOD") == 0)
- {
- int x;
- if(getX(ball)+ PWIDTH > WIDTH)
- x = WIDTH-PWIDTH;
- else
- x = getX(ball);
- setLocation(paddle, x,getY(paddle));
- if(mevent != NULL)
- {
- if(getEventType(mevent) == WINDOW_CLOSED)
- exit(0);
- }
- }
- }
- else
- {
- if(mevent != NULL)
- {
- if(getEventType(mevent) == MOUSE_MOVED && getX(mevent) >= PWIDTH/2 && getX(mevent) <= WIDTH-PWIDTH/2)
- setLocation(paddle, getX(mevent) - getWidth(paddle)/2, getY(paddle));
- else if(getEventType(mevent) == WINDOW_CLOSED)
- exit(0);
- }
- }
- // move circle along y-axis
- move(ball, velocityX,velocityY);
- GObject collision = detectCollision(window, ball);
- // bounce off left and right edge of window
- if(getX(ball) <= 0 || getX(ball) +RADIUS >= WIDTH )
- { velocityX = -velocityX; }
- else if(getY(ball) <= 0)
- { velocityY = -velocityY;}
- else if(collision != NULL)
- {
- string temp = getType(collision);
- //If collision(GObject) hit paddle
- if(collision == paddle)
- {
- velocityY = -velocityY;
- }
- //If collision hit brick
- else if((strcmp(temp,"GRect")==0) && collision != paddle)
- {
- velocityY = -velocityY;
- points++;
- bricks--;
- updateScoreboard(window, label, points);
- removeGWindow(window,collision);
- }
- }
- //Check for foul
- else if(getY(ball) + 2 * RADIUS > getY(paddle) + PWIDTH)
- {
- lives--;
- setLocation(paddle, WIDTH/2 - PWIDTH/2, HEIGHT - PHEIGHT - 50);
- setLocation(ball, WIDTH/2 - RADIUS/2, HEIGHT/2 - RADIUS/2);
- waitForClick();
- }
- // linger before moving again
- pause(10);
- }
- // 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++)
- {
- GRect rect = newGRect(0, 100, 36.6, 10);
- setLocation(rect,j*36.6 +(j+1)*3,98 + i*10 + (i+1)*3); //Some maths to get bricks
- setFilled(rect, true);
- char *color;
- switch(i)
- {
- case 0: color = "ORANGE";
- break;
- case 1: color = "RED";
- break;
- case 2: color = "MAGENTA";
- break;
- case 3: color = "BLUE";
- break;
- case 4: color = "GREEN";
- break;
- }
- setColor(rect, color);
- add(window,rect);
- }
- }
- }
- /**
- * Instantiates ball in center of window. Returns ball.
- */
- GOval initBall(GWindow window)
- {
- // TODO
- GOval ball = newGOval(0,0,RADIUS,RADIUS);
- setFilled(ball,true);
- setColor(ball,"GREEN");
- setLocation(ball, WIDTH/2 - RADIUS/2, HEIGHT/2 - RADIUS/2);
- add(window,ball);
- return ball;
- }
- /**
- * Instantiates paddle in bottom-middle of window.
- */
- GRect initPaddle(GWindow window)
- {
- // TODO
- GRect paddle = newGRect(0, 0, PWIDTH, PHEIGHT);
- setFilled(paddle, true);
- setColor(paddle,"BLUE");
- setLocation(paddle,WIDTH/2 - PWIDTH/2,HEIGHT- PHEIGHT - 50); //50 is the spacing between bottom and paddle
- add(window, paddle);
- return paddle;
- }
- /**
- * Instantiates, configures, and returns label for scoreboard.
- */
- GLabel initScoreboard(GWindow window)
- {
- GLabel scoreboard = newGLabel("");
- setFont(scoreboard, "SansSerif-36");
- add(window, scoreboard);
- setLocation(scoreboard, (WIDTH - getWidth(scoreboard)) / 2, (HEIGHT - getHeight(scoreboard)) / 2);
- return scoreboard;
- }
- /**
- * 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