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>
- // 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
- // 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;
- double velocity = 1;
- // keep playing until game over
- while (lives > 0 && bricks > 0)
- {
- updateScoreboard(window, label, points);
- //move(gobj, dx, dy);
- move(ball, velocity, velocity);
- pause(10);
- GEvent event = getNextEvent(MOUSE_EVENT);
- if (event != NULL)
- {
- // if the event was movement
- if (getEventType(event) == MOUSE_MOVED)
- {
- // ensure paddle follows top cursor
- double x = getX(event) - getWidth(paddle) / 2;
- double y = 500;
- setLocation(paddle,x, y);
- }
- }
- //collsion
- GObject object = detectCollision(window, ball);
- if (object != NULL){
- if (object == paddle)
- {
- velocity=-velocity;
- }
- else if (strcmp(getType(object), "GRect") == 0)
- {
- removeGWindow(window, object);
- velocity = -velocity;
- points++;
- bricks--;
- velocity+=0.3;
- }
- }
- //if the ball hit the right wall
- if (getX(ball) + getWidth(ball) >= getWidth(window))
- {
- velocity = -velocity;
- } else if (getX(ball) <= 0)
- {
- velocity = -velocity;
- }
- if (getY(ball) + getWidth(ball) >= getWidth(window))
- {
- lives--;
- //move ball to start
- setLocation(ball, 190, 200);
- //move paddle to start
- setLocation(paddle, 160, 500);
- waitForClick();
- } else if (getY(ball) <= 0)
- {
- velocity = -velocity;
- }
- }
- // wait for click before exiting
- waitForClick();
- // game over
- closeGWindow(window);
- return 0;
- }
- /**
- * Initializes window with a grid of bricks.
- */
- void initBricks(GWindow window)
- {
- int brick_y = 50; //start postion of y at 50 pixel from
- for(int i =0; i<ROWS; i++){
- int brick_x=2; //start postion on X axis 2 px
- for (int j=0; j<COLS; j++){
- //to make a fucking brick
- //rect = newGRect(x, y, width, height);
- GRect brick = newGRect(brick_x + 5, brick_y, 35, 10);
- //set cool fucking colors
- if(i==0) setColor(brick, "Black");
- if(i==1) setColor(brick, "Black");
- if(i==2) setColor(brick, "Blue");
- if(i==3) setColor(brick, "RED");
- if(i==4) setColor(brick, "RED");
- setFilled(brick, true);
- add(window, brick);
- brick_x+=39;
- }
- brick_y+=18;
- }
- }
- /**
- * Instantiates ball in center of window. Returns ball.
- */
- GOval initBall(GWindow window)
- {//oval = newGOval(x, y, width, height);
- GOval ball = newGOval(200 - 10, 300, 20, 20);
- setColor(ball, "Blue");
- setFilled(ball, true);
- add(window, ball);
- return ball;
- }
- /**
- * Instantiates paddle in bottom-middle of window.
- */
- GRect initPaddle(GWindow window)
- {
- GRect paddle = newGRect(150, 550, 75, 10);
- setFilled(paddle, true);
- add(window, paddle);
- setColor(paddle, "Green");
- return paddle;
- }
- /**
- * Instantiates, configures, and returns label for scoreboard.
- */
- GLabel initScoreboard(GWindow window)
- { //like the video
- GLabel label = newGLabel("Zero");
- setFont(label, "SansSerif-50");
- setColor(label, "C0C0C0");
- add(window, label);
- setLocation(label, 180, 300);
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement