jag

snake_c

public

Small snake game in C

Star0
Watch0

Commit 9666590efb5c

tarball

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

2 files changed+16-16
gitignore
Added file: .gitignore+1 additions
@@ -0,0 +1 @@
1+snake
Modified file: snake.c+15 additions−16 deletions
@@ -13,8 +13,8 @@
1313 #define UP_CODE 'A'
1414 #define DOWN_CODE 'B'
1515
16-#define WIDTH 4
17-#define HEIGHT 4
16+#define WIDTH 256
17+#define HEIGHT 256
1818
1919 typedef enum {
2020 UP, DOWN, LEFT, RIGHT
@@ -26,7 +26,7 @@ typedef struct selected {
2626 } selected;
2727
2828
29-void draw_board(selected square_selected);
29+void draw_board(selected square_selected, bool redraw);
3030
3131
3232 int main() {
@@ -46,7 +46,7 @@ int main() {
4646
4747 selected square_selected = {0, 0};
4848
49- draw_board(square_selected);
49+ draw_board(square_selected, false);
5050
5151 bool run = true;
5252
@@ -95,12 +95,7 @@ int main() {
9595 break;
9696 }
9797
98- uint8_t i;
99- for (i = 0; i < HEIGHT * 2; i++)
100- printf("\033[A");
101- printf("\r");
102-
103- draw_board(square_selected);
98+ draw_board(square_selected, true);
10499
105100 }
106101 }
@@ -117,22 +112,26 @@ int main() {
117112 }
118113
119114
120-void draw_board (selected square_selected) {
115+void draw_board (selected square_selected, bool redraw) {
121116
122117 uint8_t i, j;
123118
119+ if (redraw) {
120+ for (i = 0; i < HEIGHT; i++)
121+ printf("\033[A");
122+ printf("\r");
123+ }
124+
124125 for ( i = 0; i < HEIGHT; i++) {
125126 for (j = 0; j < WIDTH; j++) {
126- char character = 'X';
127- uint8_t color = 31;
127+ char character = ' ';
128128
129129 if (i == square_selected.y && j == square_selected.x) {
130130 character = 'O';
131- color = 32;
132131 }
133132
134- printf("\033[%um%5c\033[0m", color,color, character);
133+ printf("\033[32m%5c\033[0m", character);
135134 }
136- printf("\n\n");
135+ printf("\n");
137136 }
138137 }