jag

snake_c

public

Small snake game in C

Star0
Watch0

Commit 3475bc917dda

tarball

Jaglekon <58274093+Jaglekon@users.noreply.github.com>2026-03-31 19:26 UTC

2 files changed+72-8
remember
Modified file: snake.c+69 additions−8 deletions
@@ -28,6 +28,64 @@ int main(int argc, char *args[]) {
2828 return 1;
2929 }
3030
31+
32+ /* game settings */
33+
34+ speed game_speed = AVERAGE;
35+ selected game_size = {WIDTH, HEIGHT};
36+
37+
38+ char *env;
39+
40+ if ((env = getenv("XDG_DATA_HOME")) == NULL) {
41+ printf("No env var XDG_DATA_HOME\n");
42+ return 1;
43+ }
44+
45+ char file_path[PATH_LEN];
46+ snprintf(file_path, sizeof(file_path), "%s/%s", env, SETTING_FILE);
47+
48+ {
49+ FILE* setting_file = fopen(file_path, "r");
50+
51+ if (setting_file != NULL) {
52+
53+ char setting_input[STRING_LEN];
54+ char *endptr;
55+
56+ fgets(setting_input, sizeof(setting_input), setting_file);
57+
58+ int32_t number = strtol(setting_input, &endptr, 10);
59+
60+ if (setting_input != endptr) {
61+ if (number >= 0 && number < SPEED_L)
62+ game_speed = number;
63+ }
64+
65+
66+ fgets(setting_input, sizeof(setting_input), setting_file);
67+
68+ number = strtol(setting_input, &endptr, 10);
69+
70+ if (setting_input != endptr) {
71+ if (number >= MIN_SIZE && number <= MAX_SIZE)
72+ game_size.x = number;
73+ }
74+
75+
76+ fgets(setting_input, sizeof(setting_input), setting_file);
77+
78+ number = strtol(setting_input, &endptr, 10);
79+
80+ if (setting_input != endptr) {
81+ if (number >= MIN_SIZE && number <= MAX_SIZE)
82+ game_size.y = number;
83+ }
84+
85+ fclose(setting_file);
86+ }
87+ }
88+
3189 /* Setting up terminal with termios */
3290
3391 struct termios old, new;
@@ -59,10 +117,6 @@ int main(int argc, char *args[]) {
59117 if (!start_animation())
60118 goto end_of_program;
61119
62- /* game settings */
63-
64- speed game_speed = AVERAGE;
65- selected game_size = {WIDTH, HEIGHT};
66120
67121 /* Menu */
68122
@@ -220,7 +274,6 @@ int main(int argc, char *args[]) {
220274 nanosleep(&mili100, NULL);
221275 }
222276
223-
224277 /* initialisation of the game */
225278
226279 snake my_snake;
@@ -332,7 +385,10 @@ int main(int argc, char *args[]) {
332385
333386 if (board[head_y][head_x] == FRUIT) {
334387 my_snake.length++;
335- score++;
388+ score += 1 +
389+ (((MAX_SIZE >> 1) / (game_size.y) +
390+ (MAX_SIZE >> 1) / (game_size.x) +
391+ (my_snake.length - STARTING_LENGTH)) / (game_speed + 1));
336392 snake_part *tmp = realloc(my_snake.position, (my_snake.length + 1) * sizeof(snake_part));
337393 if (tmp == NULL)
338394 goto end_free;
@@ -375,8 +431,13 @@ end_of_program:
375431 tcsetattr(STDIN_FILENO, TCSANOW, &old);
376432 printf("\033[?25h\n\n");
377433
378-
379434 printf("\033[32m");
380- printf("\n\nYour score: %5d", score);
435+
436+ {
437+ FILE* setting_file = fopen(file_path, "w");
438+
439+ fprintf(setting_file, "%d\n%d\n%d", game_speed, game_size.x, game_size.y);
440+ }
441+
381442 return 0;
382443 }
Modified file: snake.h+3 additions
@@ -31,12 +31,15 @@
3131 #define MAX_SIZE 100
3232
3333 #define STRING_LEN 20
34+#define PATH_LEN 200
3435
3536 #define SPEED 50 /* in ms */
3637
3738 #define MAX_LENGTH 1000
3839 #define STARTING_LENGTH 2
3940
41+#define SETTING_FILE "snake_c.settings"
42+
4043 typedef enum { NEW_GAME, SETTINGS, EXIT, MAIN_MENU } main_menu;
4144
4245 typedef enum {HEIGHT_S, WIDTH_S, SPEED_S, BACK_S, SETTINGS_MENU} settings;