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
- //height and width of paddle in pixels
- #define PH 20
- #define PW 80
- // h and w of ball in pixel
- #define bal 15
- // 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;
- int runtime=0;
- //initial velocity-x started
- double velocitx;
- double velocity;
- // keep playing until game over
- while (lives > 0 && bricks > 0)
- {
- updateScoreboard(window,label, points);
- //checking if its the 1st time the ball moves after losing
- if(runtime==0)
- {
- velocitx= 2.0;
- velocity =drand48()+2;
- waitForClick();
- }
- move(ball, velocitx, velocity);
- // TODO-paddle move-done-ball move to be improved(can hit several bricks in a row, in a strange way)
- GEvent event = getNextEvent(MOUSE_EVENT);
- // to know if we have 2 wait for click to move the ball
- runtime=runtime+1;
- // if event
- if (event != NULL)
- {
- // if event movement
- if (getEventType(event) == MOUSE_MOVED)
- {
- // move
- double x = getX(event) - getWidth(paddle) / 2;
- //let's check it the paddle isn't going out of the screen
- if (x>0 && x<WIDTH-PW)
- {
- setLocation(paddle, x, HEIGHT-60-PH/2);
- }
- }
- }
- // bounce off right edge of window
- if (getX(ball) + getWidth(ball) >= getWidth(window))
- {
- velocitx = -velocitx;
- }
- else if (getX(ball) <= 0)
- {
- velocitx = -velocitx;
- }
- if (getY(ball)<=0)
- {
- velocity=-velocity;
- }
- else if(getY(ball) + getHeight(ball) >= getHeight(window) && runtime!=0)
- {
- lives=lives-1;
- runtime=0;
- while(getY(ball)+bal<HEIGHT)
- {}
- setLocation(ball,WIDTH/2-bal/2,HEIGHT/2);
- pause(20);
- }
- //lets talk collision now
- GObject object = detectCollision(window, ball);
- if(object!=NULL)
- {
- if (strcmp(getType(object), "GRect") == 0)
- {
- if (object==paddle)
- {
- velocity=-velocity;
- }
- else
- {
- points=points+1;
- bricks=bricks-1;
- removeGWindow(window,object);
- if(velocity<0)
- {
- velocity=-velocity;
- }
- else
- {
- velocitx=-velocitx;
- }
- }
- }
- }
- // linger before moving again
- pause(15);
- }
- if (bricks==0)
- {
- printf("You won! Thanks for playing!\n");
- }
- if(lives==0)
- {
- printf("You lost! Try playing again!\n");
- }
- // wait for click before exiting
- waitForClick();
- // game over
- closeGWindow(window);
- return 0;
- }
- /**
- * Initializes window with a grid of bricks.
- */
- void initBricks(GWindow window)
- {
- // TODO-done
- for (int i=0; i<ROWS;i++)
- {
- for (int j=0; j<COLS; j++)
- {
- GRect brick = newGRect(5+j*39,60+20*i,35,10);
- setFilled(brick, true);
- if (i==0)
- {
- setColor(brick,"RED");
- }
- else if (i==1)
- {
- setColor(brick,"YELLOW");
- }
- else if (i==2)
- {
- setColor(brick,"BLUE");
- }
- else if (i==3)
- {
- setColor(brick,"GREEN");
- }
- else
- {
- setColor(brick, "PINK");
- }
- add (window, brick);
- }
- }
- }
- /**
- * Instantiates ball in center of window. Returns ball.
- */
- GOval initBall(GWindow window)
- {
- // TODO-done
- GOval ball = newGOval (WIDTH/2-bal/2,HEIGHT/2,bal,bal);
- setFilled(ball,true);
- setColor(ball, "BLACK");
- add(window, ball);
- return ball;
- }
- /**
- * Instantiates paddle in bottom-middle of window.
- */
- GRect initPaddle(GWindow window)
- {
- // TODO-Done
- //i want the paddle to be at the bottom but not too much and centered
- GRect Paddle = newGRect(WIDTH/2-PW/2,HEIGHT-60-PH/2,PW,PH);
- setFilled(Paddle,true);
- setColor(Paddle, "BLACK");
- add(window, Paddle);
- return Paddle;
- }
- /**
- * Instantiates, configures, and returns label for scoreboard.
- */
- GLabel initScoreboard(GWindow window)
- {
- // TODO-done
- GLabel label = newGLabel("");
- setFont(label, "SansSerif-36");
- 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);
- }
- /**
- * 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