Fix disallowed_direction logic and improved input responsiveness

This commit is contained in:
Zac Wilson 2025-07-06 16:15:30 +01:00
parent 5ac79ce0ce
commit e7157f58c1

View file

@ -83,6 +83,7 @@ fn main() -> Result<(), anyhow::Error> {
let mut apple = random_tile();
loop {
for _ in 0..10 {
let pressed_keys = keyboard.scan_pressed_keys()?;
dbg!(&pressed_keys);
for key in pressed_keys {
@ -95,10 +96,19 @@ fn main() -> Result<(), anyhow::Error> {
};
match (snake_tiles.get(0), snake_tiles.get(1)) {
(Some(&first), Some(&second)) => {
let disallowed_direction = (
let mut disallowed_direction = (
(second.0 - first.0).rem(GRID_WIDTH),
(second.1 - first.1).rem(GRID_HEIGHT),
);
if disallowed_direction.0 == GRID_WIDTH - 1 {
disallowed_direction.0 = -1;
} else if disallowed_direction.0 == 1 - GRID_WIDTH {
disallowed_direction.0 = 1;
} else if disallowed_direction.1 == GRID_HEIGHT - 1 {
disallowed_direction.1 = -1;
} else if disallowed_direction.1 == 1 - GRID_HEIGHT {
disallowed_direction.1 = 1;
}
dbg!(disallowed_direction);
if disallowed_direction != new_direction {
direction = new_direction;
@ -110,7 +120,8 @@ fn main() -> Result<(), anyhow::Error> {
}
}
sleep(Duration::from_millis(200));
sleep(Duration::from_millis(20));
}
let front_file = *snake_tiles.get(0).unwrap();