Advertisement
Guest User

new improved breakout

a guest
Aug 4th, 2015
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.40 KB | None | 0 0
  1. //
  2. // breakout.c
  3. //
  4. // Computer Science 50
  5. // Problem Set 3
  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 <spl/gevents.h>
  17. #include <spl/gobjects.h>
  18. #include <spl/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 10
  32.  
  33. // lives
  34. #define LIVES 3
  35.  
  36. // prototypes
  37. void initBricks(GWindow window);
  38. GOval initBall(GWindow window);
  39. GRect initPaddle(GWindow window);
  40. GLabel initScoreboard(GWindow window);
  41. void updateScoreboard(GWindow window, GLabel label, int points);
  42. GObject detectCollision(GWindow window, GOval ball);
  43.  
  44. int main(void)
  45. {
  46. // seed pseudorandom number generator
  47. srand48(time(NULL));
  48.  
  49. // instantiate window
  50. GWindow window = newGWindow(WIDTH, HEIGHT);
  51.  
  52. // instantiate bricks
  53. initBricks(window);
  54.  
  55. // instantiate ball, centered in middle of window
  56. GOval ball = initBall(window);
  57.  
  58. // instantiate paddle, centered at bottom of window
  59. GRect paddle = initPaddle(window);
  60.  
  61. // instantiate scoreboard, centered in middle of window, just above ball
  62. GLabel label = initScoreboard(window);
  63.  
  64. // number of bricks initially
  65. int bricks = COLS * ROWS;
  66.  
  67. // number of lives initially
  68. int lives = LIVES;
  69.  
  70. // number of points initially
  71. int points = 0;
  72.  
  73. // velocity of balls
  74. double velocity = drand48()*rand();
  75.  
  76. // keep playing until game over
  77. while (lives > 0 && bricks > 0)
  78. {
  79. // TODO
  80. int x = 185, y = 285;
  81. // updating score board..
  82. updateScoreboard(window, label, points);
  83.  
  84. // start moving ball..
  85. move(ball, velocity, velocity);
  86. pause(10);
  87.  
  88. GEvent event = getNextEvent(MOUSE_EVENT);
  89. if (event != NULL)
  90. {
  91. if (getEventType(event) == MOUSE_MOVED)
  92. {
  93. double x = getX(event) - getWidth(paddle)/2;
  94. setLocation(paddle, x, 550);
  95. }
  96. }
  97.  
  98. // bounce back the ball if it bounce back to edges, top, brick, paddle, etc...
  99. if (getX(ball) + getWidth(ball) == getWidth(window))
  100. {
  101. velocity = -velocity;
  102. }
  103. else if (getX(ball) == 0)
  104. {
  105. velocity = -velocity;
  106. }
  107. else if (getY(ball) + getHeight(ball) > getHeight(window) - getHeight(paddle))
  108. {
  109. lives--;
  110. setLocation(ball, 193, 293);
  111. waitForClick();
  112. }
  113. else if (getY(ball) == 0)
  114. {
  115. velocity = -velocity;
  116. }
  117.  
  118. // detect collision..
  119. GObject object = detectCollision(window, ball);
  120. if (object != NULL)
  121. {
  122. if (object == paddle)
  123. {
  124. velocity = -velocity;
  125. }
  126. else if (strcmp(getType(object), "GRect") == 0)
  127. {
  128. updateScoreboard(window, label, points);
  129. removeGWindow(window, "GRect");
  130. velocity = -velocity;
  131. points++;
  132. bricks--;
  133. }
  134. else if (strcmp(getType(label), "GLabel") == 0)
  135. {
  136. return 0;
  137. }
  138. }
  139. }
  140.  
  141. if ((lives == 0) && (bricks > 0))
  142. {
  143. GLabel lost = newGLabel("Sorry!!\n\t\t\t\t YOU LOST :(");
  144. setFont(lost, "Arial-30");
  145. setLocation(lost, 185, 285);
  146. add(window, lost);
  147. }
  148. else if (bricks == 0)
  149. {
  150. GLabel lost = newGLabel("HURRAH!!!\n\t\t\t\t YOU WON :)");
  151. setFont(lost, "Arial-30");
  152. setLocation(lost, 185, 285);
  153. add(window, lost);
  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. // Making if and else conditions only for different colors in rows..
  170. int y=10;
  171. for (int i=0; i<ROWS; i++)
  172. {
  173. int x=10;
  174. if (i == 0)
  175. {
  176. for (int j=0; j<COLS; j++)
  177. {
  178. GRect bricks = newGRect(x, y, 30, 8);
  179. setFilled(bricks, true);
  180. setColor(bricks, "RED");
  181. add(window, bricks);
  182. x=x+38;
  183. }
  184. printf("\n");
  185. }
  186. else if (i == 1)
  187. {
  188. for (int j=0; j<COLS; j++)
  189. {
  190. GRect bricks = newGRect(x, y, 30, 8);
  191. setFilled(bricks, true);
  192. setColor(bricks, "BLUE");
  193. add(window, bricks);
  194. x=x+38;
  195. }
  196. printf("\n");
  197. }
  198. else if (i == 2)
  199. {
  200. for (int j=0; j<COLS; j++)
  201. {
  202. GRect bricks = newGRect(x, y, 30, 8);
  203. setFilled(bricks, true);
  204. setColor(bricks, "GREEN");
  205. add(window, bricks);
  206. x=x+38;
  207. }
  208. printf("\n");
  209. }
  210. else if (i == 3)
  211. {
  212. for (int j=0; j<COLS; j++)
  213. {
  214. GRect bricks = newGRect(x, y, 30, 8);
  215. setFilled(bricks, true);
  216. setColor(bricks, "YELLOW");
  217. add(window, bricks);
  218. x=x+38;
  219. }
  220. printf("\n");
  221. }
  222. else if (i == 4)
  223. {
  224. for (int j=0; j<COLS; j++)
  225. {
  226. GRect bricks = newGRect(x, y, 30, 8);
  227. setFilled(bricks, true);
  228. setColor(bricks, "BLUE");
  229. add(window, bricks);
  230. x=x+38;
  231. }
  232. printf("\n");
  233. }
  234. y=y+15;
  235. }
  236. }
  237.  
  238. /**
  239. * Instantiates ball in center of window. Returns ball.
  240. */
  241. GOval initBall(GWindow window)
  242. {
  243. GOval ball = newGOval(185, 285, RADIUS*2, RADIUS*2);
  244. setFilled(ball, true);
  245. setColor(ball, "BLACK");
  246. add(window, ball);
  247. return ball;
  248. }
  249.  
  250. /**
  251. * Instantiates paddle in bottom-middle of window.
  252. */
  253. GRect initPaddle(GWindow window)
  254. {
  255. GRect paddle = newGRect(165,550,50,12);
  256. setFilled(paddle, true);
  257. setColor(paddle, "BLACK");
  258. add(window, paddle);
  259. return paddle;
  260. }
  261.  
  262. /**
  263. * Instantiates, configures, and returns label for scoreboard.
  264. */
  265. GLabel initScoreboard(GWindow window)
  266. {
  267. GLabel label = newGLabel("");
  268. setFont(label, "SansSerif-40");
  269. double x= (getWidth(WIDTH) - getWidth(label))/2;
  270. double y= (getHeight(HEIGHT) + getFontAscent(label))/2;
  271. setLocation(label, x, y);
  272. return label;
  273. }
  274.  
  275. /**
  276. * Updates scoreboard's label, keeping it centered in window.
  277. */
  278. void updateScoreboard(GWindow window, GLabel label, int points)
  279. {
  280. // update label
  281. char s[12];
  282. sprintf(s, "%i", points);
  283. setLabel(label, s);
  284.  
  285. // center label in window
  286. double x = (getWidth(window) - getWidth(label)) / 2;
  287. double y = (getHeight(window) - getHeight(label)) / 2;
  288. setLocation(label, x, y);
  289. }
  290.  
  291. /**
  292. * Detects whether ball has collided with some object in window
  293. * by checking the four corners of its bounding box (which are
  294. * outside the ball's GOval, and so the ball can't collide with
  295. * itself). Returns object if so, else NULL.
  296. */
  297. GObject detectCollision(GWindow window, GOval ball)
  298. {
  299. // ball's location
  300. double x = getX(ball);
  301. double y = getY(ball);
  302.  
  303. // for checking for collisions
  304. GObject object;
  305.  
  306. // check for collision at ball's top-left corner
  307. object = getGObjectAt(window, x, y);
  308. if (object != NULL)
  309. {
  310. return object;
  311. }
  312.  
  313. // check for collision at ball's top-right corner
  314. object = getGObjectAt(window, x + 2 * RADIUS, y);
  315. if (object != NULL)
  316. {
  317. return object;
  318. }
  319.  
  320. // check for collision at ball's bottom-left corner
  321. object = getGObjectAt(window, x, y + 2 * RADIUS);
  322. if (object != NULL)
  323. {
  324. return object;
  325. }
  326.  
  327. // check for collision at ball's bottom-right corner
  328. object = getGObjectAt(window, x + 2 * RADIUS, y + 2 * RADIUS);
  329. if (object != NULL)
  330. {
  331. return object;
  332. }
  333.  
  334. // no collision
  335. return NULL;
  336. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement