/
breakout.c
337 lines (282 loc) · 7.77 KB
/
breakout.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//
// breakout.c
//
// Chashmeet Singh
//
// CS 50
//
// standard libraries
#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// Stanford Portable Library
#include "spl/gevents.h"
#include "spl/gobjects.h"
#include "spl/gwindow.h"
// height and width of game's window in pixels
#define HEIGHT 600
#define WIDTH 400
// number of rows of bricks
#define ROWS 5
// number of columns of bricks
#define COLS 10
// radius of ball in pixels
#define RADIUS 10
// lives
#define LIVES 3
// prototypes
void initBricks(GWindow window);
GOval initBall(GWindow window);
GRect initPaddle(GWindow window);
GLabel initScoreboard(GWindow window);
void updateScoreboard(GWindow window, GLabel label, int points);
GObject detectCollision(GWindow window, GOval ball);
double velocityX;
double velocityY;
int main(void)
{
// instantiate window
GWindow window = newGWindow(WIDTH, HEIGHT);
// instantiate bricks
initBricks(window);
// instantiate ball, centered in middle of window
GOval ball = initBall(window);
// instantiate paddle, centered at bottom of window
GRect paddle = initPaddle(window);
// instantiate scoreboard, centered in middle of window, just above ball
GLabel label = initScoreboard(window);
// number of bricks initially
int bricks = COLS * ROWS;
// number of lives initially
int lives = LIVES;
// number of points initially
int points = 0;
// wait for click before starting
waitForClick();
velocityX = 1.0;
velocityY = 2.5;
// keep playing until game over
while (lives > 0 && bricks > 0)
{
// Scoreboard
updateScoreboard(window, label, points);
// move ball
move(ball, velocityX, velocityY);
pause(10);
// check for mouse event.
GEvent event = getNextEvent(MOUSE_EVENT);
// Lock the paddle X to the cursor.
if (event != NULL)
{
// if the event was movement
if (getEventType(event) == MOUSE_MOVED)
{
// ensure paddle follows top cursor
double x = getX(event) - getWidth(paddle) / 2;
double y = 500;
setLocation(paddle, x, y);
}
}
GObject object = detectCollision(window, ball);
if (object != NULL)
{
// If the ball hits the paddle.
if (object == paddle)
{
velocityY = -velocityY;
}
// If the ball hits a block. Remove block, add a point, decrement count and bounce.
else if (strcmp(getType(object), "GRect") == 0)
{
removeGWindow(window, object);
velocityY = -velocityY;
points++;
bricks--;
}
}
// If the ball hits the right wall.
if (getX(ball) + getWidth(ball) >= getWidth(window))
{
velocityX = -velocityX;
}
// If the ball hits the left wall.
if (getX(ball) <= 0)
{
velocityX = -velocityX;
}
// If the ball hits the top wall.
if (getY(ball) <= 0)
{
velocityY = -velocityY;
}
// Remove a life. Start over.
if (getY(ball) + getHeight(ball) >= getHeight(window))
{
lives--;
//move ball to start
setLocation(ball, 190, 200);
//move paddle to start
setLocation(paddle, 160, 500);
waitForClick();
}
}
// You Lose Label Message for kicks.
if (bricks > 0)
{
GLabel game_over = newGLabel("YOU LOSE!");
setFont(game_over, "SansSerif-70");
setColor(game_over, "RED");
add(window, game_over);
setLocation(game_over, 15, 300);
}
else
{
GLabel game_over = newGLabel("YOU WIN!");
setFont(game_over, "SansSerif-70");
setColor(game_over, "GREEN");
add(window, game_over);
setLocation(game_over, 15, 300);
}
// wait for click before exiting
waitForClick();
// game over
closeGWindow(window);
return 0;
}
/**
* Initializes window with a grid of bricks.
*/
void initBricks(GWindow window)
{
// initial starting Y position of the bricks.
int brick_y = 50;
// the space between each line.
int row_space = 20;
// 5 rows of bricks.
for (int i = 0; i < ROWS; i++)
{
// initial starting X pos.
int brick_x = 2;
//10 bricks per row.
for (int j = 0; j < COLS; j++)
{
GRect brick = newGRect(brick_x + 5, brick_y, 35, 10);
// Setting colors based on row. Not elegant.
if (i == 0)
{
setColor(brick, "RED");
}
if (i == 1)
{
setColor(brick, "ORANGE");
}
if (i == 2)
{
setColor(brick, "YELLOW");
}
if (i == 3)
{
setColor(brick, "GREEN");
}
if (i == 4)
{
setColor(brick, "BLUE");
}
setFilled(brick, true);
add(window, brick);
brick_x = brick_x + 39;
}
brick_y = brick_y + 15;
}
}
/**
* Instantiates ball in center of window. Returns ball.
*/
GOval initBall(GWindow window)
{
GOval ball = newGOval(200 - 10, 200, 20, 20);
setColor(ball, "BLACK");
setFilled(ball, true);
add(window, ball);
return ball;
}
/**
* Instantiates paddle in bottom-middle of window.
*/
GRect initPaddle(GWindow window)
{
GRect paddle = newGRect(200 - 35, 500, 70, 10);
// purple color
setColor(paddle, "BLUE");
setFilled(paddle, true);
add(window, paddle);
return paddle;
}
/**
* Instantiates, configures, and returns label for scoreboard.
*/
GLabel initScoreboard(GWindow window)
{
GLabel label = newGLabel(" ");
setFont(label, "SansSerif-50");
setColor(label, "C0C0C0");
add(window, label);
setLocation(label, 185, 300);
return label;
}
/**
* Updates scoreboard's label, keeping it centered in window.
*/
void updateScoreboard(GWindow window, GLabel label, int points)
{
// update label
char s[12];
sprintf(s, "%i", points);
setLabel(label, s);
// center label in window
double x = (getWidth(window) - getWidth(label)) / 2;
double y = (getHeight(window) - getHeight(label)) / 2;
setLocation(label, x, y);
}
/**
* Detects whether ball has collided with some object in window
* by checking the four corners of its bounding box (which are
* outside the ball's GOval, and so the ball can't collide with
* itself). Returns object if so, else NULL.
*/
GObject detectCollision(GWindow window, GOval ball)
{
// ball's location
double x = getX(ball);
double y = getY(ball);
// for checking for collisions
GObject object;
// check for collision at ball's top-left corner
object = getGObjectAt(window, x, y);
if (object != NULL)
{
return object;
}
// check for collision at ball's top-right corner
object = getGObjectAt(window, x + 2 * RADIUS, y);
if (object != NULL)
{
return object;
}
// check for collision at ball's bottom-left corner
object = getGObjectAt(window, x, y + 2 * RADIUS);
if (object != NULL)
{
return object;
}
// check for collision at ball's bottom-right corner
object = getGObjectAt(window, x + 2 * RADIUS, y + 2 * RADIUS);
if (object != NULL)
{
return object;
}
// no collision
return NULL;
}