From 60076577cc7913fcc36b69942b530b1f9473fd78 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Tue, 23 Jan 2024 13:47:17 +0100 Subject: [PATCH] moving snake --- src/main/c/Snake/snake_start.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/main/c/Snake/snake_start.c b/src/main/c/Snake/snake_start.c index e4c826b..23418cf 100644 --- a/src/main/c/Snake/snake_start.c +++ b/src/main/c/Snake/snake_start.c @@ -11,7 +11,7 @@ void main_menu(); void game(); Snake initialize_snake(); -void get_next_move(double limit, Snake *snake); +void get_next_move(double limit, Snake *snake, bool *running); void move_snake(Snake *snake); void draw(Snake *snake); int part_of_snake(Snake *snake, unsigned char tile); @@ -56,25 +56,19 @@ void game(){ bool running = true; clock_t t = clock(); - //while (running){ - for(int i = 0; i < 100; i++){ + while (running){ system("clear"); draw(&snake); t = clock() - t; - get_next_move(TIME_TURN - (double)t / CLOCKS_PER_SEC, &snake); + get_next_move(TIME_TURN - (double)t / CLOCKS_PER_SEC, &snake, &running); t = clock(); move_snake(&snake); - - running = false; - } - getchar(); - getchar(); } Snake initialize_snake(){ - Snake snake = {{0, 1}, 3}; + Snake snake = {{1, 0}, 3}; for(int i = 0; i < AREA; i++){ snake.segments[i] = 0; } @@ -82,12 +76,10 @@ Snake initialize_snake(){ snake.segments[1] = 7 + 16 * 8; snake.segments[2] = 6 + 16 * 8; - snake.direction[0] = 1; - snake.direction[1] = 0; return snake; } -void get_next_move(double limit, Snake *snake){ +void get_next_move(double limit, Snake *snake, bool *running){ char c; c = get_character(limit); @@ -113,6 +105,8 @@ void get_next_move(double limit, Snake *snake){ snake->direction[0] = 1; snake->direction[1] = 0; break; + case 'q': + *running = false; default: break; @@ -120,7 +114,10 @@ void get_next_move(double limit, Snake *snake){ } 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; } void draw(Snake *snake){