Advertisement
Eastkap

CS50 Pset4 Breakout Prototype2

Oct 6th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.92 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=0;
  139.             while(getY(ball)+bal<HEIGHT)
  140.             {}
  141.             setLocation(ball,WIDTH/2-bal/2,HEIGHT/2);
  142.             pause(20);
  143.         }
  144.         //lets talk collision now
  145.         GObject object = detectCollision(window, ball);
  146.        
  147.         if(object!=NULL)
  148.         {
  149.             if (strcmp(getType(object), "GRect") == 0)
  150.             {
  151.                 if (object==paddle)
  152.                 {
  153.                     velocity=-velocity;
  154.                 }
  155.                 else
  156.                 {
  157.                     points=points+1;
  158.                     bricks=bricks-1;
  159.                     removeGWindow(window,object);
  160.                     if(velocity<0)
  161.                     {
  162.                         velocity=-velocity;
  163.                     }
  164.                     else
  165.                     {
  166.                         velocitx=-velocitx;
  167.                     }
  168.                 }
  169.             }
  170.         }
  171.        
  172.         // linger before moving again
  173.         pause(15);
  174.        
  175.     }
  176.     if (bricks==0)
  177.     {
  178.         printf("You won! Thanks for playing!\n");
  179.     }
  180.     if(lives==0)
  181.     {
  182.         printf("You lost! Try playing again!\n");
  183.     }
  184.  
  185.     // wait for click before exiting
  186.     waitForClick();
  187.  
  188.     // game over
  189.     closeGWindow(window);
  190.     return 0;
  191. }
  192.  
  193. /**
  194.  * Initializes window with a grid of bricks.
  195.  */
  196. void initBricks(GWindow window)
  197. {
  198.     // TODO-done
  199.     for (int i=0; i<ROWS;i++)
  200.     {
  201.         for (int j=0; j<COLS; j++)
  202.         {
  203.             GRect brick = newGRect(5+j*39,60+20*i,35,10);
  204.             setFilled(brick, true);
  205.             if (i==0)
  206.             {
  207.                 setColor(brick,"RED");
  208.             }
  209.             else if (i==1)
  210.             {
  211.                 setColor(brick,"YELLOW");
  212.             }
  213.             else if (i==2)
  214.             {
  215.                 setColor(brick,"BLUE");
  216.             }
  217.             else if (i==3)
  218.             {
  219.                 setColor(brick,"GREEN");
  220.             }
  221.             else
  222.             {
  223.                 setColor(brick, "PINK");
  224.             }
  225.             add (window, brick);
  226.         }
  227.     }
  228. }
  229.  
  230. /**
  231.  * Instantiates ball in center of window.  Returns ball.
  232.  */
  233. GOval initBall(GWindow window)
  234. {
  235.     // TODO-done
  236.     GOval ball = newGOval (WIDTH/2-bal/2,HEIGHT/2,bal,bal);
  237.     setFilled(ball,true);
  238.     setColor(ball, "BLACK");
  239.     add(window, ball);
  240.     return ball;
  241. }
  242.  
  243. /**
  244.  * Instantiates paddle in bottom-middle of window.
  245.  */
  246. GRect initPaddle(GWindow window)
  247. {
  248.     // TODO-Done
  249.     //i want the paddle to be at the bottom but not too much and centered
  250.     GRect Paddle = newGRect(WIDTH/2-PW/2,HEIGHT-60-PH/2,PW,PH);
  251.     setFilled(Paddle,true);
  252.     setColor(Paddle, "BLACK");
  253.     add(window, Paddle);
  254.     return Paddle;
  255. }
  256.  
  257. /**
  258.  * Instantiates, configures, and returns label for scoreboard.
  259.  */
  260. GLabel initScoreboard(GWindow window)
  261. {
  262.     // TODO-done
  263.     GLabel label = newGLabel("");
  264.     setFont(label, "SansSerif-36");
  265.     add(window, label);
  266.     return label;
  267. }
  268.  
  269. /**
  270.  * Updates scoreboard's label, keeping it centered in window.
  271.  */
  272. void updateScoreboard(GWindow window, GLabel label, int points)
  273. {
  274.     // update label
  275.     char s[12];
  276.     sprintf(s, "%i", points);
  277.     setLabel(label, s);
  278.  
  279.     // center label in window
  280.     double x = (getWidth(window) - getWidth(label)) / 2;
  281.     double y = (getHeight(window) - getHeight(label)) / 2;
  282.     setLocation(label, x, y);
  283. }
  284.  
  285. /**
  286.  * Detects whether ball has collided with some object in window
  287.  * by checking the four corners of its bounding box (which are
  288.  * outside the ball's GOval, and so the ball can't collide with
  289.  * itself).  Returns object if so, else NULL.
  290.  */
  291. GObject detectCollision(GWindow window, GOval ball)
  292. {
  293.     // ball's location
  294.     double x = getX(ball);
  295.     double y = getY(ball);
  296.  
  297.     // for checking for collisions
  298.     GObject object;
  299.  
  300.     // check for collision at ball's top-left corner
  301.     object = getGObjectAt(window, x, y);
  302.     if (object != NULL)
  303.     {
  304.         return object;
  305.     }
  306.  
  307.     // check for collision at ball's top-right corner
  308.     object = getGObjectAt(window, x + 2 * RADIUS, y);
  309.     if (object != NULL)
  310.     {
  311.         return object;
  312.     }
  313.  
  314.     // check for collision at ball's bottom-left corner
  315.     object = getGObjectAt(window, x, y + 2 * RADIUS);
  316.     if (object != NULL)
  317.     {
  318.         return object;
  319.     }
  320.  
  321.     // check for collision at ball's bottom-right corner
  322.     object = getGObjectAt(window, x + 2 * RADIUS, y + 2 * RADIUS);
  323.     if (object != NULL)
  324.     {
  325.         return object;
  326.     }
  327.  
  328.     // no collision
  329.     return NULL;
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement