Advertisement
Philosophist

Breakout

May 21st, 2015
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.66 KB | None | 0 0
  1. //
  2. // breakout.c
  3. //
  4. // Computer Science 50
  5. // Problem Set 3
  6. //
  7.  
  8. // standard libraries
  9. #define _XOPEN_SOURCE
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <time.h>
  14.  
  15. //My additions
  16. //#include <algorithm>
  17.  
  18. // Stanford Portable Library
  19. #include <spl/gevents.h>
  20. #include <spl/gobjects.h>
  21. #include <spl/gwindow.h>
  22.  
  23. // height and width of game's window in pixels
  24. #define HEIGHT 600
  25. #define WIDTH 400
  26.  
  27. // number of rows of bricks
  28. #define ROWS 5
  29.  
  30. // number of columns of bricks
  31. #define COLS 10
  32.  
  33. // radius of ball in pixels
  34. #define RADIUS 10
  35.  
  36. // lives
  37. #define LIVES 3
  38.  
  39. // globals
  40. int paddle_width = 85;
  41. int paddle_height = 15;
  42. int elevation = 60;
  43. int ball_diameter = 20;
  44. int current_score = 0;
  45.  
  46. // prototypes
  47. void initBricks(GWindow window);
  48. GOval initBall(GWindow window);
  49. GRect initPaddle(GWindow window);
  50. GLabel initScoreboard(GWindow window);
  51. void updateScoreboard(GWindow window, GLabel label, int points);
  52. GObject detectCollision(GWindow window, GOval ball);
  53.  
  54. int main(void)
  55. {
  56.     // seed pseudorandom number generator
  57.     srand48(time(NULL));
  58.  
  59.     // instantiate window
  60.     GWindow window = newGWindow(WIDTH, HEIGHT);
  61.  
  62.     // instantiate bricks
  63.     initBricks(window);
  64.  
  65.     // instantiate ball, centered in middle of window
  66.     GOval ball = initBall(window);
  67.  
  68.     // instantiate paddle, centered at bottom of window
  69.     GRect paddle = initPaddle(window);
  70.  
  71.     // instantiate scoreboard, centered in middle of window, just above ball
  72.     GLabel label = initScoreboard(window);
  73.    
  74.     // number of bricks initially
  75.     int bricks = COLS * ROWS;
  76.  
  77.     // number of lives initially
  78.     int lives = LIVES;
  79.  
  80.     // number of points initially
  81.     int points = 0;
  82.    
  83.     //Velocity Constants
  84.     double ball_xv = drand48();
  85.     double ball_yv = .5;//Would like to add a function to avoid y-vel values that are too small
  86.    
  87.    
  88.     // keep playing until game over
  89.     while (true)//lives > 0 && bricks > 0)
  90.     {
  91.         // TODO
  92.         GEvent event = getNextEvent(MOUSE_EVENT);
  93.         if(event != NULL){
  94.             if(getEventType(event) == MOUSE_MOVED){
  95.                 double paddle_x;
  96.                 if(getX(event)>WIDTH-paddle_width){//sets horizontal position of paddle to not let it's edges go outside of the window
  97.                     paddle_x = WIDTH-paddle_width;
  98.                 }else if(getX(event)<paddle_width/2){
  99.                     paddle_x = 0;
  100.                 }else{
  101.                     paddle_x = getX(event)-paddle_width/2;
  102.                 }
  103.                 double paddle_y = HEIGHT-elevation;
  104.                 setLocation(paddle, paddle_x, paddle_y);
  105.             }            
  106.         }
  107.         //ball movement
  108.         move(ball, ball_xv, ball_yv);
  109.         if(getX(ball)>=WIDTH-2*RADIUS || getX(ball)<=0){
  110.             ball_xv = -ball_xv;
  111.         }
  112.         if(getY(ball)>=HEIGHT-2*RADIUS || getY(ball)<=0){
  113.             ball_yv = -ball_yv;
  114.         }
  115.        
  116.         //collision detection
  117.         GObject collision = detectCollision(window, ball);
  118.         if(collision == paddle){
  119.             ball_yv = -ball_yv;
  120.         }
  121.         if(strcmp(getType(collision), "GRect") == 0){
  122.             ball_yv = -ball_yv;
  123.             current_score++;
  124.             removeGWindow(window, collision);
  125.         }
  126.        
  127.         updateScoreboard(window, label, current_score);
  128.         pause(5);
  129.     }
  130.  
  131.     // wait for click before exiting
  132.     waitForClick();
  133.  
  134.     // game over
  135.     closeGWindow(window);
  136.     return 0;
  137. }
  138.  
  139. /**
  140.  * Initializes window with a grid of bricks.
  141.  */
  142. void initBricks(GWindow window)
  143. {
  144.     for(int i=0; i<ROWS; i++){
  145.         for(int j=0; j<COLS; j++){
  146.             GRoundRect brick = newGRoundRect(j*(WIDTH-2*COLS)/COLS+2*j, i*20+2*i+20, (WIDTH-2*COLS)/COLS, 20, 10);
  147.             setFilled(brick, true);
  148.             int brick_color = i%3;
  149.             if(brick_color == 0){
  150.                 setColor(brick, "CYAN");
  151.             }else if(brick_color == 1){                
  152.                 setColor(brick, "magenta");
  153.             }else{                
  154.                 setColor(brick, "green");
  155.             }
  156.             add(window, brick);          
  157.         }
  158.     }
  159. }
  160.  
  161. /**
  162.  * Instantiates ball in center of window.  Returns ball.
  163.  */
  164. GOval initBall(GWindow window)
  165. {
  166.     GOval ball = newGOval(WIDTH/2-RADIUS, HEIGHT/2-RADIUS, 2*RADIUS, 2*RADIUS);
  167.     setFilled(ball, true);
  168.     setColor(ball, "light gray");
  169.     add(window, ball);
  170.     return ball;
  171. }
  172.  
  173. /**
  174.  * Instantiates paddle in bottom-middle of window.
  175.  */
  176. GRect initPaddle(GWindow window)
  177. {
  178.     GRect paddle = newGRect(WIDTH/2-paddle_width/2, HEIGHT-elevation, paddle_width, paddle_height);
  179.     setFilled(paddle, true);
  180.     setColor(paddle, "BLUE");
  181.     add(window, paddle);
  182.     return paddle;
  183. }
  184.  
  185. /**
  186.  * Instantiates, configures, and returns label for scoreboard.
  187.  */
  188. GLabel initScoreboard(GWindow window)
  189. {
  190.     GLabel score = newGLabel("Score: ");
  191.     setFont(score, "SansSerif-18");
  192.     setColor(score, "BLUE");
  193.     updateScoreboard(window, score, current_score);
  194.     add(window, score);
  195.     return NULL;
  196. }
  197.  
  198. /**
  199.  * Updates scoreboard's label, keeping it centered in window.
  200.  */
  201. void updateScoreboard(GWindow window, GLabel label, int points)
  202. {
  203.     // update label
  204.     char s[12];
  205.     sprintf(s, "%i", points);
  206.     setLabel(label, s);
  207.  
  208.     // center label in window
  209.     double x = (getWidth(window) - getWidth(label)) / 2;
  210.     double y = (getHeight(window) - getHeight(label)) / 2;
  211.     setLocation(label, x, y);
  212. }
  213.  
  214. /**
  215.  * Detects whether ball has collided with some object in window
  216.  * by checking the four corners of its bounding box (which are
  217.  * outside the ball's GOval, and so the ball can't collide with
  218.  * itself).  Returns object if so, else NULL.
  219.  */
  220. GObject detectCollision(GWindow window, GOval ball)
  221. {
  222.     // ball's location
  223.     double x = getX(ball);
  224.     double y = getY(ball);
  225.  
  226.     // for checking for collisions
  227.     GObject object;
  228.  
  229.     // check for collision at ball's top-left corner
  230.     object = getGObjectAt(window, x, y);
  231.     if (object != NULL)
  232.     {
  233.         return object;
  234.     }
  235.  
  236.     // check for collision at ball's top-right corner
  237.     object = getGObjectAt(window, x + 2 * RADIUS, y);
  238.     if (object != NULL)
  239.     {
  240.         return object;
  241.     }
  242.  
  243.     // check for collision at ball's bottom-left corner
  244.     object = getGObjectAt(window, x, y + 2 * RADIUS);
  245.     if (object != NULL)
  246.     {
  247.         return object;
  248.     }
  249.  
  250.     // check for collision at ball's bottom-right corner
  251.     object = getGObjectAt(window, x + 2 * RADIUS, y + 2 * RADIUS);
  252.     if (object != NULL)
  253.     {
  254.         return object;
  255.     }
  256.  
  257.     // no collision
  258.     return NULL;
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement