-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.c
156 lines (129 loc) · 3.87 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
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <stdbool.h>
#include <ncurses.h>
#include "snake.h"
void draw(Snake*, Point*);
int main(void) {
int rowc = 0;
int colc = 0;
// initialize ncurses
initscr();
noecho();
raw();//cbreak();
halfdelay(1);
curs_set(0);
keypad(stdscr, true);
// initialize game objects
Point apple = {0};
Snake snakeHead = {0};
Snake* snakeTail = &snakeHead;
getmaxyx(stdscr, rowc, colc);
srand(time(NULL));
apple.x = rand() % colc;
apple.y = rand() % rowc;
snakeHead.position.x = colc / 2;
snakeHead.position.y = rowc / 2;
// start off moving upwards
uint8_t snakeDirection = UP;
uint8_t inputDirection = UP;
bool running = true;
while(running) {
// collect input
int c = getch();
switch (c) {
case KEY_UP:
inputDirection = UP;
break;
case KEY_DOWN:
inputDirection = DOWN;
break;
case KEY_LEFT:
inputDirection = LEFT;
break;
case KEY_RIGHT:
inputDirection = RIGHT;
break;
}
// can't go immediately backwards
if (inputDirection != ((uint8_t)~snakeDirection)) {
snakeDirection = inputDirection;
}
int dx = 0;
int dy = 0;
switch (snakeDirection) {
case UP:
dy = -1;
break;
case DOWN:
dy = 1;
break;
case LEFT:
dx = -1;
break;
case RIGHT:
dx = 1;
break;
}
// move each piece before the head to the previous piece's spot
Snake* snakePart = snakeTail;
while (snakePart->previous != NULL) {
int tempX = snakePart->previous->position.x;
int tempY = snakePart->previous->position.y;
snakePart->position.x = tempX;
snakePart->position.y = tempY;
snakePart = snakePart->previous;
}
// move the head
snakeHead.position.x += dx;
snakeHead.position.y += dy;
// look for collision with tail
snakePart = &snakeHead;
while (snakePart->next != NULL) {
snakePart = snakePart->next;
if (snakeHead.position.x == snakePart->position.x &&
snakeHead.position.y == snakePart->position.y) {
running = false;
}
}
// look for collision with edges
if (snakeHead.position.x < 0 || snakeHead.position.y < 0 ||
snakeHead.position.x >= colc || snakeHead.position.y >= rowc) {
running = false;
}
// look for collisions with apple
if (snakeHead.position.x == apple.x && snakeHead.position.y == apple.y) {
// grow the snake (it will be visible next iteration)
Snake* newTail = malloc(sizeof(Snake));
newTail->next = NULL;
newTail->previous = snakeTail;
newTail->position.x = snakePart->position.x;
newTail->position.y = snakePart->position.y;
snakeTail->next = newTail;
snakeTail = newTail;
// relocate the apple
apple.x = rand() % colc;
apple.y = rand() % rowc;
}
draw(&snakeHead, &apple);
move(1, 1);
usleep(50000);
}
endwin();
return 0;
}
void draw(Snake* snake, Point* apple) {
int x = apple->x;
int y = apple->y;
erase();
move(y, x);
printw("@");
while (snake != NULL) {
x = snake->position.x;
y = snake->position.y;
move(y, x);
printw("O");
snake = snake->next;
}
}