Advertisement
Eastkap

CS50 Pset4 Breakout Prototype1

Oct 6th, 2014
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.95 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. //height and width of paddle in pixels
  25. #define PH 20
  26. #define PW 80
  27.  
  28. // h and w of ball in pixel
  29. #define bal 15
  30.  
  31. // number of rows of bricks
  32. #define ROWS 5
  33.  
  34. // number of columns of bricks
  35. #define COLS 10
  36.  
  37. // radius of ball in pixels
  38. #define RADIUS 10
  39.  
  40. // lives
  41. #define LIVES 3
  42.  
  43. // prototypes
  44. void initBricks(GWindow window);
  45. GOval initBall(GWindow window);
  46. GRect initPaddle(GWindow window);
  47. GLabel initScoreboard(GWindow window);
  48. void updateScoreboard(GWindow window, GLabel label, int points);
  49. GObject detectCollision(GWindow window, GOval ball);
  50.  
  51. int main(void)
  52. {
  53.     // seed pseudorandom number generator
  54.     srand48(time(NULL));
  55.    
  56.  
  57.     // instantiate window
  58.     GWindow window = newGWindow(WIDTH, HEIGHT);
  59.  
  60.     // instantiate bricks
  61.     initBricks(window);
  62.  
  63.     // instantiate ball, centered in middle of window
  64.     GOval ball = initBall(window);
  65.  
  66.     // instantiate paddle, centered at bottom of window
  67.     GRect paddle = initPaddle(window);
  68.  
  69.     // instantiate scoreboard, centered in middle of window, just above ball
  70.     GLabel label = initScoreboard(window);
  71.  
  72.     // number of bricks initially
  73.     int bricks = COLS * ROWS;
  74.  
  75.     // number of lives initially
  76.     int lives = LIVES;
  77.  
  78.     // number of points initially
  79.     int points = 0;
  80.     int runtime=0;
  81.     //initial velocity-x started
  82.     double velocitx;
  83.     double velocity;
  84.    
  85.  
  86.     // keep playing until game over
  87.     while (lives > 0 && bricks > 0)
  88.     {
  89.         updateScoreboard(window,label, points);
  90.         //checking if its the 1st time the ball moves after losing
  91.         if(runtime==0)
  92.         {
  93.             velocitx= 2.0;
  94.             velocity =drand48()+2;
  95.             waitForClick();
  96.         }
  97.         move(ball, velocitx, velocity);
  98.         // TODO-paddle move-done-ball move to be improved(can hit several bricks in a row, in a strange way)
  99.         GEvent event = getNextEvent(MOUSE_EVENT);
  100.        
  101.         // to know if we have 2 wait for click to move the ball
  102.         runtime=runtime+1;
  103.  
  104.         // if event
  105.         if (event != NULL)
  106.         {
  107.             // if event movement
  108.             if (getEventType(event) == MOUSE_MOVED)
  109.             {
  110.                 // move
  111.                 double x = getX(event) - getWidth(paddle) / 2;
  112.                 //let's check it the paddle isn't going out of the screen
  113.                 if (x>0 && x<WIDTH-PW)
  114.                 {
  115.                     setLocation(paddle, x, HEIGHT-60-PH/2);
  116.                 }
  117.             }
  118.         }
  119.        
  120.  
  121.         // bounce off right edge of window
  122.         if (getX(ball) + getWidth(ball) >= getWidth(window))
  123.         {
  124.             velocitx = -velocitx;
  125.         }
  126.         else if (getX(ball) <= 0)
  127.         {
  128.             velocitx = -velocitx;
  129.         }
  130.        
  131.         if (getY(ball)<=0)
  132.         {
  133.             velocity=-velocity;
  134.         }
  135.         else if(getY(ball) + getHeight(ball) >= getHeight(window) && runtime!=0)
  136.         {
  137.             lives=lives-1;
  138.             runtime=-1;
  139.             while(getY(ball)+bal<HEIGHT)
  140.             {}
  141.             removeGWindow(window,ball);
  142.             pause(20);
  143.             GOval ball = initBall(window);
  144.         }
  145.         //lets talk collision now
  146.         GObject object = detectCollision(window, ball);
  147.        
  148.         if(object!=NULL)
  149.         {
  150.             if (strcmp(getType(object), "GRect") == 0)
  151.             {
  152.                 if (object==paddle)
  153.                 {
  154.                     velocity=-velocity;
  155.                 }
  156.                 else
  157.                 {
  158.                     points=points+1;
  159.                     bricks=bricks-1;
  160.                     removeGWindow(window,object);
  161.                     if(velocity<0)
  162.                     {
  163.                         velocity=-velocity;
  164.                     }
  165.                     else
  166.                     {
  167.                         velocitx=-velocitx;
  168.                     }
  169.                 }
  170.             }
  171.         }
  172.        
  173.         // linger before moving again
  174.         pause(15);
  175.        
  176.     }
  177.     if (bricks==0)
  178.     {
  179.         printf("You won! Thanks for playing!\n");
  180.     }
  181.     if(lives==0)
  182.     {
  183.         printf("You lost! Try playing again!\n");
  184.     }
  185.  
  186.     // wait for click before exiting
  187.     waitForClick();
  188.  
  189.     // game over
  190.     closeGWindow(window);
  191.     return 0;
  192. }
  193.  
  194. /**
  195.  * Initializes window with a grid of bricks.
  196.  */
  197. void initBricks(GWindow window)
  198. {
  199.     // TODO-done
  200.     for (int i=0; i<ROWS;i++)
  201.     {
  202.         for (int j=0; j<COLS; j++)
  203.         {
  204.             GRect brick = newGRect(5+j*39,60+20*i,35,10);
  205.             setFilled(brick, true);
  206.             if (i==0)
  207.             {
  208.                 setColor(brick,"RED");
  209.             }
  210.             else if (i==1)
  211.             {
  212.                 setColor(brick,"YELLOW");
  213.             }
  214.             else if (i==2)
  215.             {
  216.                 setColor(brick,"BLUE");
  217.             }
  218.             else if (i==3)
  219.             {
  220.                 setColor(brick,"GREEN");
  221.             }
  222.             else
  223.             {
  224.                 setColor(brick, "PINK");
  225.             }
  226.             add (window, brick);
  227.         }
  228.     }
  229. }
  230.  
  231. /**
  232.  * Instantiates ball in center of window.  Returns ball.
  233.  */
  234. GOval initBall(GWindow window)
  235. {
  236.     // TODO-done
  237.     GOval ball = newGOval (WIDTH/2-bal/2,HEIGHT/2,bal,bal);
  238.     setFilled(ball,true);
  239.     setColor(ball, "BLACK");
  240.     add(window, ball);
  241.     return ball;
  242. }
  243.  
  244. /**
  245.  * Instantiates paddle in bottom-middle of window.
  246.  */
  247. GRect initPaddle(GWindow window)
  248. {
  249.     // TODO-Done
  250.     //i want the paddle to be at the bottom but not too much and centered
  251.     GRect Paddle = newGRect(WIDTH/2-PW/2,HEIGHT-60-PH/2,PW,PH);
  252.     setFilled(Paddle,true);
  253.     setColor(Paddle, "BLACK");
  254.     add(window, Paddle);
  255.     return Paddle;
  256. }
  257.  
  258. /**
  259.  * Instantiates, configures, and returns label for scoreboard.
  260.  */
  261. GLabel initScoreboard(GWindow window)
  262. {
  263.     // TODO-done
  264.     GLabel label = newGLabel("");
  265.     setFont(label, "SansSerif-36");
  266.     add(window, label);
  267.     return label;
  268. }
  269.  
  270. /**
  271.  * Updates scoreboard's label, keeping it centered in window.
  272.  */
  273. void updateScoreboard(GWindow window, GLabel label, int points)
  274. {
  275.     // update label
  276.     char s[12];
  277.     sprintf(s, "%i", points);
  278.     setLabel(label, s);
  279.  
  280.     // center label in window
  281.     double x = (getWidth(window) - getWidth(label)) / 2;
  282.     double y = (getHeight(window) - getHeight(label)) / 2;
  283.     setLocation(label, x, y);
  284. }
  285.  
  286. /**
  287.  * Detects whether ball has collided with some object in window
  288.  * by checking the four corners of its bounding box (which are
  289.  * outside the ball's GOval, and so the ball can't collide with
  290.  * itself).  Returns object if so, else NULL.
  291.  */
  292. GObject detectCollision(GWindow window, GOval ball)
  293. {
  294.     // ball's location
  295.     double x = getX(ball);
  296.     double y = getY(ball);
  297.  
  298.     // for checking for collisions
  299.     GObject object;
  300.  
  301.     // check for collision at ball's top-left corner
  302.     object = getGObjectAt(window, x, y);
  303.     if (object != NULL)
  304.     {
  305.         return object;
  306.     }
  307.  
  308.     // check for collision at ball's top-right corner
  309.     object = getGObjectAt(window, x + 2 * RADIUS, y);
  310.     if (object != NULL)
  311.     {
  312.         return object;
  313.     }
  314.  
  315.     // check for collision at ball's bottom-left corner
  316.     object = getGObjectAt(window, x, y + 2 * RADIUS);
  317.     if (object != NULL)
  318.     {
  319.         return object;
  320.     }
  321.  
  322.     // check for collision at ball's bottom-right corner
  323.     object = getGObjectAt(window, x + 2 * RADIUS, y + 2 * RADIUS);
  324.     if (object != NULL)
  325.     {
  326.         return object;
  327.     }
  328.  
  329.     // no collision
  330.     return NULL;
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement