diff --git a/snake/src/main.rs b/snake/src/main.rs index 9e20f13..e7492ce 100644 --- a/snake/src/main.rs +++ b/snake/src/main.rs @@ -83,34 +83,45 @@ fn main() -> Result<(), anyhow::Error> { let mut apple = random_tile(); loop { - let pressed_keys = keyboard.scan_pressed_keys()?; - dbg!(&pressed_keys); - for key in pressed_keys { - let new_direction = match key { - cardputer_bsc::keyboard::KeyImprint::SemiColon => (0, -1), - cardputer_bsc::keyboard::KeyImprint::Comma => (-1, 0), - cardputer_bsc::keyboard::KeyImprint::Period => (0, 1), - cardputer_bsc::keyboard::KeyImprint::Slash => (1, 0), - _ => continue, - }; - match (snake_tiles.get(0), snake_tiles.get(1)) { - (Some(&first), Some(&second)) => { - let disallowed_direction = ( - (second.0 - first.0).rem(GRID_WIDTH), - (second.1 - first.1).rem(GRID_HEIGHT), - ); - dbg!(disallowed_direction); - if disallowed_direction != new_direction { + for _ in 0..10 { + let pressed_keys = keyboard.scan_pressed_keys()?; + dbg!(&pressed_keys); + for key in pressed_keys { + let new_direction = match key { + cardputer_bsc::keyboard::KeyImprint::SemiColon => (0, -1), + cardputer_bsc::keyboard::KeyImprint::Comma => (-1, 0), + cardputer_bsc::keyboard::KeyImprint::Period => (0, 1), + cardputer_bsc::keyboard::KeyImprint::Slash => (1, 0), + _ => continue, + }; + match (snake_tiles.get(0), snake_tiles.get(1)) { + (Some(&first), Some(&second)) => { + 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; + } + } + _ => { direction = new_direction; } } - _ => { - direction = new_direction; - } } - } - sleep(Duration::from_millis(200)); + sleep(Duration::from_millis(20)); + } let front_file = *snake_tiles.get(0).unwrap();