Advertisement
laykhorn

BREAKOUT SOURCE CODE

Sep 29th, 2015
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.17 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. // Stanford Portable Library
  16. #include <spl/gevents.h>
  17. #include <spl/gobjects.h>
  18. #include <spl/gwindow.h>
  19.  
  20. // height and width of game's window in pixels
  21. #define HEIGHT 600
  22. #define WIDTH 400
  23.  
  24. // number of rows of bricks
  25. #define ROWS 5
  26.  
  27. // number of columns of bricks
  28. #define COLS 10
  29.  
  30. // radius of ball in pixels
  31. #define RADIUS 10
  32.  
  33. // lives
  34. #define LIVES 3
  35.  
  36. // prototypes
  37. void initBricks(GWindow window);
  38. GOval initBall(GWindow window);
  39. GRect initPaddle(GWindow window);
  40. GLabel initScoreboard(GWindow window);
  41. void updateScoreboard(GWindow window, GLabel label, int points);
  42. GObject detectCollision(GWindow window, GOval ball);
  43.  
  44. int main(void)
  45. {
  46.     // seed pseudorandom number generator
  47.     srand48(time(NULL));
  48.  
  49.     // instantiate window
  50.     GWindow window = newGWindow(WIDTH, HEIGHT);
  51.  
  52.     // instantiate bricks
  53.     initBricks(window);
  54.  
  55.     // instantiate ball, centered in middle of window
  56.     GOval ball = initBall(window);
  57.  
  58.     // instantiate paddle, centered at bottom of window
  59.     GRect paddle = initPaddle(window);
  60.  
  61.     // instantiate scoreboard, centered in middle of window, just above ball
  62.     GLabel label = initScoreboard(window);
  63.  
  64.     // number of bricks initially
  65.     int bricks = COLS * ROWS;
  66.  
  67.     // number of lives initially
  68.     int lives = LIVES;
  69.  
  70.     // number of points initially
  71.     int points = 0;
  72.  
  73.     // keep playing until game over
  74.     while (lives > 0 && bricks > 0)
  75.     {
  76.         // TODO
  77.         // check for mouse event
  78.         GEvent event = getNextEvent(MOUSE_EVENT);
  79.  
  80.         // if we heard one
  81.         if (event != NULL)
  82.         {
  83.             // if the event was movement
  84.             if (getEventType(event) == MOUSE_MOVED)
  85.             {
  86.                 // ensure circle follows top cursor
  87.                 double x = getX(event) - getWidth(paddle) / 2;
  88.                 double y = 15;
  89.                 setLocation(paddle, x, y);
  90.             }
  91.         }
  92.     }
  93.  
  94.     // wait for click before exiting
  95.     waitForClick();
  96.  
  97.     // game over
  98.     closeGWindow(window);
  99.     return 0;
  100. }
  101.  
  102. /**
  103.  * Initializes window with a grid of bricks.
  104.  */
  105. void initBricks(GWindow window)
  106. {
  107.     // TODO
  108.     for (int r = 0; r < ROWS; r++)
  109.     {
  110.         for (int c = 0; c < COLS; c++)
  111.             {
  112.             GRect bricks = newGRect(10, 10, 30, 15);
  113.             setFilled(bricks, true);
  114.             setColor(bricks, "RED");
  115.                 if ( r == 1)
  116.                 { setColor(bricks, "YELLOW"); }
  117.                 else if (r == 2)
  118.                 { setColor(bricks, "BLUE"); }
  119.                 else if ( r == 3)
  120.                 { setColor(bricks, "GREEN"); }
  121.                 else
  122.                 { setColor(bricks, "BLACK"); }
  123.            
  124.             add(window, bricks);
  125.             printf(" ");
  126.             }      
  127.        
  128.     }
  129. }
  130.  
  131. /**
  132.  * Instantiates ball in center of window.  Returns ball.
  133.  */
  134. GOval initBall(GWindow window)
  135. {
  136.     // TODO
  137.     return NULL;
  138. }
  139.  
  140. /**
  141.  * Instantiates paddle in bottom-middle of window.
  142.  */
  143. GRect initPaddle(GWindow window)
  144.         {
  145.             // TODO
  146.             GRect paddle = newGRect(WIDTH/2, 400, 60, 15);
  147.             setFilled(paddle, true);
  148.             setColor(paddle, "BLACK");
  149.             add(window, paddle);
  150.            
  151.         }
  152.  
  153. /**
  154.  * Instantiates, configures, and returns label for scoreboard.
  155.  */
  156. GLabel initScoreboard(GWindow window)
  157. {
  158.     // TODO
  159.     return NULL;
  160. }
  161.  
  162. /**
  163.  * Updates scoreboard's label, keeping it centered in window.
  164.  */
  165. void updateScoreboard(GWindow window, GLabel label, int points)
  166. {
  167.     // update label
  168.     char s[12];
  169.     sprintf(s, "%i", points);
  170.     setLabel(label, s);
  171.  
  172.     // center label in window
  173.     double x = (getWidth(window) - getWidth(label)) / 2;
  174.     double y = (getHeight(window) - getHeight(label)) / 2;
  175.     setLocation(label, x, y);
  176. }
  177.  
  178. /**
  179.  * Detects whether ball has collided with some object in window
  180.  * by checking the four corners of its bounding box (which are
  181.  * outside the ball's GOval, and so the ball can't collide with
  182.  * itself).  Returns object if so, else NULL.
  183.  */
  184. GObject detectCollision(GWindow window, GOval ball)
  185. {
  186.     // ball's location
  187.     double x = getX(ball);
  188.     double y = getY(ball);
  189.  
  190.     // for checking for collisions
  191.     GObject object;
  192.  
  193.     // check for collision at ball's top-left corner
  194.     object = getGObjectAt(window, x, y);
  195.     if (object != NULL)
  196.     {
  197.         return object;
  198.     }
  199.  
  200.     // check for collision at ball's top-right corner
  201.     object = getGObjectAt(window, x + 2 * RADIUS, y);
  202.     if (object != NULL)
  203.     {
  204.         return object;
  205.     }
  206.  
  207.     // check for collision at ball's bottom-left corner
  208.     object = getGObjectAt(window, x, y + 2 * RADIUS);
  209.     if (object != NULL)
  210.     {
  211.         return object;
  212.     }
  213.  
  214.     // check for collision at ball's bottom-right corner
  215.     object = getGObjectAt(window, x + 2 * RADIUS, y + 2 * RADIUS);
  216.     if (object != NULL)
  217.     {
  218.         return object;
  219.     }
  220.  
  221.     // no collision
  222.     return NULL;
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement