-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
443 lines (335 loc) · 11.6 KB
/
main.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
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
441
442
443
/*
##########################################
# #
# ---> Project Name: Block Blaster <--- #
# #
# Name: Azizur Rahaman #
# ID:0112330455 #
# #
# #
##########################################
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "raylib.h"
#define WIDTH 800
#define HEIGHT 600
#define SPEED 500.0f
#define PLAYER_SPEED 1.5 * SPEED
#define BALL_SPEED SPEED * 0.2
#define BLOCK_COUNT 60
#define BLOCK_WIDTH 40
#define BLOCK_HEIGHT 20
#define ITERATIONS 4
#define BOUNCE_WAIT 300
typedef struct {
Vector2 pos;
Vector2 size;
} Entity;
typedef struct {
Entity entity;
bool is_destroyed;
} Block;
typedef struct {
int score;
int high_score;
size_t destroyed_count;
size_t bounce_wait;
Vector2 ball_initial_pos;
Vector2 ball_speed;
float delta_time;
Entity player;
Entity ball;
} Game;
Game game = {0};
Block block_stack[BLOCK_COUNT];
void read_from_file() {
FILE *file = fopen("database.txt", "r");
if(file == NULL) {
fprintf(stderr, "error: could not open file highscore.txt\n");
fclose(file);
game.score = 0;
return;
}
int maxi = 0;
int temp;
while (fscanf(file, "%d\n", &temp) != EOF){
printf("%d\n", temp);
if(temp > maxi)
maxi = temp;
}
fclose(file);
game.high_score = maxi;
}
void write_to_file() {
FILE *file = fopen("database.txt", "a");
if(file == NULL) {
fprintf(stderr, "error: could not open file highscore.txt\n");
return;
}
fprintf(file, "\n%d", game.score);
fclose(file);
}
Entity init_entity(float x, float y, float width, float height) {
Entity entity = {
.pos = {
.x = x,
.y = y,
},
.size = {
.x = width,
.y = height,
}
};
return entity;
}
bool check_collision(Entity *e1, Entity *e2) {
if((e1->pos.x) <= (e2->pos.x + e2->size.x) &&
(e1->pos.x + e1->size.x) >= e2->pos.x &&
(e1->pos.y) <= (e2->pos.y + e2->size.y) &&
((e1->pos.y + e1->size.y) >= e2->pos.y)) {
return true;
}
return false;
}
void reset_blocks() {
size_t block_stack_pos = 0;
for(size_t layers = 0; layers <= ITERATIONS; layers++) {
for(size_t i = 0; i < BLOCK_COUNT/ITERATIONS; i++) {
Block block = {
.entity = init_entity(((BLOCK_WIDTH + 10) * i + (BLOCK_WIDTH - 5)), ((BLOCK_HEIGHT + 5) * layers) + 20, BLOCK_WIDTH, BLOCK_HEIGHT),
.is_destroyed = false,
};
block_stack[block_stack_pos++] = block;
}
}
}
void reset_ball(Entity *ball, const Game *game) {
ball->pos.x = game->ball_initial_pos.x;
ball->pos.y = game->ball_initial_pos.y;
}
float random_x() {
srand(time(NULL));
return SPEED - (float)rand()/(float)(RAND_MAX/0.19f);
}
void initialize(Game *game) {
read_from_file();
printf("%d\n", game->high_score);
game->score = 0;
game->destroyed_count = 0;
Vector2 ball_speed = {
.x = random_x(),
.y = BALL_SPEED,
};
game->ball_speed = ball_speed;
Vector2 ball_initial_pos = {
.x = WIDTH/2,
.y = HEIGHT/2,
};
game->ball_initial_pos = ball_initial_pos;
game->bounce_wait = 0;
reset_blocks();
}
void MENU_TITLE(char title[], float posX, float posY){
DrawRectangle(posX, posY, 200, 40, BLACK);
DrawText(title, posX + 30, posY + 10, 20, RAYWHITE);
}
void BUILD_BUTTON(char title[], float posX, float posY){
DrawRectangle(posX, posY, 40, 40, BLACK);
DrawText(title, posX + 15, posY + 10, 20, RAYWHITE);
}
void splash_screen(){
float posX = (WIDTH/2) - 70;
float posY = HEIGHT - (HEIGHT/4);
bool isClose = false;
while(!(IsKeyPressed(KEY_ENTER))) {
BeginDrawing();
ClearBackground(LIGHTGRAY);
DrawText("BLOCK BLASTER", 50, HEIGHT/2 - 20, 80, RED);
// Enter Button
DrawRectangle(posX, posY, 140, 40, BLACK);
DrawText("HIT ENTER!", posX + 10, posY + 10, 20, RAYWHITE);
EndDrawing();
if(WindowShouldClose()){
isClose = true;
break;
}
}
if(isClose) CloseWindow();
printf("key pressed\n");
}
void GamePlay();
void ScoreBoard();
void show_menu(){
float posX = (WIDTH/2) - 70;
float posY = 200;
while (!(WindowShouldClose())){
BeginDrawing();
ClearBackground(RAYWHITE);
BUILD_BUTTON("P", posX-60, posY);
MENU_TITLE("PLAY", posX, posY);
BUILD_BUTTON("S", posX-60, posY+50);
MENU_TITLE("SCORE BOARD", posX, posY+50);
BUILD_BUTTON("E", posX-60, posY+100);
MENU_TITLE("EXIT", posX, posY+100);
EndDrawing();
if(IsKeyPressed(KEY_P)){
GamePlay();
}else if(IsKeyPressed(KEY_S)){
ScoreBoard();
}else if(IsKeyPressed(KEY_E)){
break;
}
}
CloseWindow();
}
void playAduio(char audioFile[]){
InitAudioDevice();
Music music = LoadMusicStream(audioFile);
PlayMusicStream(music);
UpdateMusicStream(music);
UnloadMusicStream(music);
CloseAudioDevice();
}
int main() {
InitWindow(WIDTH, HEIGHT, "Block Blaster");
splash_screen();
show_menu();
CloseWindow();
return 0;
}
void GamePlay(){
initialize(&game);
game.ball = init_entity(game.ball_initial_pos.x, game.ball_initial_pos.y, 20, 20);
game.player = init_entity(WIDTH/2, HEIGHT - (HEIGHT/10), 100, 20);
// InitWindow(WIDTH, HEIGHT, "breakout");
bool isPause = false;
while(!WindowShouldClose()) {
game.delta_time = GetFrameTime();
// crash if all blocks are destroyed
if(game.destroyed_count >= BLOCK_COUNT || game.ball.pos.y >= HEIGHT-20) {
playAduio("assets/game_over.wav");
Vector2 font_size = MeasureTextEx(GetFontDefault(), "GAME OVER", 100, 10.0f);
DrawText("GAME OVER", WIDTH/2 - font_size.x/2, HEIGHT/2 - font_size.y/2, 100, RED);
EndDrawing();
write_to_file();
reset_ball(&game.ball, &game);
initialize(&game);
game.score += 1;
sleep(3);
break;
}
// block rendering and collision handling
for(size_t i = 0; i < BLOCK_COUNT; i++) {
Block *enemy = &block_stack[i];
if(!enemy->is_destroyed) {
if(i <= 14){
DrawRectangleV(enemy->entity.pos, enemy->entity.size, RED);
}else if(i <=29){
DrawRectangleV(enemy->entity.pos, enemy->entity.size, BLUE);
}else if(i <=44){
DrawRectangleV(enemy->entity.pos, enemy->entity.size, GREEN);
}else if(i <=59){
DrawRectangleV(enemy->entity.pos, enemy->entity.size, YELLOW);
}
if(check_collision(&enemy->entity, &game.ball)) {
game.destroyed_count++;
block_stack[i].is_destroyed = true;
game.score += 1;
game.ball_speed.y = BALL_SPEED;
}
}
}
if(GetKeyPressed() == KEY_SPACE){
isPause = !isPause;
}
// PAUSE
if(isPause){
MENU_TITLE("PAUSE", WIDTH/2 - 50, HEIGHT/2 - 50);
EndDrawing();
continue;
}
if(game.ball.pos.x < (0 - 25)) game.ball.pos.x = 0;
if(game.ball.pos.x > (WIDTH - game.ball.size.x + 25)) game.ball.pos.x = (WIDTH - game.ball.size.x);
if(game.ball.pos.y < (0 - 25)) game.ball.pos.y = 0;
if(game.ball.pos.y > (HEIGHT - game.ball.size.y + 25)) game.ball.pos.y = (HEIGHT - game.ball.size.y);
if(game.player.pos.x < 0) game.player.pos.x = 0;
if(game.player.pos.x > (WIDTH - game.player.size.x)) game.player.pos.x = (WIDTH - game.player.size.x);
// Key presses
//
//
if(IsKeyDown(KEY_LEFT)) game.player.pos.x -= PLAYER_SPEED * game.delta_time;
if(IsKeyDown(KEY_RIGHT)) game.player.pos.x += PLAYER_SPEED * game.delta_time;
// game.ball speed
game.ball.pos.y += (1 * game.ball_speed.y) * game.delta_time;
game.ball.pos.x += (1 * game.ball_speed.x) * game.delta_time;
// game.player game.ball collision
if(game.bounce_wait == 0) {
if((game.player.pos.x) <= (game.ball.pos.x + game.ball.size.x) &&
(game.player.pos.x + game.player.size.x) >= game.ball.pos.x &&
(game.player.pos.y) <= (game.ball.pos.y + game.ball.size.y) &&
(game.player.pos.y + 5) >= (game.ball.pos.y + game.ball.size.y) &&
(game.player.pos.y + game.player.size.y) >= (game.ball.pos.y)
) {
game.ball_speed.y *= -1;
game.bounce_wait = BOUNCE_WAIT;
} else if((game.player.pos.x) <= (game.ball.pos.x + game.ball.size.x) && (game.player.pos.x + game.ball.size.x * 0.25) >= (game.ball.pos.x + game.ball.size.x) &&
(game.player.pos.x + game.player.size.x) >= game.ball.pos.x &&
(game.player.pos.y) <= (game.ball.pos.y + game.ball.size.y) &&
(game.player.pos.y + game.player.size.y) >= (game.ball.pos.y)
) {
game.ball_speed.y *= -1;
game.ball_speed.x = -random_x();
game.bounce_wait = BOUNCE_WAIT;
} else if((game.player.pos.x) <= (game.ball.pos.x + game.ball.size.x) &&
(game.player.pos.x + game.player.size.x) >= game.ball.pos.x && (game.player.pos.x + game.player.size.x) <= (game.ball.pos.x + game.ball.size.x * 0.25) &&
(game.player.pos.y) <= (game.ball.pos.y + game.ball.size.y) &&
(game.player.pos.y + game.player.size.y) >= (game.ball.pos.y)
) {
game.ball_speed.y *= -1;
game.ball_speed.x = random_x();
game.bounce_wait = BOUNCE_WAIT;
}
}
// game.ball window collision
if(game.ball.pos.y > HEIGHT) {
game.score -= 1;
reset_ball(&game.ball, &game);
}
if(game.ball.pos.y < 0) game.ball_speed.y *= -1;
if(game.ball.pos.x > WIDTH) game.ball_speed.x *= -1;
if(game.ball.pos.x < 0) game.ball_speed.x *= -1;
// init things
BeginDrawing();
ClearBackground(LIGHTGRAY);
DrawRectangleV(game.player.pos, game.player.size, BLACK);
DrawRectangleV(game.ball.pos, game.ball.size, BLACK);
char *score_str = malloc(45);
sprintf(score_str, "Score: %d", game.score);
DrawText(score_str, (WIDTH/2) - 80, 0, 20, BLUE);
free(score_str);
EndDrawing();
if(game.bounce_wait > 0) game.bounce_wait--;
}
}
void ScoreBoard(){
read_from_file();
while(!WindowShouldClose()){
char textBuffer[20];
BeginDrawing();
ClearBackground(RAYWHITE);
//BACK BUTTON
BUILD_BUTTON("<", 10, 10);
DrawText("SCORE BOARD: ", (WIDTH/2) - 150, 50, 40, BLACK);
sprintf(textBuffer, "%d", game.high_score);
DrawText("Score: ", (WIDTH/2)-100, 100, 30, BLACK);
DrawText(textBuffer, (WIDTH/2)+ 20, 100, 30, BLACK);
EndDrawing();
if(IsKeyPressed(KEY_LEFT)){
break;
}
}
}