rahulb5

Untitled

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