Advertisement
Hazem3529

breakout

Jul 1st, 2015
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.71 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.     double velocity = 1;
  73.    
  74.     // keep playing until game over
  75.     while (lives > 0 && bricks > 0)
  76.     {
  77.         updateScoreboard(window, label, points);
  78.         //move(gobj, dx, dy);
  79.         move(ball, velocity, velocity);
  80.         pause(10);
  81.        
  82.         GEvent event = getNextEvent(MOUSE_EVENT);
  83.  if (event != NULL)
  84.         {
  85.             // if the event was movement
  86.             if (getEventType(event) == MOUSE_MOVED)
  87.            {
  88.                 // ensure paddle follows top cursor
  89.                 double x = getX(event) - getWidth(paddle) / 2;
  90.                 double y = 500;
  91.                 setLocation(paddle,x, y);
  92.             }
  93.  
  94.     }
  95.  
  96.   //collsion
  97.   GObject object = detectCollision(window, ball);
  98.   if (object != NULL){
  99.  
  100.  
  101.   if (object == paddle)
  102. {
  103.     velocity=-velocity;
  104.    
  105. }
  106.    else if (strcmp(getType(object), "GRect") == 0)
  107.             {
  108.                 removeGWindow(window, object);
  109.                 velocity = -velocity;
  110.                 points++;
  111.                 bricks--;
  112.                 velocity+=0.3;                
  113.             }
  114.   }
  115.   //if the ball hit the right wall
  116.      if (getX(ball) + getWidth(ball) >= getWidth(window))
  117.         {
  118.             velocity = -velocity;
  119.         } else if (getX(ball) <= 0)
  120.         {
  121.             velocity = -velocity;
  122.         }
  123.  
  124.     if (getY(ball) + getWidth(ball) >= getWidth(window))
  125.         {
  126.            lives--;
  127.             //move ball to start
  128.             setLocation(ball, 190, 200);
  129.             //move paddle to start
  130.             setLocation(paddle, 160, 500);
  131.             waitForClick();
  132.         } else if (getY(ball) <= 0)
  133.         {
  134.         velocity = -velocity;
  135.            
  136.         }
  137.  
  138.   }
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.     // wait for click before exiting
  146.     waitForClick();
  147.  
  148.     // game over
  149.     closeGWindow(window);
  150.     return 0;
  151.  
  152. }
  153. /**
  154.  * Initializes window with a grid of bricks.
  155.  */
  156. void initBricks(GWindow window)
  157. {
  158.         int brick_y = 50; //start postion of y at 50 pixel from
  159.     for(int i =0; i<ROWS; i++){
  160.    
  161.    
  162.         int brick_x=2;  //start postion on X axis 2 px
  163.         for (int j=0; j<COLS; j++){
  164.         //to make a fucking brick
  165.         //rect = newGRect(x, y, width, height);
  166.  
  167.             GRect brick = newGRect(brick_x + 5, brick_y, 35, 10);
  168.             //set cool fucking colors
  169.             if(i==0)  setColor(brick, "Black");
  170.              if(i==1)  setColor(brick, "Black");
  171.             if(i==2)  setColor(brick, "Blue");
  172.              if(i==3)  setColor(brick, "RED");
  173.               if(i==4)  setColor(brick, "RED");
  174.             setFilled(brick, true);
  175.             add(window, brick);            
  176.             brick_x+=39;
  177.         }
  178.    
  179.     brick_y+=18;
  180.     }
  181. }
  182.  
  183. /**
  184.  * Instantiates ball in center of window.  Returns ball.
  185.  */
  186. GOval initBall(GWindow window)
  187. {//oval = newGOval(x, y, width, height);
  188. GOval ball = newGOval(200 - 10, 300, 20, 20);
  189.     setColor(ball, "Blue");
  190.     setFilled(ball, true);
  191.     add(window, ball);
  192. return ball;
  193. }
  194.  
  195. /**
  196.  * Instantiates paddle in bottom-middle of window.
  197.  */
  198. GRect initPaddle(GWindow window)
  199. {
  200.      GRect paddle = newGRect(150, 550, 75, 10);
  201.      setFilled(paddle, true);
  202.             add(window, paddle);
  203.             setColor(paddle, "Green");            
  204.     return paddle;
  205. }
  206.  
  207. /**
  208.  * Instantiates, configures, and returns label for scoreboard.
  209.  */
  210. GLabel initScoreboard(GWindow window)
  211. { //like the video
  212.    GLabel label = newGLabel("Zero");
  213.     setFont(label, "SansSerif-50");
  214.     setColor(label, "C0C0C0");
  215.     add(window, label);
  216.     setLocation(label, 180, 300);
  217.     return label;
  218.  
  219. }
  220.  
  221. /**
  222.  * Updates scoreboard's label, keeping it centered in window.
  223.  */
  224. void updateScoreboard(GWindow window, GLabel label, int points)
  225. {
  226.     // update label
  227.     char s[12];
  228.     sprintf(s, "%i", points);
  229.     setLabel(label, s);
  230.  
  231.     // center label in window
  232.     double x = (getWidth(window) - getWidth(label)) / 2;
  233.     double y = (getHeight(window) - getHeight(label)) / 2;
  234.     setLocation(label, x, y);
  235. }
  236.  
  237. /**
  238.  * Detects whether ball has collided with some object in window
  239.  * by checking the four corners of its bounding box (which are
  240.  * outside the ball's GOval, and so the ball can't collide with
  241.  * itself).  Returns object if so, else NULL.
  242.  */
  243. GObject detectCollision(GWindow window, GOval ball)
  244. {
  245.     // ball's location
  246.     double x = getX(ball);
  247.     double y = getY(ball);
  248.  
  249.     // for checking for collisions
  250.     GObject object;
  251.  
  252.     // check for collision at ball's top-left corner
  253.     object = getGObjectAt(window, x, y);
  254.     if (object != NULL)
  255.     {
  256.         return object;
  257.     }
  258.  
  259.     // check for collision at ball's top-right corner
  260.     object = getGObjectAt(window, x + 2 * RADIUS, y);
  261.     if (object != NULL)
  262.     {
  263.         return object;
  264.     }
  265.  
  266.     // check for collision at ball's bottom-left corner
  267.     object = getGObjectAt(window, x, y + 2 * RADIUS);
  268.     if (object != NULL)
  269.     {
  270.         return object;
  271.     }
  272.  
  273.     // check for collision at ball's bottom-right corner
  274.     object = getGObjectAt(window, x + 2 * RADIUS, y + 2 * RADIUS);
  275.     if (object != NULL)
  276.     {
  277.         return object;
  278.     }
  279.  
  280.     // no collision
  281.     return NULL;
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement