-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.c
291 lines (248 loc) · 10 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
#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h> // for sleep
enum Direction {
UP,
DOWN,
LEFT,
RIGHT
};
typedef struct {
int x;
int y;
} Position;
WINDOW* create_newwin(int height, int width, int starty, int startx);
void destroy_win(WINDOW* local_win);
Position* get_border_position(int width, int height, Position borderPosition, int borderSize);
int isColliding(Position* borderPositions, int borderSize, Position pos);
int arePositionsEqual(Position a, Position b);
Position* get_border_position(int width, int height, Position borderPosition, int borderSize) {
Position* borderPositions = malloc(sizeof(Position) * borderSize);
// First loop: Top and Bottom borders
for (int i = 0; i < width; i++) {
borderPositions[i] = (Position){i, 0};
borderPositions[i + width] = (Position){i, height - 1};
}
// Second loop: Left and Right borders
for (int i = 0; i < height - 2; i++) {
borderPositions[2 * width + i] = (Position){0, 1 + i};
borderPositions[2 * width + height - 2 + i] = (Position){width - 1, 1 + i};
}
return borderPositions;
}
int arePositionsEqual(Position a, Position b) {
return a.x == b.x && a.y == b.y;
}
int isColliding(Position* collisionTargets, int numberOfTargets, Position snakePosition) {
for (int i = 0; i < numberOfTargets; i++) {
if (arePositionsEqual(collisionTargets[i], snakePosition)) {
return 1; // Position is on the border
}
}
return 0; // Position is not on the border
}
void updateSnakePosition(Position* snakeBody, int snakeLength, enum Direction direction) {
// Update body
for (int i = snakeLength - 1; i > 0; i--) {
snakeBody[i] = snakeBody[i - 1];
}
// Update head
switch (direction) {
case UP:
snakeBody[0].y -= 1;
break;
case DOWN:
snakeBody[0].y += 1;
break;
case LEFT:
snakeBody[0].x -= 1;
break;
case RIGHT:
snakeBody[0].x += 1;
break;
default:
break;
}
}
void renderSnake(WINDOW* gameWindow, Position* snakeBody, int snakeLength) {
for (int i = 0; i < snakeLength; i++) {
wattron(gameWindow, COLOR_PAIR(2));
if (i == 0) wattron(gameWindow, A_BOLD);
mvwprintw(gameWindow, snakeBody[i].y, snakeBody[i].x, "o");
if (i == 0) wattroff(gameWindow, A_BOLD);
wattroff(gameWindow, COLOR_PAIR(2));
}
}
Position get_random_position(int gameWindowWidth, int gameWindowHeight) {
int randX = (rand() % (gameWindowWidth - 2)) + 1; // +1 because we don't want food to appear on the border
int randY = (rand() % (gameWindowHeight - 2)) + 1;
return (Position){randX, randY};
}
int main(int argc, char* argv[]) {
initscr(); // Start curses mode
start_color(); // Start color functionality
cbreak(); // Line buffering disabled
keypad(stdscr, TRUE); // Enable function keys
nodelay(stdscr, TRUE); // Make getch non-blocking
noecho(); // Don't echo keypresses
// Define color pairs
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_YELLOW, COLOR_BLACK);
init_pair(4, COLOR_WHITE, COLOR_BLACK); // Define color pair
bkgd(COLOR_PAIR(1));
srand(time(NULL)); // Initialization, should only be called once.
// Global
int ch;
int last_ch;
int milisecondsToSleep = 200;
// Game
enum Direction direction = RIGHT;
int gameWindowHeight = 20;
int gameWindowWidth = 60;
Position gameWindowPosition = {3, 3};
int snakeLength = 1; // init snake length
int maxSnakeLength = (gameWindowHeight - 1) * (gameWindowWidth - 1);
Position snakeBody[maxSnakeLength];
snakeBody[0] = (Position){10, 10}; // init head position
Position foodPositions[1];
foodPositions[0] = get_random_position(gameWindowWidth, gameWindowHeight);
// Game window
int borderLength = (gameWindowWidth * 2 + gameWindowHeight * 2) - 4;
Position* borderPositions = get_border_position(gameWindowWidth, gameWindowHeight, gameWindowPosition, borderLength);
WINDOW* gameWindow = create_newwin(gameWindowHeight, gameWindowWidth, gameWindowPosition.y, gameWindowPosition.x);
// UI
Position infoPosition = {1, 1};
int uiWindowHeight = 7;
int uiWindowWidth = 60;
Position uiWindowPosition = {3, 23};
WINDOW* uiWindow = create_newwin(uiWindowHeight, uiWindowWidth, uiWindowPosition.y, uiWindowPosition.x);
// Game loop
while (1) {
// Read all available characters
while ((ch = getch()) != ERR) {
last_ch = ch; // Store the last character
}
if (last_ch != ERR) {
// Key was pressed
switch (last_ch) {
case KEY_UP:
if (direction != DOWN) {
direction = UP;
}
break;
case KEY_DOWN:
if (direction != UP) {
direction = DOWN;
}
break;
case KEY_LEFT:
if (direction != RIGHT) {
direction = LEFT;
}
break;
case KEY_RIGHT:
if (direction != LEFT) {
direction = RIGHT;
}
break;
default:
break;
}
}
// check if tail collides with food
if (isColliding(foodPositions, 1, snakeBody[snakeLength - 1])) {
// check which direction tail moves
enum Direction tailDirection;
Position oldTail = snakeBody[snakeLength - 1];
updateSnakePosition(snakeBody, snakeLength, direction);
snakeLength++;
snakeBody[snakeLength - 1] = oldTail;
foodPositions[0] = get_random_position(gameWindowWidth, gameWindowHeight); // set new random position for food
} else {
updateSnakePosition(snakeBody, snakeLength, direction);
}
// Game rendering
wclear(gameWindow);
renderSnake(gameWindow, snakeBody, snakeLength);
wattron(gameWindow, COLOR_PAIR(3));
mvwprintw(gameWindow, foodPositions[0].y, foodPositions[0].x, "O");
wattroff(gameWindow, COLOR_PAIR(3));
box(gameWindow, 0, 0);
wrefresh(gameWindow); // Update game window
// UI rendering
wclear(uiWindow);
// display information
mvwprintw(uiWindow, infoPosition.y, infoPosition.x, "Your score is: %d", snakeLength);
// mvwprintw(uiWindow, infoPosition.y, infoPosition.x, "Last key pressed: %d", last_ch);
// mvwprintw(uiWindow, infoPosition.y + 1, infoPosition.x, "Your position is x: %d, y: %d", snakeBody[0].x, snakeBody[0].y);
// mvwprintw(uiWindow, infoPosition.y + 2, infoPosition.x, "Food position is x: %d, y: %d", foodPositions[0].x, foodPositions[0].y);
if (isColliding(borderPositions, borderLength, snakeBody[0]) || (snakeLength > 1 && isColliding(&snakeBody[1], snakeLength - 1, snakeBody[0]))) {
wclear(uiWindow);
box(uiWindow, 0, 0);
nodelay(stdscr, FALSE);
wattron(uiWindow, COLOR_PAIR(1));
mvwprintw(uiWindow, infoPosition.y, infoPosition.x, "Your score is: %d", snakeLength);
wattron(uiWindow, A_BOLD);
mvwprintw(uiWindow, infoPosition.y + 1, infoPosition.x, "You lost. Press 'q' to quit or 'r' to restart the game!");
wattroff(uiWindow, A_BOLD);
wattron(uiWindow, COLOR_PAIR(1));
wrefresh(uiWindow);
int ch = getch();
while (ch != 'q' && ch != 'Q' && ch != 'r' && ch != 'R') {
ch = getch();
}
if (ch == 'q' || ch == 'Q') {
break;
} else if (ch == 'r' || ch == 'R') {
// Reset the game sate
nodelay(stdscr, TRUE);
snakeBody[0] = (Position){10, 10};
snakeLength = 1;
foodPositions[0] = get_random_position(gameWindowWidth, gameWindowHeight);
// Refresh windows
wclear(gameWindow);
wclear(uiWindow);
box(gameWindow, 0, 0);
box(uiWindow, 0, 0);
wrefresh(gameWindow);
wrefresh(uiWindow);
continue;
}
}
// if (isColliding(borderPositions, borderLength, snakeBody[0])) {
// mvwprintw(uiWindow, infoPosition.y + 3, infoPosition.x, "Don't cross the border!!!");
// }
// if (isColliding(foodPositions, 1, snakeBody[0])) {
// mvwprintw(uiWindow, infoPosition.y + 3, infoPosition.x, "Colliding with food!!!");
// }
// if (snakeLength > 1 && isColliding(&snakeBody[1], snakeLength - 1, snakeBody[0])) {
// mvwprintw(uiWindow, infoPosition.y + 3, infoPosition.x, "Colliding with itself");
// }
box(uiWindow, 0, 0);
wrefresh(uiWindow); // Update game window
usleep(1000 * milisecondsToSleep);
}
free(borderPositions);
borderPositions = NULL;
destroy_win(gameWindow);
destroy_win(uiWindow);
endwin(); // End curses mode
return 0;
}
WINDOW* create_newwin(int height, int width, int starty, int startx) {
WINDOW* local_win;
local_win = newwin(height, width, starty, startx);
box(local_win, 0, 0); /* 0, 0 gives default characters
* for the vertical and horizontal
* lines */
wrefresh(local_win); /* Show that box */
return local_win;
}
void destroy_win(WINDOW* local_win) {
wborder(local_win, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
wrefresh(local_win);
delwin(local_win);
}