End game if the snake collides with itself

This commit is contained in:
Yash Karandikar 2022-04-30 17:15:22 -05:00
parent 28c2273474
commit 890f9735a3
Signed by: karx
GPG key ID: A794DA2529474BA5

View file

@ -59,7 +59,11 @@ fn main() {
for key in keys {
use Direction::*;
match key {
Scancode::Q => game.terminate(),
Scancode::Q | Scancode::Escape => {
println!("Game over!");
println!("Your score was: {}", score);
game.terminate();
}
Scancode::W | Scancode::Up => set_if_not_opp!(dir, Up, Down),
Scancode::A | Scancode::Left => set_if_not_opp!(dir, Left, Right),
Scancode::S | Scancode::Down => set_if_not_opp!(dir, Down, Up),
@ -96,6 +100,15 @@ fn main() {
}
};
{
let hitted = cat_box::physics::check_for_collision_with_collection(&snake[0], &snake);
if hitted.len() > 1 {
println!("Game over!");
println!("Your score was: {}", score);
game.terminate();
}
}
if !cat_box::physics::check_for_collision_with_collection(&apple, &snake).is_empty() {
let x = thread_rng().gen_range(0..=27) * 37;
let y = thread_rng().gen_range(0..=27) * 37;