Browse Source

killing snake if collision

remotes/origin/David
David Moeller 11 months ago
parent
commit
d96d6594cd
  1. 24
      src/main/c/Snake/snake_start.c

24
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 move_snake(Snake *snake);
void draw(Snake *snake); void draw(Snake *snake);
int part_of_snake(Snake *snake, unsigned char tile); int part_of_snake(Snake *snake, unsigned char tile);
bool check_if_dead(Snake *snake);
void snake_start(){ void snake_start(){
@ -59,7 +60,11 @@ void game(){
while (running){ while (running){
system("clear"); system("clear");
draw(&snake); draw(&snake);
if(check_if_dead(&snake)){
break;
}
t = clock() - t; t = clock() - t;
get_next_move(TIME_TURN - (double)t / CLOCKS_PER_SEC, &snake, &running); get_next_move(TIME_TURN - (double)t / CLOCKS_PER_SEC, &snake, &running);
t = clock(); t = clock();
@ -147,4 +152,21 @@ int part_of_snake(Snake *snake, unsigned char tile){
} }
} }
return -1; 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;
} }
Loading…
Cancel
Save