From ca67976e794d7906d2d3721dd097f1b1e27a8d85 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Tue, 23 Jan 2024 14:49:21 +0100 Subject: [PATCH] Added fruit --- src/main/c/Snake/snake_start.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main/c/Snake/snake_start.c b/src/main/c/Snake/snake_start.c index ce54919..0584deb 100644 --- a/src/main/c/Snake/snake_start.c +++ b/src/main/c/Snake/snake_start.c @@ -13,9 +13,10 @@ void game(); Snake initialize_snake(); void get_next_move(double limit, Snake *snake, bool *running); void move_snake(Snake *snake); -void draw(Snake *snake); +void draw(Snake *snake, unsigned char fruit); int part_of_snake(Snake *snake, unsigned char tile); bool check_if_dead(Snake *snake); +unsigned char spawn_fruit(Snake *snake); void snake_start(){ @@ -56,10 +57,11 @@ void game(){ Snake snake = initialize_snake(); bool running = true; clock_t t = clock(); + unsigned char fruit = spawn_fruit(&snake); while (running){ system("clear"); - draw(&snake); + draw(&snake, fruit); if(check_if_dead(&snake)){ break; @@ -125,7 +127,8 @@ void move_snake(Snake *snake){ snake->segments[0] += snake->direction[0] + snake->direction[1] * 16; } -void draw(Snake *snake){ +void draw(Snake *snake, unsigned char fruit){ + printf("%d\n", fruit); printf("+"); for(int i = 0; i < WIDTH; i++){printf("-");} printf("+\n"); @@ -135,6 +138,7 @@ void draw(Snake *snake){ int index = part_of_snake(snake, i * 16 + j); if(index == 0){printf("0");} else if(index > 0){printf("O");} + else if(i * 16 + j == fruit){printf("X");} else{printf(" ");} } printf("|\n"); @@ -169,4 +173,14 @@ bool check_if_dead(Snake *snake){ } return false; +} + +unsigned char spawn_fruit(Snake *snake){ + srand(time(NULL)); + int r = 0; + while (part_of_snake(snake, r) != -1 || r % 16 == 0 || (r / 16) % 16 == 0){ + r = rand() % 256; + printf("%d\n", r); + } + return r; } \ No newline at end of file