jag

snake_c

public

Small snake game in C

Star0
Watch0

Commit 48f97764a77a

tarball

Jaglekon <jaglekon@jaglekon.de>2026-03-25 15:53 UTC

1 file changed+126-30
speed setting
Modified file: snake.c+126 additions−30 deletions
@@ -25,7 +25,7 @@
2525
2626 #define STRING_LEN 20
2727
28-#define SPEED 100 /* in ms */
28+#define SPEED 50 /* in ms */
2929
3030 #define MAX_LENGTH 1000
3131 #define STARTING_LENGTH 2
@@ -34,6 +34,8 @@ typedef enum { NEW_GAME, SETTINGS, EXIT, MAIN_MENU } main_menu;
3434
3535 typedef enum {HEIGHT_S, WIDTH_S, SPEED_S, BACK_S, SETTINGS_MENU} settings;
3636
37+typedef enum {V_FAST, FAST, AVERAGE, SLOW, V_SLOW, SPEED_L} speed;
38+
3739 typedef enum {
3840 NO_DIRECTION = -1,
3941 UP = 0,
@@ -124,7 +126,7 @@ int main(int argc, char *args[]) {
124126 bool too_small = false;
125127
126128
127- struct timespec mili100 = {0, 1000000 * SPEED};
129+ struct timespec mili100 = {0, 1000000 * 100};
128130
129131
130132 /* Animation at the start */
@@ -139,6 +141,10 @@ int main(int argc, char *args[]) {
139141
140142 main_menu selected_menu = NEW_GAME;
141143 settings selected_setting = HEIGHT_S;
144+ settings current_setting = SETTINGS_MENU;
145+
146+
147+ speed game_speed = AVERAGE;
142148
143149 while (menu) {
144150
@@ -211,62 +217,150 @@ int main(int argc, char *args[]) {
211217 } else {
212218
213219 char *setting_string[] = {
214- "HEIGHT",
215- "WIDTH",
216- "SPEED",
217- "BACK"
220+ " HEIGHT ",
221+ " WIDTH ",
222+ " SPEED ",
223+ " BACK "
218224 };
219225
226+ char *speed_char[] = {
227+ " VERY FAST ",
228+ " FAST ",
229+ " AVERAGE ",
230+ " SLOW ",
231+ " VERY SLOW "
232+ };
233+
234+ uint8_t width = (uint8_t) strlen(setting_string[0]);
235+ uint8_t width_setting = (uint8_t) strlen(speed_char[0]);
220236
221- switch (arrow) {
222- case UP:
223- selected_setting = (selected_setting + (SETTINGS_MENU - 1)) % SETTINGS_MENU;
224- changed_selected = true;
225- break;
226- case DOWN:
227- selected_setting = (selected_setting + 1) % SETTINGS_MENU;
228- changed_selected = true;
229- break;
230- default:
231- break;
232- }
233237
234238 if (changed_selected || change_menu) {
235239
236- uint8_t i;
240+ uint8_t i, j;
237241
238242 printf("\033[2J\033[H");
239243
244+
240245 for (i = 0; i < SETTINGS_MENU; i++) {
241246
247+ printf("+");
248+ for (j = 0; j < width; j++)
249+ printf("-");
250+ printf("+");
251+
252+ if (i != BACK_S) {
253+ printf (" +");
254+
255+ for (j = 0; j < width_setting; j++)
256+ printf("-");
257+ printf("+");
258+ }
259+
260+ printf("\n|");
261+
242262 if (i == selected_setting)
243263 printf("\033[32m");
244264
245265 printf("%s\033[0m", setting_string[i]);
246266
247- if (i < SETTINGS_MENU - 1)
248- printf("\n\n");
267+ printf("|");
268+
269+ if (i != BACK_S) {
270+ printf(" |");
271+ if (current_setting == i)
272+ printf("\033[32m");
273+
274+ if (i == SPEED_S) {
275+ printf("%s", speed_char[game_speed]);
276+ } else {
277+ for (j = 0; j < width_setting; j++)
278+ printf(" ");
279+ }
280+ printf("\033[0m");
281+ printf("|");
282+ }
283+
284+ printf("\n");
285+
286+ if (i != HEIGHT_S) {
287+
288+ printf("+");
289+ for (j = 0; j < width; j++)
290+ printf("-");
291+ printf("+");
292+
293+ if (i != BACK_S) {
294+ printf (" +");
295+
296+ for (j = 0; j < width_setting; j++)
297+ printf("-");
298+ printf("+");
299+ }
300+
301+ if (i < SETTINGS_MENU - 1)
302+ printf("\n");
303+ }
249304 }
250305 fflush(stdout);
251306
252307 change_menu = false;
253308 }
254309
255-
256- if (pressed_enter) {
257- switch (selected_setting) {
258- case BACK_S:
259- setting = false;
310+
311+ switch (arrow) {
312+ case UP:
313+ switch (current_setting) {
314+ case SETTINGS_MENU:
315+ selected_setting = (selected_setting + (SETTINGS_MENU - 1)) % SETTINGS_MENU;
316+ break;
317+ case SPEED_S:
318+ if (game_speed > V_FAST)
319+ game_speed--;
320+ break;
321+ default:
322+ break;
323+ }
324+ change_menu = true;
325+ break;
326+ case DOWN:
327+ switch (current_setting) {
328+ case SETTINGS_MENU:
329+ selected_setting = (selected_setting + 1) % SETTINGS_MENU;
330+ break;
331+ case SPEED_S:
332+ if (game_speed < V_SLOW)
333+ game_speed++;
334+ break;
335+ default:
336+ break;
337+ }
260338 change_menu = true;
261339 break;
262340 default:
263341 break;
342+ }
343+
344+ if (pressed_enter) {
345+ if (current_setting == SETTINGS_MENU) {
346+ switch (selected_setting) {
347+ case BACK_S:
348+ setting = false;
349+ break;
350+ case SPEED_S:
351+ current_setting = SPEED_S;
352+ break;
353+ default:
354+ break;
355+ }
356+ } else {
357+ current_setting = SETTINGS_MENU;
264358 }
359+ change_menu = true;
360+ }
265361 pressed_enter = false;
266362 }
267363
268- }
269-
270364 nanosleep(&mili100, NULL);
271365 }
272366
@@ -307,6 +401,8 @@ int main(int argc, char *args[]) {
307401
308402 bool run = true;
309403
404+ struct timespec milispeed = {0, 1000000 * (SPEED + game_speed * 25)};
405+
310406 /* start of the actual game loop */
311407
312408 while (run) {
@@ -391,7 +487,7 @@ int main(int argc, char *args[]) {
391487 draw_board(board, current_size, false, false);
392488 }
393489 }
394- nanosleep(&mili100, NULL);
490+ nanosleep(&milispeed, NULL);
395491 }
396492
397493 end_of_program:
@@ -527,7 +623,7 @@ bool start_animation() {
527623 struct winsize current_size;
528624 ioctl(STDOUT_FILENO, TIOCGWINSZ, &current_size);
529625
530- struct timespec mili10 = {0, 100000 * SPEED};
626+ struct timespec mili10 = {0, 1000000 * 10};
531627
532628 char *snake[3][7] = {
533629 {