From ef2de556e3981fe99afd6efe5967142620ae6794 Mon Sep 17 00:00:00 2001 From: xamidev <121681048+xamidev@users.noreply.github.com> Date: Fri, 13 Sep 2024 14:08:00 +0200 Subject: [PATCH] Upgrade: snake --- docs/USERS.md | 14 +++++++++++ src/programs/snake.c | 59 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 63 insertions(+), 10 deletions(-) diff --git a/docs/USERS.md b/docs/USERS.md index 5082855..a0fe44e 100644 --- a/docs/USERS.md +++ b/docs/USERS.md @@ -129,3 +129,17 @@ Lists all files present in `initrd.tar`. #### `cat ` Prints file content to terminal. Filename must be specified the same way as it is outputted when using `ls`. + +### Games + +#### `naval` + +Starts a simplified naval battle game with 5 ships, one position each. + +#### `snake ` + +Starts a simplified and buggy snake game. You can choose the speed by setting the `ticks` argument, or let it default to a normal speed. + +Controls: +- `q` to quit +- `wasd` to move diff --git a/src/programs/snake.c b/src/programs/snake.c index fcdd584..45cfc0e 100644 --- a/src/programs/snake.c +++ b/src/programs/snake.c @@ -10,13 +10,13 @@ #include "../libc/string.h" #include "../libc/crypto.h" -#define WIDTH 25 -#define HEIGHT 25 -#define PIXEL_SIZE 20 - +#define WIDTH 25 +#define HEIGHT 25 +#define PIXEL_SIZE 20 +#define MAX_SNAKE_LENGTH 256 // to add: -// score // sound +// optimization (shit) typedef struct { @@ -26,7 +26,7 @@ typedef struct typedef struct { - SnakeSegment segments[100]; + SnakeSegment segments[MAX_SNAKE_LENGTH]; int length; int dx; int dy; @@ -40,6 +40,8 @@ typedef struct Snake snake; Food food; +int score = 0; +bool onSnake; int is_snake(int x, int y); @@ -57,7 +59,6 @@ void init_game() void draw_board() { - erase_cursor(); for (int y=0; y<=HEIGHT; y++) { for (int x=0; x<=WIDTH; x++) @@ -75,6 +76,15 @@ void draw_board() } } } + + move_cursor(WIDTH+42, 2); + colorputs("Snake Game!", black, green); + move_cursor(WIDTH+42, 4); + colorprintf(yellow, black, "Score: %d", score); + move_cursor(WIDTH+42, 5); + puts("Use WASD keys to move"); + move_cursor(WIDTH+42, 6); + puts("Press Q to quit"); } int is_snake(int x, int y) @@ -107,9 +117,38 @@ void move_snake() if (snake.segments[0].x == food.x && snake.segments[0].y == food.y) { snake.length++; - food.x = rand() % (WIDTH-1); - food.y = rand() % (HEIGHT-1); + score++; + + do + { + onSnake = false; + food.x = rand() % (WIDTH-1) + 1; + food.y = rand() % (HEIGHT-1) + 1; + + for (int i=0; i