-
Notifications
You must be signed in to change notification settings - Fork 0
/
breakout.c
342 lines (258 loc) · 7.06 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
338
339
340
341
342
//
// breakout.c
//
// Computer Science 50
// Problem Set 3
//
// 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
// define height and width of rectangle
#define paddleHeight 20
#define paddleWidth 80
// prototypes
void removeGWindow(GWindow gw, GObject gobj);
void initBricks(GWindow window);
GOval initBall(GWindow window);
GRect initPaddle(GWindow window);
double velocity_x;
double velocity_y;
GLabel initScoreboard(GWindow window);
void updateScoreboard(GWindow window, GLabel label, int points);
GObject detectCollision(GWindow window, GOval ball);
int main(void)
{
// seed pseudorandom number generator
srand48(time(NULL));
// 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;
velocity_x = 1.5;
velocity_y = 3.0;
waitForClick();
// keep playing until game over
while (lives > 0 && bricks > 0)
{
updateScoreboard(window, label, points);
// TODO
move(ball, velocity_x, velocity_y);
pause(10);
GEvent event = getNextEvent(MOUSE_EVENT);
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 = HEIGHT-paddleHeight;
setLocation(paddle, x, y);
}
}
GObject object = detectCollision(window, ball);
if (object == paddle)
{
velocity_y = -velocity_y;
}
else if (strcmp(getType(object), "GRect") == 0)
{
removeGWindow(window, object);
velocity_y = -velocity_y;
bricks--;
points++;
}
if (getX(ball) + getWidth(ball) >= getWidth(window))
{
velocity_x = -velocity_x;
}
else if (getX(ball) <= 0)
{
velocity_x = -velocity_x;
}
else if(getY(ball) + getHeight(ball) >= getHeight(window))
{
lives = lives - 1;
setLocation(ball, (WIDTH/2), (HEIGHT/2));
setLocation(paddle, ((WIDTH-paddleWidth)/2), ((HEIGHT-paddleHeight)));
waitForClick();
}
else if(getY(ball) + getHeight(ball) <= 0)
{
velocity_y= -velocity_y;
}
}
// wait for click before exiting
waitForClick();
// game over
closeGWindow(window);
return 0;
}
/**
* Initializes window with a grid of bricks.
*/
void initBricks(GWindow window)
{
// TODO
int ybrick = 5;
for(int i = 0; i < ROWS; i++)
{
int xbrick = 5;
for (int j = 0; j<COLS; j++) {
GRect brick = newGRect(xbrick+10, ybrick, 32, 8);
if (i == 0)
{
setColor(brick, "BLUE");
setFilled(brick, true);
add(window, brick);
}
if (i == 1)
{
setColor(brick, "GREEN");
setFilled(brick, true);
add(window, brick);
}
if (i == 2)
{
setColor(brick, "YELLOW");
setFilled(brick, true);
add(window, brick);
}
if (i == 3)
{
setColor(brick, "RED");
setFilled(brick, true);
add(window, brick);
}
if (i == 4)
{
setColor(brick, "ORANGE");
setFilled(brick, true);
add(window, brick);
}
xbrick += (WIDTH/11);
}
ybrick = ybrick + 20;
}
}
/**
* Instantiates
ball in center of window. Returns ball.
*/
GOval initBall(GWindow window)
{
// TODO
GOval ball = newGOval((WIDTH/2), (HEIGHT/2), 30, 30);
setFilled(ball, true);
setColor(ball, "BLACK");
add(window, ball);
return ball;
}
/**
* Instantiates paddle in bottom-middle of window.
*/
GRect initPaddle(GWindow window)
{
GRect paddle = newGRect((WIDTH-paddleWidth)/2, (HEIGHT-paddleHeight), paddleWidth, paddleHeight);
setFilled(paddle, true);
setColor(paddle, "BLUE");
add(window, paddle);
return paddle;
}
/**
* Instantiates, configures, and returns label for scoreboard.
*/
GLabel initScoreboard(GWindow window)
{
// TODO
GLabel label = newGLabel(" ");
setFont(label, "Sanserif-36");
setColor(label, "RED");
setLocation(label, 150, 300);
add(window, label);
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;
}