jag

snake_c

public

Small snake game in C

Star0
Watch0

Commit 075f7abb96cd

tarball

Jaglekon <jaglekon@jaglekon.de>2026-03-23 15:42 UTC

1 file changed+23-31
fixing some logi
Modified file: snake.c+23 additions−31 deletions
@@ -1,3 +1,5 @@
1+#define _POSIX_C_SOURCE 199309L
2+
13 #include <termios.h>
24 #include <unistd.h>
35
@@ -60,8 +62,9 @@ selected new_fruit(char board[HEIGHT][WIDTH][STRING_LEN]);
6062 bool draw_menu(main_menu menu, struct winsize window_size);
6163
6264 bool draw_board(char board[HEIGHT][WIDTH][STRING_LEN],
63- struct winsize window_size, bool redraw);
64-void draw_pause(struct winsize window_size);
65+ struct winsize window_size, bool redraw, bool paused);
66+void draw_pause(struct winsize window_size, uint8_t field_width,
67+ uint8_t field_height);
6568
6669 direction read_direction(char *string, bool wasd_enabled);
6770
@@ -102,7 +105,7 @@ int main(int argc, char *args[]) {
102105
103106 new.c_lflag &= ~(ECHO | ICANON);
104107 new.c_cc[VMIN] = 0;
105- new.c_cc[VTIME] = 1;
108+ new.c_cc[VTIME] = 0;
106109
107110 tcsetattr(STDIN_FILENO, TCSANOW, &new);
108111
@@ -221,17 +224,9 @@ int main(int argc, char *args[]) {
221224
222225 bool paused = false;
223226
224- if (current_size.ws_row < ACTUAL_HEIGHT ||
225- current_size.ws_col < ACTUAL_WIDTH) {
226- too_small = true;
227+ if ((too_small = draw_board(board, current_size, true, false)))
227228 paused = true;
228229
229- printf("\033[2J\033[HToo small\n");
230- printf("Min width: %i & Min height: %i\n", ACTUAL_WIDTH, ACTUAL_WIDTH);
231- } else {
232- draw_board(board, current_size, true);
233- }
234-
235230 direction shift = RIGHT;
236231 direction tmp_direction = shift;
237232
@@ -239,6 +234,8 @@ int main(int argc, char *args[]) {
239234
240235 /* start of the actual game loop */
241236
237+ struct timespec mili100 = {0, 100000000};
238+
242239 while (run) {
243240
244241 /* input handling */
@@ -259,7 +256,7 @@ int main(int argc, char *args[]) {
259256 if (toupper(string[0]) == 'P') {
260257 paused = !paused;
261258 if (paused)
262- draw_pause(current_size);
259+ draw_board(board, current_size, true, paused);
263260 }
264261 }
265262 }
@@ -272,7 +269,7 @@ int main(int argc, char *args[]) {
272269 uint64_t time_diff = (current.tv_sec - start.tv_sec) * 1000000 +
273270 (current.tv_usec - start.tv_usec);
274271
275- if (time_diff >= SPEED * 1000) {
272+ if (/*time_diff >= SPEED * 1000*/ true) {
276273
277274 ioctl(STDOUT_FILENO, TIOCGWINSZ, &current_size);
278275
@@ -281,17 +278,7 @@ int main(int argc, char *args[]) {
281278 init_size = current_size;
282279
283280 paused = true;
284-
285- draw_board(board, current_size, true);
286-
287- if (current_size.ws_row < ACTUAL_HEIGHT ||
288- current_size.ws_col < ACTUAL_WIDTH) {
289- too_small = true;
290- } else {
291- too_small = false;
292-
293- draw_pause(current_size);
294- }
281+ too_small = draw_board(board, current_size, true, paused);
295282 }
296283
297284 if (!paused) {
@@ -349,9 +336,10 @@ int main(int argc, char *args[]) {
349336 }
350337
351338 update_field(my_snake, board, fruit);
352- draw_board(board, current_size, false);
339+ draw_board(board, current_size, false, false);
353340 }
354341 }
342+ nanosleep(&mili100, NULL);
355343 }
356344
357345 end_of_program:
@@ -546,7 +534,7 @@ bool draw_menu(main_menu menu, struct winsize window_size) {
546534 }
547535
548536 bool draw_board(char board[HEIGHT][WIDTH][STRING_LEN],
549- struct winsize window_size, bool redraw) {
537+ struct winsize window_size, bool redraw, bool paused) {
550538
551539 uint8_t i, j;
552540
@@ -601,12 +589,16 @@ bool draw_board(char board[HEIGHT][WIDTH][STRING_LEN],
601589 printf("--");
602590 printf("+");
603591
592+ if (paused)
593+ draw_pause(window_size, actual_width, actual_height);
594+
604595 fflush(stdout);
605596
606597 return false;
607598 }
608599
609-void draw_pause(struct winsize window_size) {
600+void draw_pause(struct winsize window_size, uint8_t field_width,
601+ uint8_t field_height) {
610602
611603 const uint8_t height = 5;
612604 const uint8_t width = 14;
@@ -615,10 +607,10 @@ void draw_pause(struct winsize window_size) {
615607 "| PAUSED |", "| |",
616608 "+------------+"};
617609
618- const uint8_t going_up = (ACTUAL_HEIGHT + height) >> 1;
619- const uint8_t going_right = (ACTUAL_WIDTH - width) >> 1;
610+ const uint8_t going_up = (field_height + height) >> 1;
611+ const uint8_t going_right = (field_width - width) >> 1;
620612
621- const uint8_t horizontal_buffer = (window_size.ws_col - ACTUAL_WIDTH) >> 1;
613+ const uint8_t horizontal_buffer = (window_size.ws_col - field_width) >> 1;
622614
623615 uint8_t i, j;