jag

snake_c

public

Small snake game in C

Star0
Watch0

Commit 893f512567ee

tarball

Jaglekon <58274093+Jaglekon@users.noreply.github.com>2026-08-28 22:12 UTC

10 files changed+132-82
added new lib
Modified file: CMakeLists.txt+12 additions−2 deletions
@@ -12,15 +12,25 @@ FetchContent_Declare(
1212 GIT_TAG v1.1.0
1313 )
1414
15-FetchContent_MakeAvailable(jag_parse)
15+FetchContent_Declare(
16+ jag_write
17+ GIT_REPOSITORY https://codeberg.org/Jaglekon/jag_write.git
18+ GIT_TAG v1.0.1
19+)
20+
21+FetchContent_MakeAvailable(jag_parse jag_write)
1622
1723 add_executable(snake
1824 source/main.c
1925 source/logic.c
2026 source/draw.c
27+ source/parameters.c
2128 )
2229
23-target_link_libraries(snake PRIVATE jag_parse)
30+target_link_libraries(snake PRIVATE
31+ jag_parse
32+ jag_write
33+)
2434
2535 target_compile_options(snake PRIVATE
2636 $<$<CONFIG:Debug>:-Wall -Wextra -Wpedantic>
Modified file: flake.lock+39 additions−4 deletions
@@ -18,6 +18,24 @@
1818 "url": "https://codeberg.org/Jaglekon/jag_parse.git"
1919 }
2020 },
21+ "jag-write": {
22+ "inputs": {
23+ "nixpkgs": "nixpkgs_2"
24+ },
25+ "locked": {
26+ "lastModified": 1787954748,
27+ "narHash": "sha256-cyTwqvKUAEtXfFSYUEfLH6JHRPJREzlC5wiTNMW9Oa8=",
28+ "ref": "refs/heads/main",
29+ "rev": "45cbda3bfc6147e9ed831b2d39a9134361ec7f0a",
30+ "revCount": 6,
31+ "type": "git",
32+ "url": "https://codeberg.org/Jaglekon/jag_write.git"
33+ },
34+ "original": {
35+ "type": "git",
36+ "url": "https://codeberg.org/Jaglekon/jag_write.git"
37+ }
38+ },
2139 "nixpkgs": {
2240 "locked": {
2341 "lastModified": 1787498568,
@@ -36,11 +54,27 @@
3654 },
3755 "nixpkgs_2": {
3856 "locked": {
39- "lastModified": 1787498568,
40- "narHash": "sha256-9i/VTdusq/+NM/tz+J1Re+ojkMB8MBf0QshnYfzHz30=",
57+ "lastModified": 1787736819,
58+ "narHash": "sha256-cV5xEJJK3BvhU8rEd4mC9UsmDi5qscv/kzGPhBRC5WA=",
4159 "owner": "NixOS",
4260 "repo": "nixpkgs",
43- "rev": "56c02bc00adcf003215cc4bd996d6efaf4cff188",
61+ "rev": "9fbb54b33e91ee4ca368e35a78e0613c720600b3",
62+ "type": "github"
63+ },
64+ "original": {
65+ "owner": "NixOS",
66+ "ref": "nixos-unstable",
67+ "repo": "nixpkgs",
68+ "type": "github"
69+ }
70+ },
71+ "nixpkgs_3": {
72+ "locked": {
73+ "lastModified": 1787736819,
74+ "narHash": "sha256-cV5xEJJK3BvhU8rEd4mC9UsmDi5qscv/kzGPhBRC5WA=",
75+ "owner": "NixOS",
76+ "repo": "nixpkgs",
77+ "rev": "9fbb54b33e91ee4ca368e35a78e0613c720600b3",
4478 "type": "github"
4579 },
4680 "original": {
@@ -53,7 +87,8 @@
5387 "root": {
5488 "inputs": {
5589 "jag-parse": "jag-parse",
56- "nixpkgs": "nixpkgs_2"
90+ "jag-write": "jag-write",
91+ "nixpkgs": "nixpkgs_3"
5792 }
5893 }
5994 },
Modified file: flake.nix+8 additions
@@ -5,16 +5,20 @@
55 nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
66
77 jag-parse.url = "git+https://codeberg.org/Jaglekon/jag_parse.git";
8+
9+ jag-write.url = "git+https://codeberg.org/Jaglekon/jag_write.git";
810 };
911
1012 outputs = {
1113 self,
1214 jag-parse,
15+ jag-write,
1316 nixpkgs,
1417 }: let
1518 system = "x86_64-linux";
1619 pkgs = import nixpkgs {inherit system;};
1720 jag-parse-lib = jag-parse.packages.${system}.default;
21+ jag-write-lib = jag-write.packages.${system}.default;
1822 in {
1923 packages.${system}.default = pkgs.stdenv.mkDerivation {
2024 pname = "snake";
@@ -27,9 +31,13 @@
2731 main.c \
2832 draw.c \
2933 logic.c \
34+ parameters.c \
3035 -I${jag-parse-lib}/include \
3136 -L${jag-parse-lib}/lib \
3237 -ljag_parse \
38+ -I${jag-write-lib}/include \
39+ -L${jag-write-lib}/lib \
40+ -ljag_write \
3341 -o snake
3442 '';
3543
Added file: result+1 additions
@@ -0,0 +1 @@
1+/nix/store/l5qbga6hbp0qvaakc6vvs11fdr34fi3h-snake-1.0.0
Modified file: source/draw.c+1 additions−1 deletions
@@ -75,7 +75,7 @@ bool start_animation() {
7575
7676 direction pressed_key = read_direction(string, true);
7777
78- if (pressed_key == Q_KEY || pressed_key == ESCAPE)
78+ if (pressed_key == Q_BUTTON || pressed_key == ESCAPE)
7979 return false;
8080
8181 if (pressed_key == ENTER)
Added file: source/file_edit/file_edit.c+13 additions
@@ -0,0 +1,13 @@
1+#include "snake.h"
2+
3+
4+uint8_t make_folder(char *path) {
5+
6+ errno = 0;
7+ if (mkdir(path, 0755) && errno != EEXIST) {
8+ return 0;
9+ }
10+
11+ return 1;
12+}
13+
Modified file: source/logic.c+2 additions−12 deletions
@@ -184,10 +184,10 @@ direction read_direction(char *string, bool wasd_enabled) {
184184
185185 switch (toupper(string[0])) {
186186 case 'Q':
187- return Q_KEY;
187+ return Q_BUTTON;
188188 break;
189189 case 'P':
190- return P_KEY;
190+ return P_BUTTON;
191191 break;
192192 case '\n':
193193 return ENTER;
@@ -200,16 +200,6 @@ direction read_direction(char *string, bool wasd_enabled) {
200200 }
201201 }
202202
203-uint8_t make_folder(char *path) {
204-
205- errno = 0;
206- if (mkdir(path, 0755) && errno != EEXIST) {
207- return 0;
208- }
209-
210- return 1;
211-}
212-
213203 int32_t string_to_num(char* string, int32_t min, int32_t max, int32_t number) {
214204
215205 char *endptr;
Modified file: source/main.c+16 additions−62 deletions
@@ -1,37 +1,13 @@
11 #include "snake.h"
2-//#include <jag_parse.h>
32
43
54 int main(int argc, char *args[]) {
65
76 /* command argument handling */
7+ if (!parse_parameters(argc, args))
8+ return 0;
89
910 printf("\033[?1049h");
10-
11- if (argc > 1) {
12- if (argc > 2) {
13- printf("Too many parameters\n");
14- goto help_info;
15- }
16-
17- if (!strcmp(args[1], "-h") || !strcmp(args[1], "--help")) {
18-
19- printf("This is a small TUI snake game.\n\n");
20- printf("Controls: Arrow keys/WASD to control the snake.\n");
21- printf(" P/Enter to pause.\n");
22- printf(" Q to quit.\n");
23-
24- printf("\033[?1049l");
25- return 0;
26- }
27-
28- printf("Argument \"%s\" not recognized\n", args[1]);
29-
30- help_info:
31- printf("Try \"-h\" or \"--help\" for more information\n");
32- return 1;
33- }
34-
3511
3612 /* game settings */
3713
@@ -42,48 +18,27 @@ int main(int argc, char *args[]) {
4218
4319 /* Handling Files to store settings & highscores */
4420
45- char *env;
46- char *folder_path;
4721
48- if ((env = getenv("XDG_DATA_HOME")) == NULL) {
49- if ((env = getenv("HOME")) == NULL) {
50- printf("are you ok?\n");
51- return 1;
52- }
53-
54- uint8_t error = 0;
55-
56- char local[] = "/.local";
57- char share[] = "/share";
58-
59- folder_path = malloc(strlen(env) + strlen(local) +
60- strlen(share) + strlen(SETTINGS_FOLDER) + 2);
22+ /* Making sure all the folders on the path exist */
6123
62- sprintf(folder_path, "%s%s", env, local);
63- error += make_folder(folder_path);
64-
65- strcat(folder_path, share);
66- error += make_folder(folder_path);
24+ char folder_path[PATH_MAX];
25+
26+ {
27+ char xdg_path[PATH_MAX];
6728
68- if (error <= 1) {
69- printf("Failed to create Folder");
29+ if (!create_xdg_folder(xdg_path))
7030 return 1;
71- }
72- } else {
73- folder_path = malloc(strlen(env) + strlen(SETTINGS_FOLDER) + 2);
74- sprintf(folder_path, "%s", env);
75- }
7631
77- sprintf(folder_path + strlen(folder_path), "/%s", SETTINGS_FOLDER);
78- if (!make_folder(folder_path)) {
79- printf("Failed to create Folder");
80- return 1;
32+ if(!create_folder(SETTINGS_FOLDER ,xdg_path, folder_path))
33+ return 1;
8134 }
82-
35+
8336 char *settings_path = malloc(strlen(folder_path) + strlen(SETTINGS_FILE) + 2);
8437 sprintf(settings_path, "%s/%s", folder_path, SETTINGS_FILE);
8538
8639
40+ /* Reading the files */
41+
8742 {
8843 FILE* setting_file = fopen(settings_path, "r");
8944
@@ -107,7 +62,6 @@ int main(int argc, char *args[]) {
10762 char *score_path = malloc(strlen(folder_path) + strlen(SCORE_FILE) + 2);
10863 sprintf(score_path, "%s/%s", folder_path, SCORE_FILE);
10964
110- free(folder_path);
11165
11266 {
11367 FILE* score_file = fopen(score_path, "r");
@@ -215,7 +169,7 @@ int main(int argc, char *args[]) {
215169 pressed_key = read_direction(string, false);
216170 }
217171
218- if (pressed_key == Q_KEY || pressed_key == ESCAPE) {
172+ if (pressed_key == Q_BUTTON || pressed_key == ESCAPE) {
219173 if (setting != MAIN_MENU) {
220174 setting = MAIN_MENU;
221175 change_menu = true;
@@ -477,7 +431,7 @@ int main(int argc, char *args[]) {
477431 pressed_key = read_direction(string, true);
478432
479433 if (!too_small) {
480- if (pressed_key == P_KEY || pressed_key == ENTER) {
434+ if (pressed_key == P_BUTTON || pressed_key == ENTER) {
481435 paused = (paused == 1) ? 0 : 1;
482436 if (paused)
483437 draw_board(game_size, board, current_size, true, paused, score);
@@ -487,7 +441,7 @@ int main(int argc, char *args[]) {
487441 tmp_direction = pressed_key;
488442 }
489443
490- if (pressed_key == Q_KEY || pressed_key == ESCAPE)
444+ if (pressed_key == Q_BUTTON || pressed_key == ESCAPE)
491445 run = false;
492446 }
493447
Added file: source/parameters.c+30 additions
@@ -0,0 +1,30 @@
1+#include "snake.h"
2+
3+uint8_t parse_parameters(int argc, char *args[]) {
4+
5+ if (argc <= 1) {
6+ return 1;
7+ }
8+
9+
10+ if (argc > 2) {
11+ printf("Too many parameters\n");
12+ goto help_info;
13+ }
14+
15+ if (!strcmp(args[1], "-h") || !strcmp(args[1], "--help")) {
16+
17+ printf("This is a small TUI snake game.\n\n");
18+ printf("Controls: Arrow keys/WASD to control the snake.\n");
19+ printf(" P/Enter to pause.\n");
20+ printf(" Q to quit.\n");
21+
22+ return 0;
23+ }
24+
25+ printf("Argument \"%s\" not recognized\n", args[1]);
26+
27+help_info:
28+ printf("Try \"-h\" or \"--help\" for more information\n");
29+ return 0;
30+}
Modified file: source/snake.h+10 additions−1 deletions
@@ -22,6 +22,12 @@
2222 #include <ctype.h>
2323 #include <string.h>
2424
25+#include <limits.h>
26+
27+#include <jag_parse.h>
28+#include <jag_write.h>
29+
30+
2531 #define RIGHT_CODE 'C'
2632 #define LEFT_CODE 'D'
2733 #define UP_CODE 'A'
@@ -55,7 +61,7 @@ typedef enum {V_FAST, FAST, AVERAGE, SLOW, V_SLOW, SPEED_L} speed;
5561
5662 typedef enum {
5763 UP, LEFT, DOWN, RIGHT, DIRECTIONS,
58- ENTER, P_KEY, Q_KEY, ESCAPE, DEL
64+ ENTER, P_BUTTON, Q_BUTTON, ESCAPE, DEL
5965 } direction;
6066
6167 typedef enum {
@@ -111,4 +117,7 @@ uint8_t make_folder(char *path);
111117
112118 int32_t string_to_num(char* string, int32_t min, int32_t max, int32_t number);
113119
120+
121+uint8_t parse_parameters(int argc, char *args[]);
122+
114123 #endif