-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.c
481 lines (422 loc) · 13.1 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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#include <curses.h> // for display
#include <unistd.h> // for sleep()
#include <stdlib.h> // for srand()
#include <time.h> // for time()
#define START_LENGTH 5
#define MIN_WINDOW_WIDTH 58
#define MIN_WINDOW_HEIGHT 14
char getDirection(char cd, WINDOW *win);
typedef struct snakeSection {
short int y;
short int x;
struct snakeSection *next;
} snakeSection_t;
typedef struct {
unsigned int length;
snakeSection_t *head;
char direction; // 'U' - UP, 'D' - down, 'L' - left, 'R' - right
} snake_t;
typedef struct {
unsigned int y;
unsigned int x;
char foodDisplayed; // 0 - not displayed, 1 - is displayed
} food_t;
snake_t initializeSnake(void);
void removeSnakeEndFromDisplay(snake_t snake, WINDOW *win);
void drawSnake(snake_t snake, WINDOW *win);
void advanceSnakeForward(snake_t *snake);
snakeSection_t *returnTail(snake_t snake);
char detectCollisions(snake_t snake);
char initNewGame(snake_t *gameSnake, WINDOW *win, unsigned int *score, food_t *food);
char looseMenu(WINDOW *win, unsigned int score);
char playSnake(snake_t *gameSnake, WINDOW *win, unsigned int *score, food_t *food);
char pauseMenu(WINDOW *win, unsigned int score);
char mainMenu(WINDOW *win);
char aboutMenu(WINDOW *win);
int windowWidth;
int windowHeight;
int input; // to read keyboard input
int main(void)
{
unsigned int score = 0;
food_t food; // Hold information about food;
/* gameControl
* This variable determines what game should do next
* i - initialize new game;
* g - play/continue game;
* l - lose menu;
* m - main menu;
* p - pause menu;
* m - main menu
*/
char gameControl = 'i';
initscr(); // initialize curses
windowWidth = COLS;
windowHeight = LINES;
if (windowHeight < MIN_WINDOW_HEIGHT || windowWidth < MIN_WINDOW_WIDTH) {
printf("Min window size is %ix%i. Please resize your terminal window!\n", MIN_WINDOW_WIDTH, MIN_WINDOW_HEIGHT);
delwin(stdscr);
endwin();
refresh();
return 1;
}
WINDOW *win = newwin(windowHeight-2, windowWidth-2, 1, 1);
WINDOW *looseWindow;
cbreak(); // return key immediately
noecho(); // dont print which key is pressed
nodelay(win, TRUE); // use non blocking for getch()
keypad(win, TRUE); // allow special keys
curs_set(0); // hide cursor
snake_t gameSnake = initializeSnake();
//box(win, ACS_VLINE, ACS_HLINE );
box(win, '|', ACS_HLINE );
//mvwprintw( win, 10, 15, "%c", 64 );wrefresh(win);
// draw first snake
drawSnake(gameSnake, win);
wrefresh(win);
// Main Game loop
while (1)
{
switch (gameControl) {
// Initialize new game
case 'i':
gameControl = initNewGame(&gameSnake, win, &score, &food);
break;
// Play/continue snake game
case 'g':
gameControl = playSnake(&gameSnake, win, &score, &food);
break;
// Loose game menu;
case 'l':
gameControl = looseMenu(looseWindow, score);
break;
// Pause menu
case 'p':
gameControl = pauseMenu(looseWindow, score);
break;
// Main menu
case 'm':
gameControl = mainMenu(win);
break;
// About
case 'a':
gameControl = aboutMenu(win);
break;
// Quit game
case 'q':
delwin(win);
delwin(looseWindow);
delwin(stdscr);
endwin();
refresh();
return 0;
default:
break;
}
}
return 0;
}
snake_t initializeSnake(void) {
snake_t gameSnake;
gameSnake.length = 0;
gameSnake.direction = 'R';
snakeSection_t *currentSection = NULL;
currentSection = malloc(sizeof(snakeSection_t));
if (currentSection==NULL) {
// could not allocate memory for snake section
return gameSnake;
}
gameSnake.head = currentSection;
for (int i = 0; i < 5; i++) {
currentSection->y = windowHeight/2;
currentSection->x = (windowWidth/2)-i;
currentSection->next = NULL;
gameSnake.length++;
if (i != 5-1){
currentSection->next = malloc(sizeof(snakeSection_t));
if (currentSection==NULL) {
// could not allocate memory for snake section
return gameSnake;
}
currentSection = currentSection->next;
}
}
return gameSnake;
}
void removeSnakeEndFromDisplay(snake_t snake, WINDOW *win) {
snakeSection_t *currentSection = snake.head;
while (currentSection->next!=NULL) {
currentSection = currentSection->next;
}
mvwaddch(win, currentSection->y, currentSection->x, ' ');
}
void drawSnake(snake_t snake, WINDOW *win) {
snakeSection_t *currentSection = snake.head;
mvwaddch(win, currentSection->y, currentSection->x, '&');
while (currentSection->next!=NULL)
{
currentSection = currentSection->next;
mvwaddch(win, currentSection->y, currentSection->x, '@');
}
}
void advanceSnakeForward(snake_t *snake) {
snakeSection_t *newHead = NULL;
newHead = malloc(sizeof(snakeSection_t));
newHead->x = snake->head->x;
newHead->y = snake->head->y;
newHead->next = snake->head;
snake->head = newHead;
switch (snake->direction)
{
case 'U':
newHead->y--;
break;
case 'D':
newHead->y++;
break;
case 'R':
newHead->x++;
break;
case 'L':
newHead->x--;
break;
default:
// Error case
break;
}
snakeSection_t *currentSection = snake->head;
for (size_t i = 1; i < snake->length; i++)
{
currentSection = currentSection->next;
}
if (currentSection->next != NULL) {
free(currentSection->next);
currentSection->next=NULL;
}
}
snakeSection_t *returnTail(snake_t snake) {
snakeSection_t *currentSection = snake.head;
while (currentSection->next!=NULL)
{
currentSection = currentSection->next;
}
return currentSection;
}
char detectCollisions(snake_t snake) {
// Detect wall collisions
if (snake.head->y == 0 || snake.head->x == 0 || snake.head->y == windowHeight-3 || snake.head->x == windowWidth-3){
return 1;
}
// Detect self biting
snakeSection_t *currentSection = snake.head;
while (currentSection->next != NULL) {
currentSection = currentSection->next;
if(currentSection->x == snake.head->x && currentSection->y == snake.head->y) {
return 1;
}
}
return 0;
}
char getDirection(char cd, WINDOW *win) {
int ch;
char key;
if ((ch = wgetch(win)) == ERR) {
return 0;
}
else {
switch (ch)
{
case KEY_DOWN:
key = 'D';
break;
case KEY_UP:
key = 'U';
break;
case KEY_LEFT:
key = 'L';
break;
case KEY_RIGHT:
key = 'R';
break;
case 'q': // ESC
key = 'p';
return key;
break;
default:
return 0;
}
}
if ((key == 'U' && cd == 'D') || (key == 'D' && cd == 'U') ||
(key == 'R' && cd == 'L') || (key == 'L' && cd == 'R') ) {
return 0;
} else
{
return key;
}
}
char looseMenu(WINDOW *win, unsigned int score){
win = newwin(10, 20, (windowHeight-10)/2, (windowWidth-20)/2);
nodelay(win, TRUE); // use non blocking for getch()
keypad(win, TRUE); // allow special keys
box(win, '|', ACS_HLINE );
mvwprintw(win, 1, 5, "GAME OVER!");
mvwprintw(win, 3, 1, "Score: %d", score*10);
mvwprintw(win, 5, 1, "Restart - Enter");
mvwprintw(win, 6, 1, "Main menu - q");
wrefresh(win);
while (1)
{
usleep(100000);
if ((input = wgetch(win)) != ERR) {
if (input == 10) {
return 'i';
} else if (input == 'q') {
return 'm';
}
}
}
}
char pauseMenu(WINDOW *win, unsigned int score) {
win = newwin(10, 20, (windowHeight-10)/2, (windowWidth-20)/2);
nodelay(win, TRUE); // use non blocking for getch()
keypad(win, TRUE); // allow special keys
box(win, '|', ACS_HLINE );
mvwprintw(win, 1, 5, "PAUSE MENU");
mvwprintw(win, 3, 1, "Score: %d", score*10);
mvwprintw(win, 5, 1, "Return - Enter");
mvwprintw(win, 6, 1, "Restart - Space");
mvwprintw(win, 7, 1, "Main menu - q");
wrefresh(win);
while (1)
{
usleep(100000);
if ((input = wgetch(win)) != ERR) {
if (input == 10) {
return 'g';
} else if (input == ' ') {
return 'i';
} else if (input == 'q') {
return 'm';
}
}
}
}
char mainMenu(WINDOW *win) {
wclear(win);
box(win, '|', ACS_HLINE );
mvwprintw(win, 2, (windowWidth/2)-5, "__SNAKE__");
mvwprintw(win, 3, (windowWidth/2)-5, "@@@@@@@& *");
mvwprintw(win, 5, 5, "New game - Enter");
mvwprintw(win, 6, 5, "Quit - q");
mvwprintw(win, 7, 5, "About - a");
wrefresh(win);
while (1)
{
usleep(100000);
if ((input = wgetch(win)) != ERR) {
if (input == 10) {
return 'i';
} else if (input == 'q') {
return 'q';
} else if (input == 'a') {
return 'a';
}
}
}
}
char aboutMenu(WINDOW *win) {
wclear(win);
box(win, '|', ACS_HLINE );
mvwprintw(win, 2, (windowWidth/2)-5, "ABOUT");
mvwprintw(win, 4, 5, "This game is dedicated to my friend, APE.");
mvwprintw(win, 5, 5, "Even if you feel as chasing impossible goals,");
mvwprintw(win, 6, 5, "smashing your head against the wall and biting");
mvwprintw(win, 7, 5, "yourself, remember that you have another chance");
mvwprintw(win, 8, 5, "to be better for yourself.");
mvwprintw(win, 9, 45, "-Arturs");
mvwprintw(win, windowHeight-4, 5, "Quit - q");
wrefresh(win);
while (1)
{
usleep(100000);
if ((input = wgetch(win)) != ERR) {
if (input == 'q') {
return 'm';
}
}
}
}
char initNewGame(snake_t *gameSnake, WINDOW *win, unsigned int *score, food_t *food) {
*score = 0;
food->foodDisplayed = 0;
wclear(win);
win = newwin(windowHeight-2, windowWidth-2, 1, 1);
nodelay(win, TRUE);
keypad(win, TRUE);
curs_set(0);
*gameSnake = initializeSnake();
wrefresh(win);
return 'g';
}
char playSnake(snake_t *gameSnake, WINDOW *win, unsigned int *score, food_t *food) {
box(win, '|', ACS_HLINE );
drawSnake(*gameSnake, win);
wrefresh(win);
while (1) {
usleep(100000);
// check if food eaten
if (food->foodDisplayed) {
if (gameSnake->head->y == food->y && gameSnake->head->x == food->x) {
food->foodDisplayed = 0;
gameSnake->length++;
*score += 1;
}
}
// remove last bit of snake
removeSnakeEndFromDisplay(*gameSnake, win);
// Get dirrection from user
char newDirrection;
newDirrection = getDirection(gameSnake->direction, win);
if (newDirrection != 0) {
if (newDirrection == 'p') {
return 'p';
}
gameSnake->direction = newDirrection;
}
// move snake head forward
advanceSnakeForward(gameSnake);
mvwaddch(win, gameSnake->head->y, gameSnake->head->x, '&'); //head
mvwaddch(win, gameSnake->head->next->y, gameSnake->head->next->x, '@'); //first part of neck
snakeSection_t *tail = returnTail(*gameSnake);
// calculate food location
snakeSection_t *currentSection; // to iterate over snake
if (!food->foodDisplayed) {
unsigned int r;
srand ( time(NULL) );
char foodOnSnake = 0;
do {
r = rand();
food->y = (r % (windowHeight - 4)) + 1;
food->x = (r % (windowWidth - 4)) + 1;
foodOnSnake = 0;
currentSection = gameSnake->head;
while (currentSection!=NULL) {
if (currentSection->y == food->y && currentSection->x == food->x) {
foodOnSnake = 1;
break;
}
currentSection = currentSection->next;
}
} while (foodOnSnake);
mvwaddch(win, food->y, food->x, '*');
food->foodDisplayed = 1;
}
// Check if snake hits walls or itself
if (detectCollisions(*gameSnake)) {
wrefresh(win);
return 'l';
break;
}
wrefresh(win);
}
}