From d96d6594cdf5029867c8754f8dfe3b301912700f Mon Sep 17 00:00:00 2001 From: David Moeller Date: Tue, 23 Jan 2024 14:01:43 +0100 Subject: [PATCH] killing snake if collision --- src/main/c/Snake/snake_start.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/c/Snake/snake_start.c b/src/main/c/Snake/snake_start.c index 23418cf..ce54919 100644 --- a/src/main/c/Snake/snake_start.c +++ b/src/main/c/Snake/snake_start.c @@ -15,6 +15,7 @@ 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); +bool check_if_dead(Snake *snake); void snake_start(){ @@ -59,7 +60,11 @@ void game(){ while (running){ system("clear"); draw(&snake); - + + if(check_if_dead(&snake)){ + break; + } + t = clock() - t; get_next_move(TIME_TURN - (double)t / CLOCKS_PER_SEC, &snake, &running); t = clock(); @@ -147,4 +152,21 @@ int part_of_snake(Snake *snake, unsigned char tile){ } } return -1; +} + +bool check_if_dead(Snake *snake){ + //Self + for(int i = 0; i < snake->length; i++){ + int part = part_of_snake(snake, snake->segments[i]); + if(part != -1 && part != i){ + return true; + } + } + + //Wall + if(snake->segments[0] % 16 == 0 || (snake->segments[0] / 16) % 16 == 0){ + return true; + } + + return false; } \ No newline at end of file