rahulb5

pset4

Jun 20th, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.90 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. double xvelocity = 3.0 ;
  84. double yvelocity = 3.0 ;
  85. waitForClick();
  86. while (lives > 0 && bricks > 0)
  87. {
  88.  
  89. // TODO
  90. GEvent event = getNextEvent(MOUSE_EVENT);
  91.  
  92. // if we heard one
  93. if (event != NULL)
  94. {
  95. // if the event was movement
  96. if (getEventType(event) == MOUSE_MOVED)
  97. {
  98. // ensure circle follows top cursor
  99. double x = getX(event);
  100. if(x > (getWidth(window) - wp))
  101. setLocation(paddle , (getWidth(window) - wp) , yp);
  102. else
  103. setLocation(paddle, x, yp);
  104.  
  105.  
  106. }
  107. }
  108.  
  109. move(ball , xvelocity ,yvelocity);
  110.  
  111.  
  112.  
  113.  
  114. if (getX(ball) + getWidth(ball) >= getWidth(window))
  115. xvelocity = -xvelocity;
  116. else if (getX(ball) <= 0)
  117. xvelocity = -xvelocity;
  118.  
  119.  
  120. else if(getY(ball) <= 0)
  121. yvelocity = -yvelocity ;
  122.  
  123. updateScoreboard(window, label, points);
  124.  
  125. GObject object = detectCollision(window , ball);
  126. if (object != NULL)
  127. {
  128. // If the ball hits the paddle.
  129. if (object == paddle)
  130. {
  131. yvelocity = -yvelocity;
  132. }
  133.  
  134. // If the ball hits a block. Remove block, add a point, decrement count and bounce.
  135. else if (strcmp(getType(object), "GRect") == 0)
  136. {
  137. removeGWindow(window, object);
  138. yvelocity = -yvelocity;
  139. points++;
  140. bricks--;
  141. }
  142. }
  143.  
  144.  
  145.  
  146. if(getY(ball) + getHeight(ball) >= getHeight(window) )
  147. {
  148. lives-- ;
  149. setLocation(ball , 190 , 290 );
  150. setLocation(paddle , xp ,yp );
  151. pause(1000);
  152. }
  153. pause(10);
  154. }
  155.  
  156. // wait for click before exiting
  157. waitForClick();
  158.  
  159. // game over
  160. closeGWindow(window);
  161. return 0;
  162. }
  163.  
  164. /**
  165. * Initializes window with a grid of bricks.
  166. */
  167. void initBricks(GWindow window)
  168. {
  169. //TODO
  170. int ysb = 3 ;
  171. int xsb = 5 ;
  172. for (int i = 0 ; i < ROWS ; i++ )
  173. {
  174. for (int j = 0 ; j < COLS ; j++ )
  175. {
  176. GRect brick = newGRect(xsb , ysb , xb , yb);
  177. if(i == 0)
  178. setColor(brick , "RED" );
  179. if(i == 1)
  180. setColor(brick , "ORANGE");
  181. if(i == 2)
  182. setColor(brick ,"YELLOW" );
  183. if(i == 3)
  184. setColor(brick ,"GREEN" );
  185. if(i == 4)
  186. setColor(brick , "BLUE");
  187.  
  188. setFilled(brick , true);
  189. add(window , brick );
  190. xsb = xsb + 39 ;
  191. printf("\n");
  192. }
  193. xsb = 5 ;
  194. ysb = ysb + 15 ;
  195. }
  196.  
  197. }
  198.  
  199. /**
  200. * Instantiates ball in center of window. Returns ball.
  201. */
  202. GOval initBall(GWindow window)
  203. {
  204. // TODO
  205. GOval ball = newGOval( 190 , 290 , 20 , 20 );
  206. setColor(ball , "BLACK");
  207. setFilled(ball , true);
  208. add(window , ball);
  209. return ball;
  210. }
  211.  
  212. /**
  213. * Instantiates paddle in bottom-middle of window.
  214. */
  215. GRect initPaddle(GWindow window)
  216. {
  217. // TODO
  218.  
  219. GRect paddle = newGRect(xp , yp , wp , hp );
  220. setFilled(paddle, true);
  221. setColor(paddle, "RED");
  222.  
  223. add(window, paddle);
  224. return paddle;
  225. }
  226.  
  227. /**
  228. * Instantiates, configures, and returns label for scoreboard.
  229. */
  230. GLabel initScoreboard(GWindow window)
  231. {
  232. // TODO
  233. GLabel points = newGLabel("0");
  234. setLocation(points , 200 ,300 );
  235. setFilled(points , true);
  236. setColor(points , "GREY");
  237.  
  238. add(window ,points);
  239. return points ;
  240. }
  241.  
  242. /**
  243. * Updates scoreboard's label, keeping it centered in window.
  244. */
  245. void updateScoreboard(GWindow window, GLabel label, int points)
  246. {
  247. // update label
  248. char s[12];
  249. sprintf(s, "%i", points);
  250. setLabel(label, s);
  251.  
  252. // center label in window
  253. double x = (getWidth(window) - getWidth(label)) / 2;
  254. double y = (getHeight(window) - getHeight(label)) / 2;
  255. setLocation(label, x, y);
  256. }
  257.  
  258. /**
  259. * Detects whether ball has collided with some object in window
  260. * by checking the four corners of its bounding box (which are
  261. * outside the ball's GOval, and so the ball can't collide with
  262. * itself). Returns object if so, else NULL.
  263. */
  264. GObject detectCollision(GWindow window, GOval ball)
  265. {
  266. // ball's location
  267. double x = getX(ball);
  268. double y = getY(ball);
  269.  
  270. // for checking for collisions
  271. GObject object;
  272.  
  273. // check for collision at ball's top-left corner
  274. object = getGObjectAt(window, x, y);
  275. if (object != NULL)
  276. {
  277. return object;
  278. }
  279.  
  280. // check for collision at ball's top-right corner
  281. object = getGObjectAt(window, x + 2 * RADIUS, y);
  282. if (object != NULL)
  283. {
  284. return object;
  285. }
  286.  
  287. // check for collision at ball's bottom-left corner
  288. object = getGObjectAt(window, x, y + 2 * RADIUS);
  289. if (object != NULL)
  290. {
  291. return object;
  292. }
  293.  
  294. // check for collision at ball's bottom-right corner
  295. object = getGObjectAt(window, x + 2 * RADIUS, y + 2 * RADIUS);
  296. if (object != NULL)
  297. {
  298. return object;
  299. }
  300.  
  301. // no collision
  302. return NULL;
  303. }
Advertisement
Add Comment
Please, Sign In to add comment