-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.c
393 lines (341 loc) · 9.08 KB
/
snake.c
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
#include "snake.h"
#define SCREEN_WIDTH 1920
#define SCREEN_HEIGTH 1080
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 800
// laptop screen
#if 0
// 1920/2+80
#define WINDOW_X 1040
#define WINDOW_Y 140
// 2nd screen
#else
// 1920 + 1920/2+80
#define WINDOW_X 2960
#define WINDOW_Y 140
#endif
#define GRID_SIZE 30
#define CELL_SIZE 20
#define PAD_X 100
#define PAD_Y 100
// 30^2=900
#define SNAKE_MAX_LEN 900
#define FONT "JetBrainsMono-Light.ttf"
#define FONT_SIZE 50
typedef enum { UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3, NONE = 4 } Direction;
typedef struct {
size_t x;
size_t y;
} Cell;
typedef struct {
size_t len;
Cell body[SNAKE_MAX_LEN];
Cell *head;
Direction dir;
} Snake;
typedef struct {
size_t x;
size_t y;
// size_t lifetime_sec;
} Apple;
size_t rand_size_t(size_t min, size_t max) {
return min + (rand() % (max - min + 1));
}
void SDL_RenderFillCircle(SDL_Renderer *renderer, int center_x, int center_y,
int r, SDL_Color c) {
SDL_SetRenderDrawColor(renderer, c.r, c.g, c.b, c.a);
int x, y_top, y_bot;
for (int dy = 0; dy < r; ++dy) {
x = sqrt(r * r - dy * dy);
y_top = center_y + dy;
y_bot = center_y - dy;
SDL_RenderDrawLine(renderer, center_x - x, y_top, center_x + x, y_top);
SDL_RenderDrawLine(renderer, center_x - x, y_bot, center_x + x, y_bot);
}
}
void grid_render(SDL_Renderer *renderer) {
SDL_SetRenderDrawColor(renderer, 0x40, 0x40, 0x50, 0xFF);
SDL_Rect rect = (SDL_Rect){.w = CELL_SIZE, .h = CELL_SIZE};
for (size_t i = 0; i < GRID_SIZE; ++i) {
rect.y = PAD_Y + (i * CELL_SIZE);
for (size_t j = 0; j < GRID_SIZE; ++j) {
rect.x = PAD_X + (j * CELL_SIZE);
if (i == 0 || j == 0 || i == GRID_SIZE - 1 || j == GRID_SIZE - 1) {
// fill outer rects
SDL_RenderFillRect(renderer, &rect);
} else {
SDL_RenderDrawRect(renderer, &rect);
}
}
}
}
void apple_render(SDL_Renderer *renderer, Apple apple) {
#if 0
// square apple
SDL_Rect rect = {
.h = CELL_SIZE,
.w = CELL_SIZE,
.x = PAD_X + CELL_SIZE * apple.x,
.y = PAD_Y + CELL_SIZE * apple.y,
};
SDL_SetRenderDrawColor(renderer, 0xDD, 0x23, 0x23, 0xFF);
SDL_RenderFillRect(renderer, &rect);
#else
// circular apple
SDL_Color color = {0xDD, 0x23, 0x23, 0xFF};
size_t r = CELL_SIZE / 2;
size_t x = PAD_X + CELL_SIZE * apple.x + r;
size_t y = PAD_Y + CELL_SIZE * apple.y + r;
SDL_RenderFillCircle(renderer, x, y, r, color);
#endif
}
void apple_spawn(Apple *apple, Snake snake) {
size_t x, y;
bool is_on_snake = true;
while (is_on_snake) {
is_on_snake = false;
x = rand_size_t(1, GRID_SIZE - 2);
y = rand_size_t(1, GRID_SIZE - 2);
for (size_t i = 0; i < snake.len; ++i) {
if (x == snake.body[i].x && y == snake.body[i].y) {
is_on_snake = true;
break;
}
}
}
apple->x = x;
apple->y = y;
}
void snake_render(SDL_Renderer *renderer, Snake snake) {
SDL_SetRenderDrawColor(renderer, 0x33, 0xCC, 0x33, 0xFF);
SDL_Rect rect = {.h = CELL_SIZE, .w = CELL_SIZE};
for (size_t i = 0; i < snake.len; ++i) {
rect.x = PAD_X + snake.body[i].x * CELL_SIZE;
rect.y = PAD_Y + snake.body[i].y * CELL_SIZE;
SDL_RenderFillRect(renderer, &rect);
}
}
void snake_reset(Snake *snake) {
snake->len = 3;
for (size_t i = 0; i < 3; ++i) {
snake->body[i].x = 15 - i;
snake->body[i].y = 15;
}
snake->dir = NONE;
}
void snake_grow(Snake *snake) {
if (snake->len < SNAKE_MAX_LEN) {
++snake->len;
}
}
void snake_move(Snake *snake) {
if (snake->dir != NONE) {
for (size_t i = snake->len - 1; i > 0; --i) {
snake->body[i] = snake->body[i - 1];
}
}
switch (snake->dir) {
case UP:
--snake->head->y;
break;
case DOWN:
++snake->head->y;
break;
case LEFT:
--snake->head->x;
break;
case RIGHT:
++snake->head->x;
break;
case NONE:
break;
}
}
bool is_collision_snake_wall(Snake snake) {
if (snake.head->x < 1 || snake.head->x > GRID_SIZE - 2 || snake.head->y < 1 ||
snake.head->y > GRID_SIZE - 2) {
return true;
}
return false;
}
bool is_collision_snake_apple(Snake snake, Apple apple) {
if (snake.head->x == apple.x && snake.head->y == apple.y) {
return true;
}
return false;
}
bool is_collision_snake_snake(Snake snake) {
for (size_t i = 1; i < snake.len; ++i) {
if (snake.head->x == snake.body[i].x && snake.head->y == snake.body[i].y) {
return true;
}
}
return false;
}
int main() {
/*************
* Setup SDL *
*************/
if (SDL_INIT_VIDEO < 0) {
fprintf(stderr, "ERROR: SDL_INIT_VIDEO");
return 1;
}
if (TTF_Init() < 0) {
fprintf(stderr, "ERROR: TTF_Init");
return 1;
}
TTF_Font *font = TTF_OpenFont(FONT, FONT_SIZE);
if (!font) {
fprintf(stderr, "ERROR: Failed to load font");
return 1;
}
SDL_Window *window;
SDL_Renderer *renderer;
window = SDL_CreateWindow("Snake", WINDOW_X, WINDOW_Y, WINDOW_WIDTH,
WINDOW_HEIGHT, SDL_WINDOW_BORDERLESS);
if (!window) {
fprintf(stderr, "ERROR: SDL_CreateWindow");
return 1;
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (!renderer) {
fprintf(stderr, "ERROR: SDL_CreateRenderer");
return 1;
}
/*************
* Game Loop *
**************/
Snake snake;
snake.head = snake.body;
snake_reset(&snake);
bool snake_has_moved;
Apple apple;
apple_spawn(&apple, snake);
size_t score = 0;
size_t highscore = 0;
SDL_Event event;
bool quit = false;
bool paused = false;
while (!quit) {
snake_has_moved = false;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
quit = true;
break;
case SDL_KEYUP:
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
quit = true;
break;
case SDLK_p:
paused = paused ? false : true;
break;
// arrow keys movement
case SDLK_UP:
if (snake.dir != DOWN && !snake_has_moved && !paused) {
snake.dir = UP;
snake_has_moved = true;
}
break;
case SDLK_DOWN:
if (snake.dir != UP && !snake_has_moved && !paused) {
snake.dir = DOWN;
snake_has_moved = true;
}
break;
case SDLK_LEFT:
if (snake.dir != RIGHT && !snake_has_moved && !paused) {
snake.dir = LEFT;
snake_has_moved = true;
}
break;
case SDLK_RIGHT:
if (snake.dir != LEFT && !snake_has_moved && !paused) {
snake.dir = RIGHT;
snake_has_moved = true;
}
break;
// wasd movement
case SDLK_w:
if (snake.dir != DOWN && !snake_has_moved && !paused) {
snake.dir = UP;
snake_has_moved = true;
}
break;
case SDLK_s:
if (snake.dir != UP && !snake_has_moved && !paused) {
snake.dir = DOWN;
snake_has_moved = true;
}
break;
case SDLK_a:
if (snake.dir != RIGHT && !snake_has_moved && !paused) {
snake.dir = LEFT;
snake_has_moved = true;
}
break;
case SDLK_d:
if (snake.dir != LEFT && !snake_has_moved && !paused) {
snake.dir = RIGHT;
snake_has_moved = true;
}
break;
} // switch(event.key.keysym.sym)
break;
} // switch(event.type)
} // while(SDL_PollEvent)
// RENDER START
SDL_RenderClear(renderer);
if (!paused) {
if (is_collision_snake_apple(snake, apple)) {
snake_grow(&snake);
apple_spawn(&apple, snake);
++score;
}
snake_move(&snake);
snake_has_moved = false;
if (is_collision_snake_wall(snake)) {
snake_reset(&snake);
highscore = score > highscore ? score : highscore;
score = 0;
}
if (is_collision_snake_snake(snake)) {
snake_reset(&snake);
highscore = score > highscore ? score : highscore;
score = 0;
}
}
grid_render(renderer);
apple_render(renderer, apple);
snake_render(renderer, snake);
SDL_Surface *text_surface;
SDL_Color color = {0x40, 0x40, 0x50, 0xFF};
char buff[256];
sprintf(buff, "Score: %zu Best: %zu", score, highscore);
text_surface = TTF_RenderText_Solid(font, buff, color);
if (!text_surface) {
fprintf(stderr, "ERROR: Failed to render text");
}
SDL_Texture *text_texture;
text_texture = SDL_CreateTextureFromSurface(renderer, text_surface);
SDL_Rect dest = {130, 25, text_surface->w, text_surface->h};
SDL_FreeSurface(text_surface);
SDL_RenderCopy(renderer, text_texture, NULL, &dest);
SDL_DestroyTexture(text_texture);
SDL_SetRenderDrawColor(renderer, 0x18, 0x18, 0x18, 0xFF); // rgba
SDL_RenderPresent(renderer);
SDL_Delay(100);
// RENDER END
} // while(!quit)
/***********
* Cleanup *
***********/
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return 0;
}