Advertisement
Guest User

Ibrahim / Breakout.c

a guest
Dec 13th, 2014
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.10 KB | None | 0 0
  1. //
  2. // breakout.c
  3. //
  4. // Computer Science 50
  5. // Problem Set 4
  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 "gevents.h"
  17. #include "gobjects.h"
  18. #include "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 20
  32.  
  33. // lives
  34. #define LIVES 3
  35.  
  36. // Paddle width
  37. #define PWIDTH 70
  38.  
  39. // Paddle height
  40. #define PHEIGHT 10
  41.  
  42. // prototypes
  43. void initBricks(GWindow window);
  44. GOval initBall(GWindow window);
  45. GRect initPaddle(GWindow window);
  46. GLabel initScoreboard(GWindow window);
  47. void updateScoreboard(GWindow window, GLabel label, int points);
  48. GObject detectCollision(GWindow window, GOval ball);
  49.  
  50. int main(int argc,char* argv[])
  51. {
  52.     // seed pseudorandom number generator
  53.     srand48(time(NULL));
  54.  
  55.     // instantiate window
  56.     GWindow window = newGWindow(WIDTH, HEIGHT);
  57.  
  58.     // instantiate bricks
  59.     initBricks(window);
  60.  
  61.     // instantiate ball, centered in middle of window
  62.     GOval ball = initBall(window);
  63.  
  64.     // instantiate paddle, centered at bottom of window
  65.     GRect paddle = initPaddle(window);
  66.    
  67.     // instantiate scoreboard, centered in middle of window, just above ball
  68.     GLabel label = initScoreboard(window);
  69.  
  70.     // number of bricks initially
  71.     int bricks = COLS * ROWS;
  72.  
  73.     // number of lives initially
  74.     int lives = LIVES;
  75.  
  76.     // number of points initially
  77.     int points = 0;
  78.    
  79.     // keep playing until game over
  80.     double velocityX =-0.7  ;
  81.     if(drand48() > 0.5)
  82.         velocityX = - velocityX; //Random direction velocity
  83.     double velocityY = 2.6;
  84.     char s[3];
  85.     sprintf(s, "%i", points);
  86.     setLabel(label,s);
  87.     waitForClick();
  88.     while (lives > 0 && bricks > 0)
  89.     {
  90.         // Check mouse events
  91.         GEvent mevent = getNextEvent(MOUSE_EVENT) ;
  92.         if(argc > 1)
  93.         {
  94.             if(strcmp(argv[1], "GOD") == 0)
  95.             {    
  96.                 int x;
  97.            
  98.                 if(getX(ball)+ PWIDTH > WIDTH)
  99.                     x = WIDTH-PWIDTH;
  100.                 else
  101.                     x = getX(ball);
  102.                     setLocation(paddle, x,getY(paddle));
  103.            
  104.             if(mevent != NULL)  
  105.             {
  106.                 if(getEventType(mevent) == WINDOW_CLOSED)
  107.                 exit(0);
  108.             }
  109.         }
  110.         }
  111.         else
  112.         {
  113.             if(mevent != NULL)  
  114.             {
  115.                 if(getEventType(mevent) == MOUSE_MOVED && getX(mevent) >= PWIDTH/2 && getX(mevent) <= WIDTH-PWIDTH/2)
  116.                     setLocation(paddle, getX(mevent) - getWidth(paddle)/2, getY(paddle));    
  117.                 else if(getEventType(mevent) == WINDOW_CLOSED)
  118.                     exit(0);
  119.             }
  120.          }
  121.         // move circle along y-axis
  122.         move(ball, velocityX,velocityY);
  123.        
  124.        
  125.         GObject collision = detectCollision(window, ball);
  126.         // bounce off left and right edge of window
  127.         if(getX(ball) <= 0 || getX(ball) +RADIUS >= WIDTH )
  128.           {  velocityX = -velocityX; }
  129.         else if(getY(ball) <= 0)
  130.           {  velocityY = -velocityY;}
  131.         else if(collision != NULL)
  132.         {
  133.             string temp = getType(collision);
  134.             //If collision(GObject) hit paddle
  135.             if(collision == paddle)
  136.             {  
  137.                 velocityY = -velocityY;
  138.             }
  139.             //If collision hit brick
  140.             else if((strcmp(temp,"GRect")==0) && collision != paddle)
  141.             {
  142.                  velocityY = -velocityY;
  143.                  points++;
  144.                  bricks--;
  145.                  updateScoreboard(window, label, points);
  146.                  removeGWindow(window,collision);
  147.             }
  148.            
  149.            
  150.          
  151.         }
  152.        
  153.         //Check for foul
  154.         else if(getY(ball) + 2 * RADIUS > getY(paddle) + PWIDTH)
  155.         {
  156.             lives--;
  157.             setLocation(paddle, WIDTH/2 - PWIDTH/2, HEIGHT - PHEIGHT - 50);
  158.             setLocation(ball, WIDTH/2 - RADIUS/2, HEIGHT/2 - RADIUS/2);            
  159.             waitForClick();
  160.            
  161.         }
  162.        
  163.        
  164.        
  165.        
  166.         // linger before moving again
  167.         pause(10);
  168.     }
  169.  
  170.    
  171.    
  172.     // wait for click before exiting
  173.     waitForClick();
  174.  
  175.     // game over
  176.     closeGWindow(window);
  177.     return 0;
  178. }
  179.  
  180. /**
  181.  * Initializes window with a grid of bricks.
  182.  */
  183. void initBricks(GWindow window)
  184. {
  185.     for(int i = 0; i < ROWS; i++)
  186.     {
  187.         for(int j = 0; j < COLS; j++)
  188.         {
  189.             GRect rect = newGRect(0, 100, 36.6, 10);
  190.             setLocation(rect,j*36.6 +(j+1)*3,98 + i*10 + (i+1)*3); //Some maths to get bricks
  191.             setFilled(rect, true);
  192.             char *color;
  193.  
  194.             switch(i)
  195.             {
  196.                 case 0: color = "ORANGE";
  197.                         break;
  198.                 case 1: color = "RED";
  199.                         break;
  200.                 case 2: color = "MAGENTA";
  201.                         break;
  202.                 case 3: color = "BLUE";
  203.                         break;
  204.                 case 4: color = "GREEN";
  205.                         break;
  206.                
  207.             }
  208.             setColor(rect, color);
  209.             add(window,rect);
  210.         }
  211.     }
  212.    
  213. }
  214.  
  215. /**
  216.  * Instantiates ball in center of window.  Returns ball.
  217.  */
  218. GOval initBall(GWindow window)
  219. {
  220.     // TODO
  221.    
  222.     GOval ball = newGOval(0,0,RADIUS,RADIUS);
  223.     setFilled(ball,true);
  224.     setColor(ball,"GREEN");
  225.     setLocation(ball, WIDTH/2 - RADIUS/2, HEIGHT/2 - RADIUS/2);
  226.     add(window,ball);
  227.    
  228.     return ball;
  229. }
  230.  
  231. /**
  232.  * Instantiates paddle in bottom-middle of window.
  233.  */
  234. GRect initPaddle(GWindow window)
  235. {
  236.     // TODO
  237.    
  238.     GRect paddle = newGRect(0, 0, PWIDTH, PHEIGHT);
  239.     setFilled(paddle, true);
  240.     setColor(paddle,"BLUE");
  241.     setLocation(paddle,WIDTH/2 - PWIDTH/2,HEIGHT- PHEIGHT - 50); //50 is the spacing between bottom and paddle
  242.     add(window, paddle);
  243.         return paddle;
  244. }
  245.  
  246. /**
  247.  * Instantiates, configures, and returns label for scoreboard.
  248.  */
  249. GLabel initScoreboard(GWindow window)
  250. {
  251.     GLabel scoreboard = newGLabel("");
  252.     setFont(scoreboard, "SansSerif-36");
  253.     add(window, scoreboard);
  254.     setLocation(scoreboard, (WIDTH - getWidth(scoreboard)) / 2, (HEIGHT - getHeight(scoreboard)) / 2);
  255.     return scoreboard;
  256. }
  257.  
  258. /**
  259.  * Updates scoreboard's label, keeping it centered in window.
  260.  */
  261. void updateScoreboard(GWindow window, GLabel label, int points)
  262. {
  263.     // update label
  264.     char s[12];
  265.     sprintf(s, "%i", points);
  266.     setLabel(label, s);
  267.  
  268.     // center label in window
  269.     double x = (getWidth(window) - getWidth(label)) / 2;
  270.     double y = (getHeight(window) - getHeight(label)) / 2;
  271.     setLocation(label, x, y);
  272. }
  273.  
  274. /**
  275.  * Detects whether ball has collided with some object in window
  276.  * by checking the four corners of its bounding box (which are
  277.  * outside the ball's GOval, and so the ball can't collide with
  278.  * itself).  Returns object if so, else NULL.
  279.  */
  280. GObject detectCollision(GWindow window, GOval ball)
  281. {
  282.     // ball's location
  283.     double x = getX(ball);
  284.     double y = getY(ball);
  285.  
  286.     // for checking for collisions
  287.     GObject object;
  288.  
  289.     // check for collision at ball's top-left corner
  290.     object = getGObjectAt(window, x, y);
  291.     if (object != NULL)
  292.     {
  293.         return object;
  294.     }
  295.  
  296.     // check for collision at ball's top-right corner
  297.     object = getGObjectAt(window, x + 2 * RADIUS, y);
  298.     if (object != NULL)
  299.     {
  300.         return object;
  301.     }
  302.  
  303.     // check for collision at ball's bottom-left corner
  304.     object = getGObjectAt(window, x, y + 2 * RADIUS );
  305.     if (object != NULL)
  306.     {
  307.         return object;
  308.     }
  309.  
  310.     // check for collision at ball's bottom-right corner
  311.     object = getGObjectAt(window, x + 2 * RADIUS, y + 2 * RADIUS );
  312.     if (object != NULL)
  313.     {
  314.         return object;
  315.     }
  316.  
  317.     // no collision
  318.     return NULL;
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement