jag

snake_c

public

Small snake game in C

Star0
Watch0

Commit 815882c55c3a

tarball

Jaglekon <58274093+Jaglekon@users.noreply.github.com>2026-03-23 21:11 UTC

2 files changed+119-18
animation
Modified file: art.txt+7 additions−9 deletions
@@ -1,9 +1,7 @@
1-
2-
3- ________
4- _/ _ \_ ________ _________ _
5- ________/ (_) \ / \ / \_____
6- \ \ \ / \ / \
7- /_______/ _ \ / \ / _____/
8- \_ (_) _ ___\/___ ___\/____ _/
9- \________/
1+ ________
2+ _/ _ \___________________
3+ ________/ (_) \ /\ / \_____
4+ \ \ \ / \ / \
5+ /_______/ _ \ / \ / _____/
6+ \_ (_) ____\/______\/_____/
7+ \________/
Modified file: snake.c+112 additions−9 deletions
@@ -10,7 +10,6 @@
1010
1111 #include <sys/ioctl.h>
1212
13-#include <sys/time.h>
1413 #include <time.h>
1514
1615 #include <ctype.h>
@@ -26,9 +25,6 @@
2625
2726 #define STRING_LEN 20
2827
29-#define ACTUAL_WIDTH ((WIDTH * 2) + 2)
30-#define ACTUAL_HEIGHT (HEIGHT + 2)
31-
3228 #define SPEED 100 /* in ms */
3329
3430 #define MAX_LENGTH 1000
@@ -118,7 +114,117 @@ int main(int argc, char *args[]) {
118114 current_size = init_size;
119115 bool too_small = false;
120116
117+
121118 struct timespec mili100 = {0, 1000000 * SPEED};
119+ struct timespec mili10 = {0, 100000 * SPEED};
120+
121+ {
122+
123+ char *snake[3][7] = {
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+ uint8_t width_snake[3] = {};
154+
155+ {
156+ uint8_t i;
157+
158+ for (i = 0; i < 3; i++)
159+ width_snake[i] = (uint8_t) strlen(snake[i][0]);
160+ }
161+
162+ uint8_t middle;
163+
164+ uint8_t height = 7;
165+ uint8_t width = width_snake[0] + width_snake[1] + width_snake[2];
166+
167+ for (middle = 1; width < current_size.ws_col; middle++)
168+ width += width_snake[1];
169+
170+ int32_t end = current_size.ws_col + width;
171+
172+
173+
174+ while (end >= 0) {
175+
176+ ioctl(STDOUT_FILENO, TIOCGWINSZ, &current_size);
177+
178+ int32_t start = end - width;
179+
180+ int32_t i, j;
181+
182+ char string[1000] = {};
183+ ssize_t length_string = read(STDIN_FILENO, string, sizeof(string) - 1);
184+
185+ if (length_string > 0) {
186+
187+ string[length_string] = '\0';
188+
189+
190+ if (toupper(string[0]) == 'Q')
191+ goto end_of_program;
192+
193+ if (string[0] == '\n')
194+ break;
195+ }
196+
197+ printf("\033[2J\033[H");
198+
199+ for (j = 0; j < ((height > current_size.ws_row) ? current_size.ws_row : height) ; j++) {
200+
201+ for (i = 0; i < current_size.ws_col; i++) {
202+ int32_t index = i - start;
203+
204+ if (index >= 0 && index < width) {
205+ if (index < width_snake[0]) {
206+ printf("%c", snake[0][j][index]);
207+ } else if (index < width_snake[0] + width_snake[1] * middle) {
208+ printf("%c", snake[1][j][(index - width_snake[0]) % width_snake[1]]);
209+ } else {
210+ printf("%c", snake[2][j][index - (width_snake[0] + width_snake[1] * middle)]);
211+ }
212+ } else {
213+ printf(" ");
214+ }
215+ }
216+
217+ printf("\n");
218+
219+ }
220+ fflush(stdout);
221+
222+ end--;
223+
224+ nanosleep(&mili10, NULL);
225+ }
226+
227+ }
122228
123229 /* Menu */
124230
@@ -138,10 +244,6 @@ int main(int argc, char *args[]) {
138244
139245 direction arrow = NO_DIRECTION;
140246
141- /* Check for window size */
142-
143- struct timeval current;
144- gettimeofday(&current, NULL);
145247
146248 if (true) {
147249
@@ -258,8 +360,9 @@ int main(int argc, char *args[]) {
258360
259361 /* game logic */
260362
363+ /*
261364 struct timeval current;
262- gettimeofday(&current, NULL);
365+ gettimeofday(&current, NULL);*/
263366
264367 if (/*time_diff >= SPEED * 1000*/ true) {