jag

snake_c

public

Small snake game in C

Star0
Watch0

Commit 323d6261ffa8

tarball

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

1 file changed+121-112
animation finished
Modified file: snake.c+121 additions−112 deletions
@@ -55,12 +55,12 @@ void update_field(snake my_snake, char board[HEIGHT][WIDTH][STRING_LEN],
5555 selected fruit);
5656 selected new_fruit(char board[HEIGHT][WIDTH][STRING_LEN]);
5757
58+bool start_animation();
5859 bool draw_menu(main_menu menu, struct winsize window_size);
59-
6060 bool draw_board(char board[HEIGHT][WIDTH][STRING_LEN],
61- struct winsize window_size, bool redraw, bool paused);
62-void draw_pause(struct winsize window_size, uint8_t field_width,
63- uint8_t field_height);
61+ struct winsize window_size, bool redraw, bool paused);
62+void draw_pause(struct winsize window_size,
63+ uint8_t field_width,uint8_t field_height);
6464
6565 direction read_direction(char *string, bool wasd_enabled);
6666
@@ -116,115 +116,11 @@ int main(int argc, char *args[]) {
116116
117117
118118 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];
166119
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';
188120
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- }
121+ /* Animation at the start */
122+ if (!start_animation())
123+ goto end_of_program;
228124
229125 /* Menu */
230126
@@ -350,7 +246,7 @@ int main(int argc, char *args[]) {
350246 if (toupper(string[0]) == 'Q')
351247 run = false;
352248
353- if (toupper(string[0]) == 'P') {
249+ if (toupper(string[0]) == 'P' || string[0] == '\n') {
354250 paused = !paused;
355251 if (paused)
356252 draw_board(board, current_size, true, paused);
@@ -526,6 +422,119 @@ selected new_fruit(char board[HEIGHT][WIDTH][STRING_LEN]) {
526422 return fruit;
527423 }
528424
425+bool start_animation() {
426+
427+ struct winsize current_size;
428+ ioctl(STDOUT_FILENO, TIOCGWINSZ, &current_size);
429+
430+ struct timespec mili10 = {0, 100000 * SPEED};
431+
432+ char *snake[3][7] = {
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+ uint8_t width_snake[3] = {};
463+
464+ {
465+ uint8_t i;
466+
467+ for (i = 0; i < 3; i++)
468+ width_snake[i] = (uint8_t) strlen(snake[i][0]);
469+ }
470+
471+ uint8_t middle;
472+
473+ uint8_t height = 7;
474+ uint8_t width = width_snake[0] + width_snake[1] + width_snake[2];
475+
476+ for (middle = 1; width < current_size.ws_col; middle++)
477+ width += width_snake[1];
478+
479+ int32_t end = current_size.ws_col + width;
480+
481+
482+
483+ while (end >= 0) {
484+
485+ ioctl(STDOUT_FILENO, TIOCGWINSZ, &current_size);
486+
487+ int32_t start = end - width;
488+
489+ int32_t i, j;
490+
491+ char string[1000] = {};
492+ ssize_t length_string = read(STDIN_FILENO, string, sizeof(string) - 1);
493+
494+ if (length_string > 0) {
495+
496+ string[length_string] = '\0';
497+
498+
499+ if (toupper(string[0]) == 'Q')
500+ return false;
501+
502+ if (string[0] == '\n')
503+ break;
504+ }
505+
506+ printf("\033[2J\033[H");
507+
508+ for (j = 0; j < ((height > current_size.ws_row) ? current_size.ws_row : height) ; j++) {
509+
510+ for (i = 0; i < current_size.ws_col; i++) {
511+ int32_t index = i - start;
512+
513+ if (index >= 0 && index < width) {
514+ if (index < width_snake[0]) {
515+ printf("%c", snake[0][j][index]);
516+ } else if (index < width_snake[0] + width_snake[1] * middle) {
517+ printf("%c", snake[1][j][(index - width_snake[0]) % width_snake[1]]);
518+ } else {
519+ printf("%c", snake[2][j][index - (width_snake[0] + width_snake[1] * middle)]);
520+ }
521+ } else {
522+ printf(" ");
523+ }
524+ }
525+
526+ printf("\n");
527+
528+ }
529+ fflush(stdout);
530+
531+ end--;
532+
533+ nanosleep(&mili10, NULL);
534+ }
535+ return true;
536+}
537+
529538 bool draw_menu(main_menu menu, struct winsize window_size) {
530539
531540 char *menu_string[] = {"NEW GAME", "SETTINGS", "EXIT"};