Compare commits

...

2 commits

Author SHA1 Message Date
Yash Karandikar 890f9735a3
End game if the snake collides with itself 2022-04-30 17:15:22 -05:00
Yash Karandikar 28c2273474
Rework border loop 2022-04-30 17:02:08 -05:00

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;
@ -127,18 +140,21 @@ fn main() {
y /= 37;
if dir == Direction::Left || dir == Direction::Right {
if x == 0 {
snake[0].translate((27 * 37, 0));
} else if x == 27 {
snake[0].translate((-27 * 37, 0));
if x <= 0 {
let diff = (27 * 37) - (x * 37);
snake[0].translate((diff, 0));
} else if x >= 27 {
let diff = 0 - (x * 37);
snake[0].translate((diff, 0));
}
}
if dir == Direction::Up || dir == Direction::Down {
if y == 0 {
snake[0].translate((0, -27 * 37));
} else if y == 27 {
snake[0].translate((0, 27 * 37));
if y <= 0 {
let diff = (y * 37) - (27 * 37);
snake[0].translate((0, diff));
} else if y >= 27 {
snake[0].translate((0, (y * 37)));
}
}
}