-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlappyBird.cpp
More file actions
178 lines (138 loc) · 4.42 KB
/
Copy pathFlappyBird.cpp
File metadata and controls
178 lines (138 loc) · 4.42 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
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
#include <iostream>
#include "raylib.h"
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif
#define MAX_TUBES 100
#define FLOPPY_RADIUS 24
#define TUBES_WIDTH 80
typedef struct Floppy {
Vector2 position;
int radius;
Color color;
} Floppy;
typedef struct Tubes {
Rectangle rec;
Color color;
bool active;
} Tubes;
static const int screenWidth = 800;
static const int screenHeight = 450;
static bool gameOver = false;
static bool pause = false;
static int score = 0;
static int hiScore = 0;
static Floppy floppy = { 0 };
static Tubes tubes[MAX_TUBES*2] = { 0 };
static Vector2 tubesPos[MAX_TUBES] = { 0 };
static int tubesSpeedX = 0;
static bool superfx = false;
static void InitGame(void);
static void UpdateGame(void);
static void DrawGame(void);
static void UnloadGame(void);
static void UpdateDrawFrame(void);
int main(void) {
if (system("./allocate_resources 1, 200, 0") != 0)
return 1;
InitWindow(screenWidth, screenHeight, "classic game: floppy");
InitGame();
SetTargetFPS(60);
while (!WindowShouldClose()) {
UpdateDrawFrame();
}
UnloadGame();
CloseWindow();
system("./detach_resources 1, 200, 0");
return 0;
}
void InitGame(void) {
floppy.radius = FLOPPY_RADIUS;
floppy.position = (Vector2){80, screenHeight/2 - floppy.radius};
tubesSpeedX = 2;
for (int i = 0; i < MAX_TUBES; i++) {
tubesPos[i].x = 400 + 280*i;
tubesPos[i].y = -GetRandomValue(0, 120);
}
for (int i = 0; i < MAX_TUBES*2; i += 2) {
tubes[i].rec.x = tubesPos[i/2].x;
tubes[i].rec.y = tubesPos[i/2].y;
tubes[i].rec.width = TUBES_WIDTH;
tubes[i].rec.height = 255;
tubes[i+1].rec.x = tubesPos[i/2].x;
tubes[i+1].rec.y = 600 + tubesPos[i/2].y - 255;
tubes[i+1].rec.width = TUBES_WIDTH;
tubes[i+1].rec.height = 255;
tubes[i/2].active = true;
}
score = 0;
gameOver = false;
superfx = false;
pause = false;
}
void UpdateGame(void) {
if (!gameOver)
{
if (IsKeyPressed('P'))
pause = !pause;
if (!pause) {
for (int i = 0; i < MAX_TUBES; i++)
tubesPos[i].x -= tubesSpeedX;
for (int i = 0; i < MAX_TUBES*2; i += 2) {
tubes[i].rec.x = tubesPos[i/2].x;
tubes[i+1].rec.x = tubesPos[i/2].x;
}
if (IsKeyDown(KEY_SPACE) && !gameOver)
floppy.position.y -= 3;
else
floppy.position.y += 1;
for (int i = 0; i < MAX_TUBES*2; i++) {
if (CheckCollisionCircleRec(floppy.position, floppy.radius, tubes[i].rec)) {
gameOver = true;
pause = false;
}
else if ((tubesPos[i/2].x < floppy.position.x) && tubes[i/2].active && !gameOver) {
score += 100;
tubes[i/2].active = false;
superfx = true;
if (score > hiScore)
hiScore = score;
}
}
}
}
else {
if (IsKeyPressed(KEY_ENTER)) {
InitGame();
gameOver = false;
}
}
}
void DrawGame(void) {
BeginDrawing();
ClearBackground(RAYWHITE);
if (!gameOver) {
DrawCircle(floppy.position.x, floppy.position.y, floppy.radius, DARKGRAY);
for (int i = 0; i < MAX_TUBES; i++) {
DrawRectangle(tubes[i*2].rec.x, tubes[i*2].rec.y, tubes[i*2].rec.width, tubes[i*2].rec.height, GRAY);
DrawRectangle(tubes[i*2 + 1].rec.x, tubes[i*2 + 1].rec.y, tubes[i*2 + 1].rec.width, tubes[i*2 + 1].rec.height, GRAY);
}
if (superfx) {
DrawRectangle(0, 0, screenWidth, screenHeight, WHITE);
superfx = false;
}
DrawText(TextFormat("%04i", score), 20, 20, 40, GRAY);
DrawText(TextFormat("HI-SCORE: %04i", hiScore), 20, 70, 20, LIGHTGRAY);
if (pause)
DrawText("GAME PAUSED", screenWidth/2 - MeasureText("GAME PAUSED", 40)/2, screenHeight/2 - 40, 40, GRAY);
}
else
DrawText("PRESS [ENTER] TO PLAY AGAIN", GetScreenWidth()/2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20)/2, GetScreenHeight()/2 - 50, 20, GRAY);
EndDrawing();
}
void UnloadGame(void) {
}
void UpdateDrawFrame(void) {
UpdateGame();
DrawGame();
}