-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
357 lines (319 loc) · 6.3 KB
/
main.c
File metadata and controls
357 lines (319 loc) · 6.3 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
#include <curses.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define SNAKE_LEN 3
#define HEAD_UP '^'
#define HEAD_RIGHT '>'
#define HEAD_DOWN 'v'
#define HEAD_LEFT '<'
#define BODY_V '|'
#define BODY_H '-'
#define FOOD_C '#'
#define SPEED 200
#define GAME_OVER "Game Over!"
#define GAME_OVER_MSG "Press Q to quit or R to restart"
typedef enum { up, right, down, left } direction;
typedef struct snake {
int x, y;
direction dir;
struct snake *next, *prev;
} snake_t;
typedef struct food {
int x, y;
} food_t;
unsigned int row, col;
unsigned int score = 0;
static void game_over(void) {
char score_msg[32];
sprintf(score_msg, "Your Score: %d", score);
erase();
attron(COLOR_PAIR(COLOR_GREEN));
mvprintw(row / 2 - 1, (col - strlen(GAME_OVER)) / 2, GAME_OVER);
mvprintw(row / 2, (col - strlen(score_msg)) / 2, "%s", score_msg);
mvprintw(row / 2 + 1, (col - strlen(GAME_OVER_MSG)) / 2, GAME_OVER_MSG);
attroff(COLOR_PAIR(COLOR_GREEN));
timeout(-1);
int ch;
while (1) {
ch = getch();
if (ch == 'q') {
return;
}
}
}
/*
* Spawns the food at random co ordinates
*/
static void spawn_food(snake_t *snake, food_t *food) {
int x = rand() % col;
int y = rand() % row;
bool flag = false;
snake_t *temp;
temp = snake;
// Check if location is on the snake
while (temp) {
if (x == temp->x && y == temp->y) {
flag = true;
break;
}
temp = temp->next;
}
// Try again
if (flag == true) {
spawn_food(snake, food);
} else {
food->x = x;
food->y = y;
;
}
}
// Increase the snake length
static void add_part(snake_t *snake) {
snake_t *temp, *prev;
temp = snake;
while (temp->next) {
temp = temp->next;
}
temp->next = malloc(sizeof(snake_t));
prev = temp;
temp = temp->next;
switch (prev->dir) {
case up:
temp->x = prev->x;
temp->y = prev->y - 1;
break;
case right:
temp->x = prev->x - 1;
temp->y = prev->y;
break;
case down:
temp->x = prev->x;
temp->y = prev->y + 1;
break;
case left:
temp->x = prev->x + 1;
temp->y = prev->y;
break;
}
temp->dir = prev->dir;
temp->next = NULL;
temp->prev = prev;
}
/*
* Initialise ncurses
*/
static void init(void) {
initscr();
start_color();
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_YELLOW, COLOR_BLACK);
raw();
noecho();
timeout(SPEED);
keypad(stdscr, TRUE);
getmaxyx(stdscr, row, col);
curs_set(0);
}
/*
* Initialise snake
*/
static void init_snake(snake_t *snake) {
snake->dir = right;
snake->x = col / 2 + 1;
snake->y = row / 2;
snake->prev = NULL;
snake->next = NULL;
for (int i = 0; i < SNAKE_LEN; ++i) {
add_part(snake);
}
}
/*
* Print snake
*/
static void print_snake(snake_t *snake) {
erase();
snake_t *temp;
temp = snake;
// Drawn head
char head;
switch (snake->dir) {
case up:
head = HEAD_UP;
break;
case right:
head = HEAD_RIGHT;
break;
case down:
head = HEAD_DOWN;
break;
case left:
head = HEAD_LEFT;
break;
}
attron(COLOR_PAIR(COLOR_RED));
mvaddch(temp->y, temp->x, head);
attroff(COLOR_PAIR(COLOR_RED));
// Draw body
temp = temp->next;
char body;
attron(COLOR_PAIR(COLOR_GREEN));
while (temp) {
if (temp->dir == down || temp->dir == up) {
body = BODY_V;
} else {
body = BODY_H;
}
mvaddch(temp->y, temp->x, body);
temp = temp->next;
}
attroff(COLOR_PAIR(COLOR_GREEN));
mvprintw(row - 1, 0, "Score: %d", score);
}
/*
* Move snake in direction of head, checking if it collides with food
*/
static bool move_snake(snake_t *snake, food_t *food) {
snake_t *temp;
temp = snake;
bool alive = true;
static int speed = SPEED;
// Find last part
while (temp->next) {
temp = temp->next;
}
// Set part location to previous part location
while (temp->prev) {
temp->x = temp->prev->x;
temp->y = temp->prev->y;
temp->dir = temp->prev->dir;
temp = temp->prev;
}
// Move snake head in original or specified direction
int ch = getch();
switch (ch) {
case 'k':
case KEY_UP:
if (snake->dir != down) {
snake->dir = up;
}
break;
case 'l':
case KEY_RIGHT:
if (snake->dir != left) {
snake->dir = right;
}
break;
case 'j':
case KEY_DOWN:
if (snake->dir != up) {
snake->dir = down;
}
break;
case 'h':
case KEY_LEFT:
if (snake->dir != right) {
snake->dir = left;
}
break;
case 'q':
alive = false;
break;
default:
break;
}
if (alive == false)
return alive;
// Let snake wrap around
switch (snake->dir) {
case up:
if (snake->y == 0) {
snake->y = row - 1;
} else {
--snake->y;
}
break;
case right:
if (snake->x == col - 1) {
snake->x = 0;
} else {
++snake->x;
}
break;
case down:
if (snake->y == row - 1) {
snake->y = 0;
} else {
++snake->y;
}
break;
case left:
if (snake->x == 0) {
snake->x = col - 1;
} else {
--snake->x;
}
break;
}
// If snake collides with food, respawn food and increase snake length
if (snake->x == food->x && snake->y == food->y) {
add_part(snake);
speed = speed >= 50 ? speed * 0.9 : 50;
score += 10;
timeout(speed);
spawn_food(snake, food);
}
temp = snake->next;
while (temp) {
if (temp->x == snake->x && temp->y == snake->y) {
alive = false;
break;
} else {
temp = temp->next;
}
}
return alive;
}
/*
* Prints the food
*/
static void print_food(food_t *food) {
attron(COLOR_PAIR(COLOR_YELLOW));
mvaddch(food->y, food->x, FOOD_C);
attroff(COLOR_PAIR(COLOR_YELLOW));
}
int main(void) {
// Initialise ncurses
init();
// Initialise snake
snake_t *snake;
snake = malloc(sizeof(snake_t));
food_t *food;
food = malloc(sizeof(food_t));
init_snake(snake);
spawn_food(snake, food);
// Game loop
while (1) {
print_snake(snake);
print_food(food);
if (move_snake(snake, food) == false) {
game_over();
break;
}
}
// Cleanup before exit
free(food);
snake_t *temp = snake;
snake_t *prev = NULL;
while (temp) {
prev = temp;
temp = temp->next;
free(prev);
}
clear();
endwin();
return EXIT_SUCCESS;
}