-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
429 lines (371 loc) · 13.4 KB
/
main.cpp
File metadata and controls
429 lines (371 loc) · 13.4 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
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
// based on the template provided for the second project
#define _USE_MATH_DEFINES
#include<math.h>
#include<stdio.h>
#include<string.h>
extern "C" {
#include"./SDL2-2.0.10/include/SDL.h"
#include"./SDL2-2.0.10/include/SDL_main.h"
}
#define SNAKE_LENGTH 100
#define TILE_SIZE 30
// Changable parameters
#define SCR_WIDTH 800
#define SCR_HEIGHT 600
#define POINT_PER_FOOD 1
#define SPEED_UP 5
#define SPEED_UP_FREQUENCY 5 // in seconds
#define INITIAL_POS_X SCR_WIDTH/2
#define INITIAL_POS_Y SCR_HEIGHT/2
#define RED_DOT_SPAWN_CHANCE 10
#define INITIAL_LENGTH 2
#define SHORTEN_SNAKE_LENGHT 1
#define RED_DOT_TIME 5
// Structs for storing information (positions, snake lenght etc)
struct Point {
int x, y;
};
struct Snake {
Point body[SNAKE_LENGTH];
int length;
int direction;
};
// ------------- SDL DRAWING FUNCTIONS ------------- taken from the template project
// draw a text txt on surface screen, starting from the point (x, y)
// charset is a 128x128 bitmap containing character images
void drawString(SDL_Surface *screen, int x, int y, const char *text, SDL_Surface *charset) {
int px, py, c;
SDL_Rect s, d;
s.w = 8;
s.h = 8;
d.w = 8;
d.h = 8;
while(*text) {
c = *text & 255;
px = (c % 16) * 8;
py = (c / 16) * 8;
s.x = px;
s.y = py;
d.x = x;
d.y = y;
SDL_BlitSurface(charset, &s, screen, &d);
x += 8;
text++;
};
};
// draw a surface sprite on a surface screen in point (x, y)
// (x, y) is the center of sprite on screen
void drawSurface(SDL_Surface *screen, SDL_Surface *sprite, int x, int y) {
SDL_Rect dest;
dest.x = x - sprite->w / 2;
dest.y = y - sprite->h / 2;
dest.w = sprite->w;
dest.h = sprite->h;
SDL_BlitSurface(sprite, NULL, screen, &dest);
};
// draw a single pixel
void drawPixel(SDL_Surface *surface, int x, int y, Uint32 color) {
int bpp = surface->format->BytesPerPixel;
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
*(Uint32 *)p = color;
};
// draw a vertical (when dx = 0, dy = 1) or horizontal (when dx = 1, dy = 0) line
void drawLine(SDL_Surface *screen, int x, int y, int l, int dx, int dy, Uint32 color) {
for(int i = 0; i < l; i++) {
drawPixel(screen, x, y, color);
x += dx;
y += dy;
};
};
// draw a rectangle of size l by k
void drawRectangle(SDL_Surface *screen, int x, int y, int l, int k,
Uint32 outlineColor, Uint32 fillColor) {
int i;
drawLine(screen, x, y, k, 0, 1, outlineColor);
drawLine(screen, x + l - 1, y, k, 0, 1, outlineColor);
drawLine(screen, x, y, l, 1, 0, outlineColor);
drawLine(screen, x, y + k - 1, l, 1, 0, outlineColor);
for(i = y + 1; i < y + k - 1; i++)
drawLine(screen, x + 1, i, l - 2, 1, 0, fillColor);
};
// ------------- SNAKE FUNCTIONS -------------
void moveSnake(Snake &snake) {
for (int i = snake.length - 1; i > 0; --i) {
snake.body[i] = snake.body[i - 1]; // movement
}
switch (snake.direction) {
case SDLK_UP:
snake.body[0].y -= TILE_SIZE;
break;
case SDLK_DOWN:
snake.body[0].y += TILE_SIZE;
break;
case SDLK_LEFT:
snake.body[0].x -= TILE_SIZE;
break;
case SDLK_RIGHT:
snake.body[0].x += TILE_SIZE;
break;
}
}
void changeDirection(Snake &snake, int newDirection) {
if ((snake.direction == SDLK_UP && newDirection == SDLK_DOWN) ||
(snake.direction == SDLK_DOWN && newDirection == SDLK_UP) ||
(snake.direction == SDLK_LEFT && newDirection == SDLK_RIGHT) ||
(snake.direction == SDLK_RIGHT && newDirection == SDLK_LEFT)) {
return; //prevents reversing
}
snake.direction = newDirection;
}
void addSnakePart(Snake &snakeInfo) {
snakeInfo.body[snakeInfo.length] = snakeInfo.body[snakeInfo.length - 1];
snakeInfo.length++;
}
int forceTurn(Snake &snake) {
int dir = snake.direction;
int x = snake.body[0].x;
int y = snake.body[0].y;
if (x <= TILE_SIZE && dir != SDLK_UP && dir != SDLK_DOWN) {
if (y<TILE_SIZE+50) { return dir = SDLK_DOWN; }
else { return dir = SDLK_UP; }
}
else if (x >= SCR_WIDTH - TILE_SIZE && dir != SDLK_DOWN && dir != SDLK_UP) {
if (y >= SCR_HEIGHT - TILE_SIZE) { return dir = SDLK_UP; }
else { return dir = SDLK_DOWN; }
}
if (y <= TILE_SIZE+50 && dir != SDLK_LEFT && dir != SDLK_RIGHT) {
if (x >= SCR_WIDTH - TILE_SIZE) { return dir = SDLK_LEFT; }
else { return dir = SDLK_RIGHT; }
}
else if (y >= SCR_HEIGHT - TILE_SIZE && dir != SDLK_RIGHT && dir != SDLK_LEFT) {
if (x <= TILE_SIZE) { return dir = SDLK_RIGHT; }
else { return dir = SDLK_LEFT; }
}
return dir;
}
// ------------- GAME FUNCTIONS -------------
// Collision
bool checkCollision(const Point &point, const Snake &snake, bool isSnake) {
int l = isSnake ? snake.length : 1; double d = isSnake ? 2.0 : 1.5; int i = isSnake ? 1 : 0;
for (i; i < l; ++i) {
if (((point.x >= snake.body[i].x-TILE_SIZE/d)
&& (point.x <= snake.body[i].x + TILE_SIZE/d))
&& ((point.y >= snake.body[i].y - TILE_SIZE/d)
&& (point.y <= snake.body[i].y + TILE_SIZE/d))) {return true;}} // true = collided
return false;
}
// Food related functions (Red & Blue Dots)
void generateFood(Point &food, const Snake &snake) {
do {
do {food.x = (rand() % (SCR_WIDTH / TILE_SIZE)) * TILE_SIZE;} while (food.x <= TILE_SIZE);
do{ food.y = (rand() % (SCR_HEIGHT / TILE_SIZE)) * TILE_SIZE - TILE_SIZE;} while (food.y <= TILE_SIZE+50);
} while (checkCollision(food, snake, true));
}
void deSpawn(Point &redDot, SDL_Surface *screen, int czarny) {
drawRectangle(screen, redDot.x, redDot.y, TILE_SIZE, TILE_SIZE, czarny, czarny);
redDot.x = -10;
redDot.y = -10;
}
void checkFoodEaten(Snake &snake, Point &food, int &points) {
if (checkCollision(food, snake, false)) {
addSnakePart(snake);
generateFood(food, snake);
points+= POINT_PER_FOOD;
}
}
void redDotFunction(Point &redDot, int &redDotSpawn, double &redDotTimer, Snake &snakeInfo, int &slowness, unsigned int &speed, SDL_Surface *screen, int czarny, int czerwony, int &points) {
if (redDotTimer <= 0) {
redDotSpawn = 0;
redDotTimer = RED_DOT_TIME;
deSpawn(redDot, screen, czarny);
}
if (redDotSpawn==0 && (rand() % RED_DOT_SPAWN_CHANCE) == 1){
redDotSpawn = 1;
generateFood(redDot, snakeInfo);
}
if (checkCollision(redDot, snakeInfo, false)&& redDotTimer > 0){
redDotTimer = RED_DOT_TIME;
points++;
int r = rand() % 2;
if (snakeInfo.length>1 && r == 0) { // shortening the snake
snakeInfo.length -= SHORTEN_SNAKE_LENGHT;
}
else { // slows down
slowness += SPEED_UP*SHORTEN_SNAKE_LENGHT;
speed == 1 ? speed = 1 : speed -= SHORTEN_SNAKE_LENGHT;
}
redDotSpawn = 0;
}
}
void checkSpeedup(double &worldTime, unsigned int &speed, int &slowness, double &lastSpeedupTime) {
if (worldTime - lastSpeedupTime >= SPEED_UP_FREQUENCY) {
slowness -= SPEED_UP;
speed++;
lastSpeedupTime = worldTime; // Update the last speedup time
}
}
// info text
void drawInfoText(SDL_Surface *screen, SDL_Surface *charset, char *text, double &worldTime, double &fps, int &points, unsigned int &speed, int &gameover){
if (!gameover){
sprintf(text, "Elapsed time = %.1lf s %.0lf fps. Points = %.2d. Speed = %.0d", worldTime, fps, points, speed);
drawString(screen, 4, 10, text, charset);
sprintf(text, "Esc - exit, n - new game, arrow keys - moving");
drawString(screen, 4, 20, text, charset);
sprintf(text, "Project by: ,Copyright 2025. ");
drawString(screen, screen->w - strlen(text)*8 -4 , 30, text, charset);
sprintf(text, "Implemented requirements: 1-4, A, B, C, D");
drawString(screen, screen->w - strlen(text)*8 -4 , 40, text, charset);
}
else {
sprintf(text, "Game Over! Points = %.2d", points);
drawString(screen, screen->w / 2 - strlen(text) * 8 / 2, 20, text, charset);
sprintf(text,"Press n to start a new game or Esc to exit");
drawString(screen, screen->w / 2 - strlen(text) * 8 / 2, 30, text, charset);
}
}
// New Game (n)
void newGame(Snake &snake, Point &food, int &frames, double &worldTime, double &fpsTimer, double &fps, int &t1, Uint32 &lastMove) {
snake = {{{INITIAL_POS_X, INITIAL_POS_Y}}, INITIAL_LENGTH, SDLK_RIGHT};
generateFood(food, snake);
frames = 0;
worldTime = 0;
fpsTimer = 0;
fps = 0;
t1 = SDL_GetTicks();
lastMove = SDL_GetTicks();
}
// Loading BMPs
SDL_Surface* loadSurface(const char *path) {
SDL_Surface* s = SDL_LoadBMP(path);
if (s == NULL) {
printf("Unable to load image %s! SDL Error: %s\n", path, SDL_GetError());
return NULL;
}
return s;
}
//
// ------------- MAIN FUNCTION -------------
#ifdef __cplusplus
extern "C"
#endif
int main(int argc, char **argv) {
int t1, t2, quit, frames, rc, points, slowness, gameover;
unsigned int speed;
double delta, worldTime, lastSpeedup, fpsTimer, fps;
SDL_Event event;
SDL_Surface *screen, *charset, *snake, *food, *reddot;
SDL_Texture *scrtex;
SDL_Window *window;
SDL_Renderer *renderer;
if(SDL_Init(SDL_INIT_EVERYTHING) != 0) { printf("SDL_Init error: %s\n", SDL_GetError()); return 1;}
rc = SDL_CreateWindowAndRenderer(SCR_WIDTH, SCR_HEIGHT, 0,
&window, &renderer);
if(rc != 0) { SDL_Quit(); printf("SDL_CreateWindowAndRenderer error: %s\n", SDL_GetError()); return 1; };
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
SDL_RenderSetLogicalSize(renderer, SCR_WIDTH, SCR_HEIGHT);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_SetWindowTitle(window, "Snake Game");
screen = SDL_CreateRGBSurface(0, SCR_WIDTH, SCR_HEIGHT, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
scrtex = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, SCR_WIDTH, SCR_HEIGHT);
// turning off cursor
SDL_ShowCursor(SDL_DISABLE);
// loading bmp
charset = loadSurface("./cs8x8.bmp"); //SDL_LoadBMP("./cs8x8.bmp");
food = loadSurface("./food.bmp");
snake = loadSurface("./snakebody.bmp");
reddot = loadSurface("./reddot.bmp");
if(charset == NULL || food == NULL || snake == NULL || reddot == NULL) {
printf("SDL_LoadBMP error: %s\n", SDL_GetError());
SDL_FreeSurface(screen);
SDL_DestroyTexture(scrtex);
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_Quit();
return 1;
};
SDL_SetColorKey(charset, true, 0x000000);
//creating initial snake and food
Snake snakeInfo = {{{INITIAL_POS_X, INITIAL_POS_Y}}, INITIAL_LENGTH, SDLK_RIGHT};
Point foodInfo;
Point redDot; double redDotTimer = RED_DOT_TIME; int redDotSpawn = 0;
generateFood(foodInfo, snakeInfo);
char text[128];
int czarny = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
int zielony = SDL_MapRGB(screen->format, 0x00, 0xFF, 0x00);
int czerwony = SDL_MapRGB(screen->format, 0xFF, 0x00, 0x00);
int niebieski = SDL_MapRGB(screen->format, 0x11, 0x11, 0xCC);
t1 = SDL_GetTicks();
frames = 0; fpsTimer = 0; fps = 0; worldTime = 0; lastSpeedup = 0;
points = 0;
slowness = 100; // Increase value to slow down
speed = 1; // To indicate speed level
Uint32 lastMove = SDL_GetTicks(); // For appropriate movement
gameover = 0; quit = 0;
while(!quit) {
t2 = SDL_GetTicks();
delta = (t2 - t1) * 0.001; t1 = t2;
worldTime += delta;
if (redDotSpawn){redDotTimer -= delta;}
SDL_FillRect(screen, NULL, czarny);
drawSurface(screen, food, foodInfo.x, foodInfo.y);
for (int i = 0; i < snakeInfo.length; i++) {
drawSurface(screen, snake, snakeInfo.body[i].x, snakeInfo.body[i].y);
}
if (redDotSpawn){
drawSurface(screen, reddot, redDot.x, redDot.y);
drawRectangle(screen, 0, 52, (SCR_WIDTH/RED_DOT_TIME)*redDotTimer , 4, czerwony, czerwony);
}
fpsTimer += delta;
if(fpsTimer > 0.5) {
fps = frames * 2;
frames = 0;
fpsTimer -= 0.5;
};
// info text
drawRectangle(screen, 4, 4, SCR_WIDTH - 8, 50, czerwony, niebieski);
drawInfoText(screen, charset, text, worldTime, fps, points, speed, gameover);
SDL_UpdateTexture(scrtex, NULL, screen->pixels, screen->pitch);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, scrtex, NULL, NULL);
SDL_RenderPresent(renderer);
checkSpeedup(worldTime, speed, slowness, lastSpeedup);
// handling of events
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_KEYDOWN:
if(event.key.keysym.sym == SDLK_ESCAPE) quit = 1;
else if(event.key.keysym.sym == SDLK_n) {
newGame(snakeInfo, foodInfo, frames, fpsTimer, fps, worldTime, t1, lastMove);
gameover = 0;
}
else if(event.key.keysym.sym == SDLK_LEFT) changeDirection(snakeInfo, SDLK_LEFT);
else if(event.key.keysym.sym == SDLK_RIGHT) changeDirection(snakeInfo, SDLK_RIGHT);
else if(event.key.keysym.sym == SDLK_UP) changeDirection(snakeInfo, SDLK_UP);
else if(event.key.keysym.sym == SDLK_DOWN) changeDirection(snakeInfo, SDLK_DOWN);
break;
case SDL_QUIT:
quit = 1;
break;
};
};
frames++;
if (!gameover && SDL_GetTicks() - lastMove > slowness) {
moveSnake(snakeInfo);
if (snakeInfo.body[0].x < TILE_SIZE || snakeInfo.body[0].x >= SCR_WIDTH-TILE_SIZE || snakeInfo.body[0].y < TILE_SIZE+45|| snakeInfo.body[0].y >= SCR_HEIGHT-TILE_SIZE){
snakeInfo.direction= forceTurn(snakeInfo);
}
checkFoodEaten(snakeInfo, foodInfo, points);
redDotFunction(redDot, redDotSpawn, redDotTimer, snakeInfo, slowness, speed, screen, czarny, czerwony, points);
if (snakeInfo.length > 2 && checkCollision(snakeInfo.body[0], snakeInfo, true)){ gameover = 1; }
lastMove = SDL_GetTicks();
}
};
// freeing all surfaces
SDL_FreeSurface(charset);
SDL_FreeSurface(screen);
SDL_DestroyTexture(scrtex);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
};