-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
441 lines (349 loc) · 9.8 KB
/
main.cpp
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
Powerful Trap Beat | Strong by Alex-Productions | https://onsound.eu/
Music promoted by https://www.chosic.com/free-music/all/
Creative Commons CC BY 3.0
https://creativecommons.org/licenses/by/3.0/
*/
#include <raylib.h>
#include <fmt/core.h>
#include <vector>
#include <filesystem>
namespace fs = std::filesystem;
int window_width = 1200;
int window_height = 600;
int paddle_width = 300;
int paddle_height = 20;
float paddle_x = 0;
float paddle_y = 0;
float paddle_speed = 500;
int ball_radius = 25;
float ball_x = 0;
float ball_y = 0;
float ball_speed_x = 0;
float ball_speed_y = 0;
enum powerup_ability {
POWER_BALL_SMALLER,
POWER_BALL_BIGGER,
POWER_BALL_SLOWER,
POWER_BALL_FASTER,
POWER_PADDLE_LONGER,
POWER_PADDLE_SHORTER,
POWER_COUNT,
};
struct powerup {
int size;
int x;
int y;
powerup_ability ability;
};
std::vector<powerup> powerups;
int powerup_count = 20;
int points = 0;
Sound boing;
Music music;
bool start_game = false;
int window_center_x() {
return (window_width / 2);
}
int window_center_y() {
return (window_height / 2);
}
void init() {
InitWindow(window_width, window_height, "Bryan's First Game");
InitAudioDevice();
boing = LoadSound("assets/boing2.wav");
music = LoadMusicStream("assets/Powerful-Trap.mp3");
int gap = 20;
paddle_x = window_center_x() - (paddle_width / 2);
paddle_y = window_height - paddle_height - gap;
ball_x = window_center_x();
ball_y = window_center_y();
PlayMusicStream(music);
}
void remove_powerup(std::vector<powerup>::iterator it) {
powerups.erase(it);
}
void add_random_powerup() {
powerup p;
p.size = 20;
p.x = GetRandomValue(50, window_width - 50);
p.y = GetRandomValue(50, window_height - paddle_height - 100);
p.ability = static_cast<powerup_ability>(GetRandomValue(0, POWER_COUNT));
powerups.push_back(p);
}
bool check_powerup_collisions(const powerup& p) {
Rectangle rect;
rect.width = p.size;
rect.height = p.size;
rect.x = p.x - (rect.width / 2);
rect.y = p.y - (rect.height /2);
Vector2 circle_position;
circle_position.x = ball_x;
circle_position.y = ball_y;
if (CheckCollisionCircleRec(circle_position, ball_radius, rect)) {
// do all the powerups
switch (p.ability) {
case POWER_BALL_BIGGER:
fmt::print("powerup: ball bigger\n");
ball_radius += 20;
if (ball_radius > 100) {
ball_radius = 100;
}
break;
case POWER_BALL_SMALLER:
fmt::print("powerup: ball smaller\n");
ball_radius -= 20;
if (ball_radius < 20) {
ball_radius = 20;
}
break;
case POWER_BALL_SLOWER:
{
fmt::print("powerup: ball slower\n");
ball_speed_x = ball_speed_x / 2;
ball_speed_y = ball_speed_y / 2;
int min_ball_sleep = 50;
if (ball_speed_x < min_ball_sleep) {
ball_speed_x = min_ball_sleep;
}
if (ball_speed_y < min_ball_sleep) {
ball_speed_y = min_ball_sleep;
}
}
break;
case POWER_BALL_FASTER:
{
fmt::print("powerup: ball faster\n");
ball_speed_x = ball_speed_x * 2;
ball_speed_y = ball_speed_y * 2;
int max_ball_speed = 2000;
if (ball_speed_x > max_ball_speed) {
ball_speed_x = max_ball_speed;
}
if (ball_speed_y > max_ball_speed) {
ball_speed_y = max_ball_speed;
}
}
break;
case POWER_PADDLE_LONGER:
fmt::print("powerup: ball longer\n");
paddle_width = paddle_width + 20;
if (paddle_width > 500) {
paddle_width = 500;
}
break;
case POWER_PADDLE_SHORTER:
fmt::print("powerup: ball shorter\n");
paddle_width = paddle_width - 20;
if (paddle_width < 50) {
paddle_width = 50;
}
break;
default:
break;
}
return true;
}
return false;
}
void clear_powerups() {
powerups.clear();
}
void update() {
float time = GetFrameTime();
if (start_game == false && IsKeyPressed(KEY_ENTER)) {
start_game = true;
ball_speed_x = 250;
ball_speed_y = 250;
for (int i = 0; i < powerup_count; i += 1) {
add_random_powerup();
}
}
if (start_game == false) {
return;
}
if(IsKeyPressed(KEY_UP)) {
paddle_speed += 100;
}
if(IsKeyPressed(KEY_DOWN)) {
paddle_speed -= 100;
}
// if (IsKeyDown(KEY_LEFT)) {
// // move padding to the left
// paddle_x = paddle_x - (time * paddle_speed);
// if (paddle_x < 0) {
// paddle_x = 0;
// }
// } else if (IsKeyDown(KEY_RIGHT)) {
// // move padding to the right
// paddle_x = paddle_x + (time * paddle_speed);
// int max_x = window_width - paddle_width;
// if (paddle_x > max_x) {
// paddle_x = max_x;
// }
// }
auto mouse = GetMousePosition();
auto move_diff = mouse.x - (paddle_x + (paddle_width / 2));
if (move_diff < 0 ) {
move_diff = -1;
} else if (move_diff > 0) {
move_diff = 1;
} else {
move_diff = 0;
}
if (paddle_x < 0) {
paddle_x = 0;
}
int max_x = window_width - paddle_width;
if (paddle_x > max_x) {
paddle_x = max_x;
}
// paddle_x = mouse.x;
// paddle_y = mouse.y;
paddle_x += move_diff * paddle_speed * time;
ball_x = ball_x + (time * ball_speed_x);
ball_y = ball_y + (time * ball_speed_y);
int min_ball_x = ball_radius;
int min_ball_y = ball_radius;
int max_ball_x = window_width - ball_radius;
int max_ball_y = window_height - ball_radius;
bool bounce = false;
if (ball_x < min_ball_x) {
ball_x = min_ball_x;
ball_speed_x = -ball_speed_x;
bounce = true;
}
if (ball_x > max_ball_x) {
ball_x = max_ball_x;
ball_speed_x = -ball_speed_x;
bounce = true;
}
if (ball_y < min_ball_y) {
ball_y = min_ball_y;
ball_speed_y = -ball_speed_y;
bounce = true;
}
// if (ball_y > max_ball_y) {
// ball_y = max_ball_y;
// ball_speed_y = -ball_speed_y;
// bounce = true;
// }
if (ball_y > window_height + ball_radius) {
// LOSE, RESTART GAME
start_game = false;
ball_x = window_center_x();
ball_y = window_center_y();
ball_speed_x = 0;
ball_speed_y = 0;
clear_powerups();
return;
}
Vector2 circle_position;
circle_position.x = ball_x;
circle_position.y = ball_y;
Rectangle paddle;
paddle.width = paddle_width;
paddle.height = paddle_height;
paddle.x = paddle_x;
paddle.y = paddle_y;
if (CheckCollisionCircleRec(circle_position, ball_radius, paddle)) {
// ball_speed_x = -ball_speed_x;
ball_speed_y = -ball_speed_y;
bounce = true;
points += 100;
}
if (bounce) {
PlaySound(boing);
}
bool removed_powerup = false;
for (auto it = powerups.begin(); it != powerups.end(); it++) {
const auto& powerup = *it;
if (check_powerup_collisions(powerup)) {
points += 500;
remove_powerup(it);
removed_powerup = true;
break;
}
}
if (removed_powerup) {
add_random_powerup();
}
}
void draw_start_text() {
int x = 0;
int y = 200;
Rectangle rect;
rect.width = 360;
rect.height = 200;
rect.x = window_center_x() - (rect.width / 2) - x;
rect.y = window_center_y() - (rect.height / 2) - y;
DrawRectangleRounded(rect, 1, 3, MAROON);
auto text = "START!";
int font_size = 80;
int width = MeasureText(text, font_size);
DrawText(text, window_center_x() - (width / 2) - x, window_center_y() - 30 - y, font_size, WHITE);
}
void draw_ball() {
DrawCircle(ball_x, ball_y, ball_radius, WHITE);
}
void draw_paddle() {
DrawRectangle(paddle_x, paddle_y, paddle_width, paddle_height, RED);
DrawText(fmt::format("({:.0f},{:.0f})", paddle_x, paddle_y).c_str(), paddle_x, paddle_y, 20, WHITE);
}
void draw_powerups() {
for (auto &powerup: powerups) {
Rectangle rect;
rect.width = powerup.size;
rect.height = powerup.size;
rect.x = powerup.x - (rect.width / 2);
rect.y = powerup.y - (rect.height / 2);
Color a = RED;
Color b = GREEN;
Color c = ORANGE;
Color d = YELLOW;
if (powerup.ability == POWER_BALL_SLOWER ||
powerup.ability == POWER_BALL_SMALLER ||
powerup.ability == POWER_PADDLE_SHORTER)
{
b = MAROON;
c = MAGENTA;
d = VIOLET;
}
DrawRectangleGradientEx(rect, a, b, c, d);
}
}
void draw_points() {
std::string text = fmt::format("{} Points", points);
int font_size = 40;
int width = MeasureText(text.c_str(), font_size);
DrawText(text.c_str(), window_width - width - 10, 10, font_size, WHITE);
}
void draw() {
BeginDrawing();
ClearBackground(BLACK);
DrawFPS(10, 10);
draw_ball();
draw_paddle();
draw_powerups();
draw_points();
if (start_game == false) {
draw_start_text();
}
EndDrawing();
}
int main() {
fmt::print("Hello, world\n");
init();
SetExitKey(KEY_ESCAPE);
while (!WindowShouldClose()) {
UpdateMusicStream(music);
update();
draw();
}
fmt::print("Closing...\n");
UnloadSound(boing);
UnloadMusicStream(music);
CloseAudioDevice();
CloseWindow();
return 0;
}