-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceCode.cpp
More file actions
400 lines (335 loc) · 14 KB
/
Copy pathSourceCode.cpp
File metadata and controls
400 lines (335 loc) · 14 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
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
#include <SDL.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <sstream>
#include <SDL_ttf.h>
SDL_Texture* loadTexture(const std::string& path, SDL_Renderer* renderer) {
SDL_Surface* surface = SDL_LoadBMP(path.c_str());
if (!surface) {
std::cerr << "Unable to load image " << path << "! SDL_Error: " << SDL_GetError() << std::endl;
return nullptr;
}
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
return texture;
}
SDL_Texture* renderText(const std::string& message, TTF_Font* font, SDL_Color color, SDL_Renderer* renderer) {
SDL_Surface* surface = TTF_RenderText_Solid(font, message.c_str(), color);
if (!surface) {
std::cerr << "Failed to render text to surface! SDL_ttf Error: " << TTF_GetError() << std::endl;
return nullptr;
}
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
return texture;
}
struct Events {
bool exit = false;
bool left = false;
bool right = false;
bool up = false;
bool down = false;
bool space = false;
bool enter = false;
};
struct Item {
float x;
float y;
float w;
float h;
float velX;
float velY;
Item(float x, float y, float w, float velX, float velY) : x(x), y(y), w(w), h(w), velX(velX), velY(velY) {}
};
void SDLEvents(Events& event) {
SDL_Event sdl_event;
while (SDL_PollEvent(&sdl_event) > 0) {
if (sdl_event.type == SDL_QUIT) {
event.exit = true;
return;
}
if (sdl_event.type == SDL_KEYDOWN) {
switch (sdl_event.key.keysym.sym) {
case SDLK_UP:
event.up = true;
break;
case SDLK_DOWN:
event.down = true;
break;
case SDLK_LEFT:
event.left = true;
break;
case SDLK_RIGHT:
event.right = true;
break;
case SDLK_SPACE:
event.space = true;
break;
case SDLK_RETURN:
case SDLK_KP_ENTER:
event.enter = true;
break;
}
}
if (sdl_event.type == SDL_KEYUP) {
switch (sdl_event.key.keysym.sym) {
case SDLK_UP:
event.up = false;
break;
case SDLK_DOWN:
event.down = false;
break;
case SDLK_LEFT:
event.left = false;
break;
case SDLK_RIGHT:
event.right = false;
break;
case SDLK_SPACE:
event.space = false;
break;
case SDLK_RETURN:
case SDLK_KP_ENTER:
event.enter = false;
break;
}
}
}
}
bool isCollision(Item& i1, Item& i2) {
return (i1.x < i2.x + i2.w &&
i1.x + i1.w > i2.x &&
i1.y < i2.y + i2.w &&
i1.y + i1.w > i2.y);
}
void resetGame(Item& cha, Item& ene, Item& food, Item& food1, Item& poison, int windowsizex, int windowsizey) {
// Reset character position and size
cha.x = windowsizex / 2;
cha.y = windowsizey / 2;
cha.w = windowsizex / 10;
cha.h = cha.w;
while (isCollision(cha, ene)) {
ene.x += cha.w;
ene.y += cha.w;
}
ene.velX = static_cast<float> (rand() % 7 + 2) / 10.0;
ene.velY = static_cast<float> (rand() % 7 + 2) / 10.0;
// Reset food position
food.x = rand() % (windowsizex - static_cast<int>(food.w));
food.y = rand() % (windowsizey - static_cast<int>(food.h));
food1.x = rand() % (windowsizex - static_cast<int>(food1.w));
food1.y = rand() % (windowsizey - static_cast<int>(food1.h));
poison.x = rand() % (windowsizex - static_cast<int>(poison.w));
poison.y = rand() % (windowsizey - static_cast<int>(poison.h));
}
void drawText(SDL_Renderer* renderer, const std::string& message, TTF_Font* font, SDL_Color color, int x, int y) {
SDL_Texture* textTexture = renderText(message, font, color, renderer);
if (textTexture) {
int texW = 0;
int texH = 0;
SDL_QueryTexture(textTexture, NULL, NULL, &texW, &texH);
SDL_Rect dstRect = { x, y, texW, texH };
SDL_RenderCopy(renderer, textTexture, NULL, &dstRect);
SDL_DestroyTexture(textTexture);
}
}
int main(int argc, char* argv[]) {
TTF_Init();
int windowsizex = 800;
int windowsizey = 600;
// Define items
Item cha(windowsizex / 2, windowsizey / 2, windowsizex / 10, 0.8, 0.8);
Item ene(70, 10, 70, static_cast<float> (rand() % 7 + 2) / 10.0, static_cast<float> (rand() % 7 + 2) / 10.0);
Item dang(690, 5, 100, 0, 0);
Item food(rand() % (windowsizex - 50), rand() % (windowsizey - 50), 20, 0, 0);
Item food1(rand() % (windowsizex - 50), rand() % (windowsizey - 50), 20, 0, 0);
Item poison(rand() % (windowsizex - 50), rand() % (windowsizey - 50), 20, 0, 0);
SDL_Window* window = SDL_CreateWindow("SQUARE GAME!", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, windowsizex, windowsizey, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Texture* background = loadTexture("./assets/PlayGround.bmp", renderer);
SDL_Texture* character = loadTexture("./assets/Square.bmp", renderer);
SDL_Texture* enemy = loadTexture("./assets/Enemy.bmp", renderer);
SDL_Texture* foodTexture = loadTexture("./assets/Heart.bmp", renderer);
SDL_Texture* gameOver = loadTexture("./assets/GameOverScreen.bmp", renderer);
SDL_Texture* gameStart = loadTexture("./assets/StartScreen.bmp", renderer);
SDL_Texture* danger = loadTexture("./assets/DangerSign.bmp", renderer);
SDL_Texture* food1Texture = loadTexture("./assets/Food.bmp", renderer);
SDL_Texture* poisonfood = loadTexture("./assets/Poison.bmp", renderer);
// Load font
TTF_Font* font = TTF_OpenFont("./assets/BrownieStencil-8O8MJ.ttf", 24);
bool running = true;
bool gameOverState = false;
bool gameStartState = true;
Events event;
int a = rand() % 5 + 3; // 3 --- 7
int b = rand() % 8 + 9; // 9 ---- 16
Uint32 startTime = SDL_GetTicks();
SDL_Color textColor = { 255, 255, 255, 255 };
// Game start loop
while (gameStartState) {
SDLEvents(event);
if (event.enter) {
gameStartState = false;
startTime = SDL_GetTicks();
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, gameStart, NULL, NULL);
SDL_RenderPresent(renderer);
}
while (running) {
SDLEvents(event);
if (event.exit) running = false;
int seconds = (SDL_GetTicks() - startTime) / 1000;
int score = 100 * seconds; // Calculate score based on time
if (!gameOverState) {
if (event.up) { (seconds < b && seconds > a) ? cha.y += cha.velX : cha.y -= cha.velX; }
if (event.down) { (seconds < b && seconds > a) ? cha.y -= cha.velX : cha.y += cha.velX; }
if (event.left) { (seconds < b && seconds > a) ? cha.x += cha.velX : cha.x -= cha.velX; }
if (event.right) { (seconds < b && seconds > a) ? cha.x -= cha.velX : cha.x += cha.velX; }
if ((seconds > b)) {
a = 1.4 * (rand() % (seconds + 5) + 3); // 3 --- 7
b = 1.4 * (rand() % (seconds + 8) + 9); // 9 ---- 16
}
// Boundary check
if (cha.x < 0) cha.x = 0;
if (cha.x + cha.w > windowsizex) cha.x = windowsizex - cha.w;
if (cha.y < 0) cha.y = 0;
if (cha.y + cha.h > windowsizey) cha.y = windowsizey - cha.h;
// Food size
food.w -= 0.01;
food.h = food.w;
food1.w -= 0.03;
food1.h = food1.w;
poison.w -= 0.008;
poison.h = poison.w;
ene.x += ((float)seconds / 20.0 + 0.3) * ene.velX;
ene.y += ((float)seconds / 20.0) * ene.velY;
// Reset food position if it's gone
if (food.w <= 15) {
food.w = rand() % 40 + 30;
food.h = food.w;
food.x = rand() % (windowsizex - static_cast<int>(food.w));
food.y = rand() % (windowsizey - static_cast<int>(food.h));
while (isCollision(food, cha) || isCollision(food, food1)) {
food1.x = rand() % (windowsizex - static_cast<int>(food.w));
food1.y = rand() % (windowsizey - static_cast<int>(food.h));
}
}
if (food1.w <= 10) {
food1.w = rand() % 40 + 30;
food1.h = food.w;
food1.x = rand() % (windowsizex - static_cast<int>(food1.w));
food1.y = rand() % (windowsizey - static_cast<int>(food1.h));
while (isCollision(food1, cha) || isCollision(food1, food)) {
food1.x = rand() % (windowsizex - static_cast<int>(food1.w));
food1.y = rand() % (windowsizey - static_cast<int>(food1.h));
}
}
if (poison.w <= 5) {
poison.w = rand() % 40 + 30;
poison.h = poison.w;
poison.x = rand() % (windowsizex - static_cast<int>(poison.w));
poison.y = rand() % (windowsizey - static_cast<int>(poison.h));
while (isCollision(poison, cha) || isCollision(poison, food1) || isCollision(poison, food)) {
food1.x = rand() % (windowsizex - static_cast<int>(poison.w));
food1.y = rand() % (windowsizey - static_cast<int>(poison.h));
}
}
// Check for collision with food
if (isCollision(cha, food)) {
// Increase character size
if (cha.w <= 90) {
cha.w += static_cast<int>(food.w / 1.3);
cha.h = cha.w;
}
// Reset food position
food.x = rand() % (windowsizex - static_cast<int>(food.w));
food.y = rand() % (windowsizey - static_cast<int>(food.h));
}
else if (isCollision(cha, food1)) {
if (cha.w <= 90) {
cha.w += static_cast<int>(food1.w / 1.3);
cha.h = cha.w;
}
food1.x = rand() % (windowsizex - static_cast<int>(food1.w));
food1.y = rand() % (windowsizey - static_cast<int>(food1.h));
}
else if (isCollision(cha, poison)) {
cha.w -= static_cast<float>(poison.w / 1.3);
cha.h = cha.w;
poison.x = rand() % (windowsizex - static_cast<int>(poison.w));
poison.y = rand() % (windowsizey - static_cast<int>(poison.h));
}
else {
// Shrink character size
cha.w -= 0.01;
cha.h = cha.w;
if (cha.w <= 20) {
gameOverState = true;
}
}
// Check for collision with enemy
if (isCollision(cha, ene)) {
gameOverState = true;
}
}
else {
// Wait for space key to restart the game
if (event.space) {
resetGame(cha, ene, food, food1, poison, windowsizex, windowsizey);
startTime = SDL_GetTicks();
seconds = (SDL_GetTicks() - startTime) / 1000;
gameOverState = false;
}
}
if (ene.x <= 0 || ene.x + (ene.w / 2) >= windowsizex) {
ene.velX = -ene.velX;
}
if (ene.y <= 0 || ene.y + (ene.w / 2) >= windowsizey) {
ene.velY = -ene.velY;
}
SDL_RenderClear(renderer);
if (!gameOverState) {
// Render game objects
SDL_RenderCopy(renderer, background, NULL, NULL);
SDL_Rect chaRect = { static_cast<int>(cha.x), static_cast<int>(cha.y), static_cast<int>(cha.w), static_cast<int>(cha.h) };
SDL_RenderCopy(renderer, character, NULL, &chaRect);
SDL_Rect enemyRect = { static_cast<int>(ene.x), static_cast<int>(ene.y), static_cast<int>(ene.w), static_cast<int>(ene.h) };
SDL_RenderCopy(renderer, enemy, NULL, &enemyRect);
SDL_Rect foodRect = { static_cast<int>(food.x), static_cast<int>(food.y), static_cast<int>(food.w), static_cast<int>(food.h) };
SDL_RenderCopy(renderer, foodTexture, NULL, &foodRect);
SDL_Rect food1Rect = { static_cast<int>(food1.x), static_cast<int>(food1.y), static_cast<int>(food1.w), static_cast<int>(food1.h) };
SDL_RenderCopy(renderer, food1Texture, NULL, &food1Rect);
SDL_Rect poisonfoodRect = { static_cast<int>(poison.x), static_cast<int>(poison.y), static_cast<int>(poison.w), static_cast<int>(poison.h) };
SDL_RenderCopy(renderer, poisonfood, NULL, &poisonfoodRect);
if (seconds < b && seconds > a) {
SDL_Rect dangRect = { static_cast<int>(dang.x), static_cast<int>(dang.y), static_cast<int>(dang.w), static_cast<int>(dang.h) };
SDL_RenderCopy(renderer, danger, NULL, &dangRect);
}
// Display score
std::stringstream ss;
ss << "SCORE: " << score;
std::string scoreText = ss.str();
drawText(renderer, scoreText, font, textColor, 10, 10);
}
else {
SDL_RenderCopy(renderer, gameOver, NULL, NULL);
}
SDL_RenderPresent(renderer);
}
TTF_CloseFont(font);
SDL_DestroyTexture(gameOver);
SDL_DestroyTexture(foodTexture);
SDL_DestroyTexture(food1Texture);
SDL_DestroyTexture(poisonfood);
SDL_DestroyTexture(enemy);
SDL_DestroyTexture(danger);
SDL_DestroyTexture(character);
SDL_DestroyTexture(background);
SDL_DestroyTexture(gameStart);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}