From 9a3247bf684857c1cf33b4990a87e14897f62bd8 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Thu, 25 Jan 2024 10:22:33 +0100 Subject: [PATCH] refactoring: movement of snake --- src/main/c/Snake/snake_start.c | 16 ++++++---------- src/main/c/Snake/snake_start.h | 2 +- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/main/c/Snake/snake_start.c b/src/main/c/Snake/snake_start.c index 068eca7..70bc96e 100644 --- a/src/main/c/Snake/snake_start.c +++ b/src/main/c/Snake/snake_start.c @@ -77,7 +77,7 @@ void game(){ } Snake initialize_snake(){ - Snake snake = {{1, 0}, 3}; + Snake snake = {1, 3}; for(int i = 0; i < AREA; i++){ snake.segments[i] = 0; } @@ -96,23 +96,19 @@ void get_next_move(double limit, Snake *snake, bool *running){ switch (c){ case 'w': case 'A': - snake->direction[0] = 0; - snake->direction[1] = -1; + snake->direction = -16; break; case 'a': case 'D': - snake->direction[0] = -1; - snake->direction[1] = 0; + snake->direction = -1; break; case 's': case 'B': - snake->direction[0] = 0; - snake->direction[1] = 1; + snake->direction = 16; break; case 'd': case 'C': - snake->direction[0] = 1; - snake->direction[1] = 0; + snake->direction = 1; break; case 'q': *running = false; @@ -126,7 +122,7 @@ void move_snake(Snake *snake){ for(int i = snake->length - 1; i > 0; i--){ snake->segments[i] = snake->segments[i - 1]; } - snake->segments[0] += snake->direction[0] + snake->direction[1] * 16; + snake->segments[0] += snake->direction; } void draw(Snake *snake, unsigned char fruit){ diff --git a/src/main/c/Snake/snake_start.h b/src/main/c/Snake/snake_start.h index 87a4e1c..9240626 100644 --- a/src/main/c/Snake/snake_start.h +++ b/src/main/c/Snake/snake_start.h @@ -6,7 +6,7 @@ #define AREA HEIGHT * WIDTH typedef struct{ - signed char direction[2]; + signed char direction; char length; unsigned char segments[AREA]; }Snake;