Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
141 lines (109 loc) · 3.65 KB
/
Copy pathmain.c
File metadata and controls
141 lines (109 loc) · 3.65 KB
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
#include "raylib.h"
#include <stdio.h>
typedef enum GameScreen {MENU,PLAYING,GAMEOVER} GameScreen;
int main(void) {
const int screenWidth = 800;
const int screenHeight = 450;
int bucketX = 400;
int bucketY = 430;
int ballX = 400;
int ballY = 0;
int score = 0;
int highScore = 0;
GameScreen currentScreen = MENU;
InitWindow(screenWidth, screenHeight, "My 2D Game");
InitAudioDevice();
Music menuMusic = LoadMusicStream("sound/open_screen.mp3");
PlayMusicStream(menuMusic);
Sound catchSound = LoadSound("sound/collect_sound.wav");
Sound failSound = LoadSound("sound/fail.wav");
FILE *file = fopen("highscore.txt", "r");
if (file != NULL) {
fscanf(file, "%i", &highScore);
fclose(file);
}
SetTargetFPS(60);
while (!WindowShouldClose()) {
UpdateMusicStream(menuMusic);
switch (currentScreen)
{
case MENU:
if (IsKeyPressed(KEY_ENTER)) {
currentScreen = PLAYING;
StopMusicStream(menuMusic);
}
break;
case PLAYING:
if(IsKeyDown(KEY_RIGHT)){
bucketX +=7;
}
else if (IsKeyDown(KEY_LEFT))
{
bucketX -=7;
}
ballY += 1 + (score / 2);
if (ballY > screenHeight)
{ PlaySound(failSound);
currentScreen = GAMEOVER;
if(score > highScore){
highScore = score;
FILE *file = fopen("highscore.txt", "w");
if (file != NULL) {
fprintf(file, "%i", highScore);
fclose(file);
}
}
currentScreen = GAMEOVER;
}
bool collision = CheckCollisionCircleRec(
(Vector2){ballX, ballY}, 25,
(Rectangle){bucketX, bucketY, 20, 15}
);
if (collision == true)
{
score += 1;
ballY = 0;
ballX = GetRandomValue(1, screenWidth);
PlaySound(catchSound);
}
break;
case GAMEOVER:
// The Restart Logic
if (IsKeyPressed(KEY_ENTER)) {
score = 0;
bucketX = 400;
ballY = 0;
ballX = GetRandomValue(1, screenWidth);
currentScreen=PLAYING;
}
break;
}
BeginDrawing();
ClearBackground(RAYWHITE);
switch(currentScreen) {
case MENU:
DrawText("THE CATCHER", 250, 150, 40, DARKGRAY);
DrawText("Press ENTER to Start", 270, 250, 20, DARKGRAY);
DrawText("Press ESC to Quit", 285, 300, 20, GRAY);
break;
case PLAYING:
DrawCircle(ballX, ballY, 15, PURPLE);
DrawRectangle(bucketX, bucketY, 30, 20, BLACK);
DrawText(TextFormat("Score: %i", score), 10, 10, 20, DARKGRAY);
break;
case GAMEOVER:
DrawText("GAME OVER", 300, 200, 40, RED);
DrawText(TextFormat("Final Score: %i", score), 320, 250, 20, DARKGRAY);
DrawText(TextFormat("High Score: %i", highScore), 0, 0, 22, DARKGRAY);
DrawText("Press ENTER to play again", 250, 300, 20, DARKGRAY);
break;
}
EndDrawing();
}
UnloadSound(catchSound);
UnloadSound(failSound);
UnloadMusicStream(menuMusic);
CloseAudioDevice();
CloseWindow();
return 0;
}