Advertisement
fernandoReyna

breakoutcode

Jun 11th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.24 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.     // inspoints = 0;
  53.  
  54.     // keep playing until game over
  55.     while (lives > 0 && bricks > 0)
  56.     {
  57.         // TODO
  58.         double velocity  = 2.15;
  59.         while(true)
  60.          {
  61.          move(circle,velocity,velocity);
  62.            if (getX(circle) + getWidth(circle) >= getWith(window))
  63.            {
  64.            velocity = -velocity;
  65.            }
  66.            else if (getX(circle) <= 0 )
  67.            {
  68.            velocity = - velocity;
  69.            }
  70.            else if (getY(circle) <=0)
  71.            {
  72.            velocity = -velocity;
  73.            }
  74.            else if (getY(circle) >= 540)
  75.            {
  76.            velocity = -velocity;
  77.            }
  78.            pause(10);
  79.          }
  80.        
  81.     }
  82.  
  83.     // wait for click before exiting
  84.     waitForClick();tantiate bricks
  85.     initBricks(window);
  86.  
  87.     // instantiate ball, centered in middle of window
  88.     GOval ball = initBall(window);
  89.  
  90.     // instantiate paddle, centered at bottom of window
  91.     GRect paddle = initPaddle(window);
  92.  
  93.     // instantiate scoreboard, centered in middle of window, just above ball
  94.     GLabel label = initScoreboard(window);
  95.  
  96.     // number of bricks initially
  97.     int bricks = COLS * ROWS;
  98.  
  99.     // number of lives initially
  100.     int lives = LIVES;
  101.  
  102.     // number of points initially
  103.     int
  104.  
  105.     // game over
  106.     closeGWindow(window);
  107.     return 0;
  108. }
  109.  
  110. /**
  111.  * Initializes window with a grid of bricks.
  112.  */
  113. void initBricks(GWindow window)
  114. {
  115. int x =0; int y =0;
  116. for(int a=0; a<4;a++)
  117.  {
  118.  for (int i=0;i<8;i++)
  119.   {
  120.   GRect rect = newGRect(x,y,45,20);
  121.   setColor(rect,"red");
  122.   setFilled(rect,true);
  123.   add(window,rect);
  124.  
  125.   x=x+50;
  126.   }
  127.   x=0;
  128.   y=y+25;
  129.   }
  130. }
  131.     // TODO
  132.    
  133.  
  134.  
  135. /**
  136.  * Instantiates ball in center of window.  Returns ball.
  137.  */
  138. GOval initBall(GWindow window)
  139. {
  140.     // TODO
  141.         GOval circle = newGOval(175,245,50,50);
  142.        setColor(circle,"black");
  143.        setFilled(circle,true);
  144.        add(window,circle);
  145.        
  146.        
  147.          
  148.     return NULL;
  149.        }
  150. }
  151.  
  152.  
  153. /**
  154.  * Instantiates paddle in bottom-middle of window.
  155.  */
  156. GRect initPaddle(GWindow window)
  157. {
  158.     // TODO
  159.   GRect rect = newGRect(135,520,65,10);
  160.   setColor(rect,"black");
  161.   setFilled(rect,true);
  162.   add(window,rect);
  163.    
  164.     while (true)
  165.     {
  166.       GEvent event = getNextEvent(MOUSE_EVENT);
  167.      
  168.       if(event != NULL)
  169.       {
  170.         double x = getX(event) - getWidth(rect)/2;
  171.         double y= 520;//getY(event) - getWidth(rect);
  172.         setLocation(rect,x,y);
  173.         }
  174.       }
  175.     return NULL;
  176. }
  177.  
  178. /**
  179.  * Instantiates, configures, and returns label for scoreboard.
  180.  */
  181. GLabel initScoreboard(GWindow window)
  182. {
  183.     // TODO
  184.     return NULL;
  185. }
  186.  
  187. /**
  188.  * Updates scoreboard's label, keeping it centered in window.
  189.  */
  190. void updateScoreboard(GWindow window, GLabel label, int points)
  191. {
  192.     // update label
  193.     char s[12];
  194.     sprintf(s, "%i", points);
  195.     setLabel(label, s);
  196.  
  197.     // center label in window
  198.     double x = (getWidth(window) - getWidth(label)) / 2;
  199.     double y = (getHeight(window) - getHeight(label)) / 2;
  200.     setLocation(label, x, y);
  201. }
  202.  
  203. /**
  204.  * Detects whether ball has collided with some object in window
  205.  * by checking the four corners of its bounding box (which are
  206.  * outside the ball's GOval, and so the ball can't collide with
  207.  * itself).  Returns object if so, else NULL.
  208.  */
  209. GObject detectCollision(GWindow window, GOval ball)
  210. {
  211.     // ball's location
  212.     double x = getX(ball);
  213.     double y = getY(ball);
  214.  
  215.     // for checking for collisions
  216.     GObject object;
  217.  
  218.     // check for collision at ball's top-left corner
  219.     object = getGObjectAt(window, x, y);
  220.     if (object != NULL)
  221.     {
  222.         return object;
  223.     }
  224.  
  225.     // check for collision at ball's top-right corner
  226.     object = getGObjectAt(window, x + 2 * RADIUS, y);
  227.     if (object != NULL)
  228.     {
  229.         return object;
  230.     }
  231.  
  232.     // check for collision at ball's bottom-left corner
  233.     object = getGObjectAt(window, x, y + 2 * RADIUS);
  234.     if (object != NULL)
  235.     {
  236.         return object;
  237.     }
  238.  
  239.     // check for collision at ball's bottom-right corner
  240.     object = getGObjectAt(window, x + 2 * RADIUS, y + 2 * RADIUS);
  241.     if (object != NULL)
  242.     {
  243.         return object;
  244.     }
  245.  
  246.     // no collision
  247.     return NULL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement