-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
277 lines (235 loc) · 8.41 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
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <list>
#include <iostream>
#define WINDOW_WIDTH 1200
#define WINDOW_HEIGHT 600
struct Pipe
{
int x; // x-coordinate of the pipe
int gapY; // y-coordinate of the gap's center
static const int GAP_HEIGHT = 250; // The height of the gap; you can adjust this value
static const int PIPE_WIDTH = 50; // Width of the pipe; adjust based on your pipe sprite
bool passed = false;
};
struct Bird
{
int x, y; // Bird's position
int width, height; // Bird's dimensions
float velocity; // Bird's vertical velocity
float gravity; // Gravity pulling the bird downwards
float lift; // Upwards force when the bird flaps
float terminal_velocity;
bool isColliding = false;
};
int main(int argc, char *argv[])
{
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
return 1;
}
SDL_Window *window = SDL_CreateWindow("Flappy Bird", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
if (!window)
{
SDL_Log("Unable to create window: %s", SDL_GetError());
return 1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
if (!renderer)
{
SDL_Log("Unable to create renderer: %s", SDL_GetError());
return 1;
}
if (!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG))
{
SDL_Log("Unable to initialize SDL_image: %s", IMG_GetError());
return 1;
}
// load sprites
SDL_Texture *birdTexture = IMG_LoadTexture(renderer, "sprites/bird.png");
if (!birdTexture)
{
SDL_Log("Failed to load bird texture: %s", IMG_GetError());
return 1;
}
SDL_Texture *upperPipeTexture = IMG_LoadTexture(renderer, "sprites/upper_pipe.png");
if (!upperPipeTexture)
{
SDL_Log("Failed to load upper_pipe texture: %s", IMG_GetError());
return 1;
}
SDL_Texture *lowerPipeTexture = IMG_LoadTexture(renderer, "sprites/lower_pipe.png");
if (!lowerPipeTexture)
{
SDL_Log("Failed to load lower_pipe texture: %s", IMG_GetError());
return 1;
}
SDL_Texture *bgTexture = IMG_LoadTexture(renderer, "sprites/background.png");
if (!bgTexture)
{
SDL_Log("Failed to load background texture: %s", IMG_GetError());
return 1;
}
if (TTF_Init() == -1)
{
SDL_Log("Unable to initialize SDL_ttf: %s", TTF_GetError());
return 1;
}
TTF_Font *font = TTF_OpenFont("font.ttf", 24); // Adjust the size (24 here) as needed
if (!font)
{
SDL_Log("Unable to load font: %s", TTF_GetError());
return 1;
}
bool isRunning = true;
SDL_Event event;
int score = 0;
bool gameOver = false;
// Bird instance (player)
Bird bird;
bird.x = WINDOW_WIDTH / 8; // starting position
bird.y = WINDOW_HEIGHT / 2; // starting position
bird.width = 50; // adjust based on your sprite
bird.height = 50; // adjust based on your sprite
bird.velocity = 0;
bird.gravity = 0.008;
bird.lift = -3;
bird.terminal_velocity = 6;
std::list<Pipe> pipes;
Uint32 lastPipeSpawnTime = SDL_GetTicks();
const Uint32 PIPE_SPAWN_INTERVAL = 2000;
while (isRunning)
{
Uint32 currentTime = SDL_GetTicks();
if (currentTime - lastPipeSpawnTime > PIPE_SPAWN_INTERVAL)
{
Pipe newPipe;
newPipe.x = WINDOW_WIDTH; // Assuming WINDOW_WIDTH is the width of your game window
newPipe.gapY = (rand() % (WINDOW_HEIGHT - Pipe::GAP_HEIGHT)) + Pipe::GAP_HEIGHT / 2;
pipes.push_back(newPipe);
lastPipeSpawnTime = currentTime;
}
// Move all pipes to the left
for (Pipe &pipe : pipes)
{
pipe.x -= 1; // Adjust the speed as needed
}
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
isRunning = false;
}
else if (event.type == SDL_MOUSEBUTTONDOWN || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_SPACE))
{
bird.velocity += bird.lift;
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // Black background
SDL_RenderClear(renderer);
SDL_Rect bgRect = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT};
SDL_RenderCopy(renderer, bgTexture, NULL, &bgRect);
// Render score
std::string scoreText = "Score: " + std::to_string(score);
SDL_Color textColor = {255, 255, 255, 255}; // White text; adjust as needed
SDL_Surface *scoreSurface = TTF_RenderText_Solid(font, scoreText.c_str(), textColor);
if (!scoreSurface)
{
SDL_Log("Unable to render text: %s", TTF_GetError());
return 1;
}
SDL_Texture *scoreTexture = SDL_CreateTextureFromSurface(renderer, scoreSurface);
if (!scoreTexture)
{
SDL_Log("Unable to create texture from rendered text: %s", SDL_GetError());
return 1;
}
int scoreWidth = scoreSurface->w;
int scoreHeight = scoreSurface->h;
SDL_Rect scoreRect = {10, 10, scoreWidth, scoreHeight}; // Position the score at the top-left of the screen with a little offset
SDL_RenderCopy(renderer, scoreTexture, NULL, &scoreRect);
SDL_FreeSurface(scoreSurface);
SDL_DestroyTexture(scoreTexture);
// Draw bird
SDL_Rect birdRect = {bird.x, bird.y, bird.width, bird.height};
SDL_RenderCopy(renderer, birdTexture, NULL, &birdRect);
// Bird physics
bird.velocity += bird.gravity;
bird.y += bird.velocity;
// Optional: Add a terminal velocity
if (abs(bird.velocity) > abs(bird.terminal_velocity))
{
bird.velocity = bird.terminal_velocity;
}
if (bird.y < 0)
{
bird.y = 0;
bird.velocity = 0; // stop the bird from moving upwards
}
else if (bird.y + bird.height > WINDOW_HEIGHT)
{
bird.y = WINDOW_HEIGHT - bird.height;
}
// Drawing Pipes
for (const Pipe &pipe : pipes)
{
// Upper pipe
SDL_Rect upperDest = {pipe.x, 0, Pipe::PIPE_WIDTH, pipe.gapY - Pipe::GAP_HEIGHT / 2};
SDL_RenderCopy(renderer, upperPipeTexture, NULL, &upperDest);
// Lower pipe
SDL_Rect lowerDest = {pipe.x, pipe.gapY + Pipe::GAP_HEIGHT / 2, Pipe::PIPE_WIDTH, WINDOW_HEIGHT - (pipe.gapY + Pipe::GAP_HEIGHT / 2)};
SDL_RenderCopy(renderer, lowerPipeTexture, NULL, &lowerDest);
}
// Removing Off-screen Pipes
while (!pipes.empty() && pipes.front().x + Pipe::PIPE_WIDTH < 0)
{
pipes.pop_front();
}
bool hasCollided = false; // Local flag for this frame
for (Pipe &pipe : pipes)
{
if (!pipe.passed && bird.x > pipe.x + Pipe::PIPE_WIDTH)
{
score++;
pipe.passed = true;
std::cout << "Score: " << score << std::endl;
}
if (bird.x + bird.width > pipe.x && bird.x < pipe.x + Pipe::PIPE_WIDTH)
{
if (bird.y < pipe.gapY - Pipe::GAP_HEIGHT / 2 || bird.y + bird.height > pipe.gapY + Pipe::GAP_HEIGHT / 2)
{
hasCollided = true;
break;
}
}
}
if (hasCollided && !bird.isColliding) // If there's a collision now, but there wasn't in the last frame
{
std::cout << "Bird collided with pipe!" << std::endl;
score = 0;
bird.isColliding = true; // Update the global flag
gameOver = true;
}
else if (!hasCollided) // If there's no collision now
{
bird.isColliding = false; // Reset the global flag
}
if (gameOver)
{
gameOver = false;
}
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(birdTexture);
SDL_DestroyTexture(upperPipeTexture);
SDL_DestroyTexture(lowerPipeTexture);
SDL_DestroyTexture(bgTexture);
TTF_CloseFont(font);
TTF_Quit();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}