Advertisement
fernandoReyna

bounce /breakout completed

Jun 23rd, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.67 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. #define PADDLEHEIGHT 20
  24. #define PADDLEWIDTH 100
  25. // number of rows of bricks
  26. #define ROWS 5
  27.  
  28. // number of columns of bricks
  29. #define COLS 10
  30.  
  31. // radius of ball in pixels
  32. #define RADIUS 10
  33.  
  34. // lives
  35. #define LIVES 3
  36.  
  37. // prototypes
  38. void initBricks(GWindow window);
  39. GOval initBall(GWindow window);
  40. GRect initPaddle(GWindow window);
  41. GLabel initScoreboard(GWindow window);
  42. void updateScoreboard(GWindow window, GLabel label, int points);
  43. GObject detectCollision(GWindow window, GOval ball);
  44. int lives = LIVES;
  45. int points = 0;
  46.  
  47. int main(void)
  48. {
  49.     // seed pseudorandom number generator
  50.     srand48(time(NULL));
  51.     // instantiate window
  52.     GWindow window = newGWindow(WIDTH, HEIGHT);
  53.     // instantiate bricks
  54.     initBricks(window);
  55.     // instantiate ball, centered in middle of window
  56.     GOval ball = initBall(window);
  57.     // instantiate paddle, centered at bottom of window
  58.     GRect paddle = initPaddle(window);
  59.     // instantiate scoreboard, centered in middle of window, just above ball
  60.     GLabel label = initScoreboard(window);
  61.  
  62.     // number of bricks initially
  63.     int bricks = COLS * ROWS;
  64.    
  65.        double velocity = drand48();
  66.        double velocityY = 2;
  67.    
  68.     // keep playing until game over
  69.      
  70.     while (lives >0 && points < 50)
  71.    
  72.    
  73.     {
  74.        
  75.        
  76.        GEvent event = getNextEvent(MOUSE_EVENT);
  77.        
  78.         if(event !=NULL)
  79.         {
  80.             if(getEventType(event) == MOUSE_MOVED)
  81.             {
  82.         double x = getX(event) - getWidth(paddle)/2;
  83.         double y=500;
  84.         setLocation(paddle,x,y);
  85.         }
  86.      
  87.     }
  88.    
  89.          move(ball,velocity,velocityY);
  90.            if (getX(ball) + getWidth(ball) >= getWidth(window))
  91.            {
  92.            velocity = -velocity;
  93.            }
  94.            else if (getY(ball) <= 0)
  95.            {
  96.            velocityY = - velocityY;
  97.            }
  98.            else if (getX(ball) <=0)
  99.            {
  100.            velocity = -velocity;
  101.            }
  102.            else if (getY(ball)+ getHeight(ball) >= getHeight(window))
  103.            {
  104.          
  105.           lives = lives - 1;
  106.           waitForClick();
  107.           setLocation(ball,190,290);
  108.           setLocation(paddle,150,500);
  109.            }
  110.            
  111.            GObject object = detectCollision(window,ball);
  112.            if (object == paddle)
  113.            {
  114.            velocityY = -velocityY;
  115.            }
  116.            
  117.            if (object != NULL)
  118.            {
  119.                if (strcmp(getType(object),"GRect") == 0 && object != paddle && object != label)
  120.                {
  121.                
  122.                  removeGWindow(window,object);
  123.                  velocityY = -velocityY;
  124.                  points = points+1;
  125.                  updateScoreboard (window, label, points);
  126.                  }
  127.              }
  128.              
  129.            pause(10);
  130.             }
  131.             waitForClick();
  132.        
  133.     // game over
  134.     closeGWindow(window);
  135.     return 0;
  136. }
  137.  
  138. /**
  139.  * Initializes window with a grid of bricks.
  140.  */
  141. void initBricks(GWindow window)
  142. {
  143.     int   i, j, y;
  144.     int x = -38;
  145.    
  146.     for( j = 0; j < COLS ;j++)
  147.      {
  148.      x = x+40;
  149.      y = 40;
  150.      GRect brick = newGRect(x,y,36,10);
  151.      add (window, brick);
  152.      setColor(brick,"PINK");
  153.      setFilled(brick,true);
  154.      
  155.      for(int i = 0; i< ROWS -1 ;i++)
  156.        {
  157.        y = y + 12;
  158.        GRect brick = newGRect(x,y,36,10);
  159.        add(window,brick);
  160.        
  161.        if(i == 0)
  162.          {
  163.           setColor(brick,"RED");
  164.           setFilled(brick,true);
  165.          }
  166.          
  167.          if (i==1)
  168.          {
  169.          setColor(brick,"ORANGE");
  170.          setFilled(brick,true);
  171.          }
  172.          
  173.          if (i == 2)
  174.          {
  175.          setColor(brick,"YELLOW");
  176.          setFilled(brick,true);
  177.          }
  178.          if(i == 3)
  179.          {
  180.          setColor(brick,"GREEN");
  181.          setFilled(brick, true);
  182.           }
  183.          }
  184.       }
  185.     }
  186.      
  187.        
  188.  
  189. /**
  190.  * Instantiates ball in center of window.  Returns ball.
  191.  */
  192. GOval initBall(GWindow window)
  193. {
  194.     GOval ball = newGOval(175,245,20,20);
  195.     add(window,ball);
  196.     setColor(ball,"black");
  197.     setFilled(ball,true);
  198.    
  199.     return ball;
  200. }
  201.  
  202. /**
  203.  * Instantiates paddle in bottom-middle of window.
  204.  */
  205. GRect initPaddle(GWindow window)
  206. {
  207.     GRect paddle =newGRect(135,520,PADDLEWIDTH,PADDLEHEIGHT);
  208.     add(window,paddle);
  209.    
  210.     setColor(paddle,"black");
  211.     setFilled(paddle,true);
  212.            
  213.     return paddle;
  214.  }
  215.  
  216. /**
  217.  * Instantiates, configures, and returns label for scoreboard.
  218.  */
  219. GLabel initScoreboard(GWindow window)
  220. {
  221.     GLabel label = newGLabel (" ");
  222.     add (window,label);
  223.    
  224.     setLocation(label,100,100);
  225.     setFont(label,"Arial-20");
  226.     return label;
  227. }
  228.  
  229. /**
  230.  * Updates scoreboard's label, keeping it centered in window.
  231.  */
  232. void updateScoreboard(GWindow window, GLabel label, int points)
  233. {
  234.     // update label
  235.     char s[12];
  236.     sprintf(s, "%i", points);
  237.     setLabel(label, s);
  238.  
  239.     // center label in window
  240.     double x = (getWidth(window) - getWidth(label)) / 2;
  241.     double y = (getHeight(window) - getHeight(label)) / 2;
  242.     setLocation(label, x, y);
  243. }
  244.  
  245. /**
  246.  * Detects whether basll has collided with some object in window
  247.  * by checking the four corners of its bounding box (which are
  248.  * outside the ball's GOval, and so the ball can't collide with
  249.  * itself).  Returns object if so, else NULL.
  250.  */
  251. GObject detectCollision(GWindow window, GOval ball)
  252. {
  253.     // ball's location
  254.     double x = getX(ball);
  255.     double y = getY(ball);
  256.  
  257.     // for checking for collisions
  258.     GObject object;
  259.  
  260.     // check for collision at ball's top-left corner
  261.     object = getGObjectAt(window, x, y);
  262.     if (object != NULL)
  263.     {
  264.         return object;
  265.     }
  266.  
  267.     // check for collision at ball's top-right corner
  268.     object = getGObjectAt(window, x + 2 * RADIUS, y);
  269.     if (object != NULL)
  270.     {
  271.         return object;
  272.     }
  273.  
  274.     // check for collision at ball's bottom-left corner
  275.     object = getGObjectAt(window, x, y + 2 * RADIUS);
  276.     if (object != NULL)
  277.     {
  278.         return object;
  279.     }
  280.  
  281.     // check for collision at ball's bottom-right corner
  282.     object = getGObjectAt(window, x + 2 * RADIUS, y + 2 * RADIUS);
  283.     if (object != NULL)
  284.     {
  285.         return object;
  286.     }
  287.  
  288.     // no collision
  289.     return NULL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement