jag

snake_c

public

Small snake game in C

Star0
Watch0

Commit 94b4a7823fa0

tarball

Jaglekon <58274093+Jaglekon@users.noreply.github.com>2026-04-03 22:10 UTC

1 file changed+73-18
almost
Modified file: drawing.c+73 additions−18 deletions
@@ -536,36 +536,83 @@ void draw_pause(struct winsize window_size, uint8_t field_width,
536536
537537 void draw_highscore (int16_t new_score, score leader_board[SCORE_NUMBER], struct winsize window_size) {
538538
539- uint8_t i;
539+ uint8_t i, j;
540540
541- printf("\033[2J\033[H");
541+ char *header[] = {
542+ " _ _ ___ ___ _ _ ",
543+ " | || |_ _/ __| || | ",
544+ " | __ || | (_ | __ | ",
545+ " _|_||_|___\\___|_||_|_ ",
546+ "/ __|/ __/ _ \\| _ \\ __|",
547+ "\\__ \\ (_| (_) | / _| ",
548+ "|___/\\___\\___/|_|_\\___|",
549+ " "
550+ };
542551
543- printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");
552+ uint8_t header_height = 8;
553+ uint8_t header_width = strlen(header[0]);
544554
545- printf("┃ RANK NAME SCORE ┃\n");
546- printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n");
547-
555+ uint8_t full_height = header_height + SCORE_NUMBER + 5 + 1;
556+ uint8_t full_width = header_width + 4;
548557
549- for (i = 0; i < SCORE_NUMBER; i++) {
558+ uint8_t gap_name = 9;
550559
551- char *ordinals[] = {
552- "st", "nd", "rd", "th"
553- };
560+ printf("\033[2J\033[H");
554561
555- printf("┃ %2d%-4s %-9s %06d ┃\n",i + 1, (i >= 3) ? ordinals[3] : ordinals[i] ,leader_board[i].name, leader_board[i].points);
556- printf("┃ ┃\n");
557-
562+ if (window_size.ws_col < full_width || window_size.ws_row < full_height) {
563+ printf("Too small\n");
564+ printf("Min size: x: %d y: %d", full_width, full_height);
565+ fflush(stdout);
566+ return;
558567 }
559-
560- printf("\033[%c┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛", UP_CODE);
561-
568+
569+ for (i = 0; i < (window_size.ws_row - full_height) >> 1; i++)
570+ printf("\n");
571+
572+ for (i = 0; i < full_height - 1; i++) {
573+
574+ for (j = 0; j < (window_size.ws_col - full_width) >> 1; j++)
575+ printf(" ");
576+
577+ if (!i) {
578+ printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");
579+ } else if (i < header_height + 1) {
580+ printf("┃ \033[31;1m%s\033[0m ┃\n", header[i - 1]);
581+ } else if(i < header_height + 2) {
582+ printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━┫\n");
583+ } else if (i < header_height + 3) {
584+ printf("┃ RANK NAME SCORE ┃\n");
585+ } else if (i < header_height + 4) {
586+ printf("┠─────────────────────────┨\n");
587+ } else if (i < header_height + 4 + SCORE_NUMBER) {
588+ uint8_t pos = i - header_height - 4;
589+ char *ordinals[] = {
590+ "st", "nd", "rd", "th"
591+ };
592+
593+ printf("┃ \033[%dm%2d%-4s %-9s %06d\033[0m ┃\n", (pos == new_score) ? 32 : 0 ,pos + 1, (pos >= 3) ? ordinals[3] : ordinals[pos] ,leader_board[pos].name, leader_board[pos].points);
594+ } else {
595+ printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");
596+ }
597+ }
598+
562599 fflush(stdout);
563600
564601 if (new_score != -1) {
565- for (i = (SCORE_NUMBER - new_score) * 2 - 1; i > 0; i--)
602+
603+ char *input = "Put your Name in";
604+
605+ for (i = 0; i < (window_size.ws_col - strlen(input)) >> 1; i++)
606+ printf(" ");
607+ printf("%s", input);
608+
609+
610+ for (i = (SCORE_NUMBER - new_score) + 1; i > 0; i--)
566611 printf("\033[%c", UP_CODE);
567612 printf("\r");
568- for (i = 0; i < 8 + 1; i++)
613+ for (i = 0; i < (window_size.ws_col - full_width) >> 1; i++)
614+ printf(" ");
615+ for (i = 0; i < gap_name; i++)
569616 printf("\033[%c", RIGHT_CODE);
570617 printf("_");
571618 fflush(stdout);
@@ -599,6 +646,14 @@ void draw_highscore (int16_t new_score, score leader_board[SCORE_NUMBER], struct
599646 struct timespec mili100 = {0, 1000000 * 100};
600647 nanosleep(&mili100, NULL);
601648 }
649+ } else {
650+
651+ char *back = "BACK";
652+
653+ for (i = 0; i < (window_size.ws_col - strlen(back)) >> 1; i++)
654+ printf(" ");
655+ printf("\033[32m%s\033[0m", back);
656+ fflush(stdout);
602657 }
603658 }