diff --git a/src/direction.rs b/src/direction.rs index 1112510..cf6b8d3 100644 --- a/src/direction.rs +++ b/src/direction.rs @@ -1,3 +1,5 @@ +use sdl2::keyboard::Keycode; + #[derive(Debug, Copy, Clone, PartialEq)] pub enum Direction { Up, @@ -24,4 +26,18 @@ impl Direction { Direction::Up => (0, -1), } } + + pub fn from_keycode(keycode: Keycode) -> Option { + match keycode { + Keycode::D => Some(Direction::Right), + Keycode::Right => Some(Direction::Right), + Keycode::A => Some(Direction::Left), + Keycode::Left => Some(Direction::Left), + Keycode::W => Some(Direction::Up), + Keycode::Up => Some(Direction::Up), + Keycode::S => Some(Direction::Down), + Keycode::Down => Some(Direction::Down), + _ => None, + } + } } \ No newline at end of file