| @@ -6,17 +6,30 @@ use sdl2::image::{self, LoadTexture, InitFlag}; | |||
| use sdl2::rect::{Point, Rect}; | |||
| use std::time::Duration; | |||
| const PLAYER_MOVEMENT_SPEED: i32 = 10; | |||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | |||
| enum Direction { | |||
| Up, | |||
| Down, | |||
| Left, | |||
| Right, | |||
| } | |||
| #[derive(Debug)] | |||
| struct Player { | |||
| position: Point, | |||
| sprite: Rect, | |||
| speed: i32, | |||
| direction: Direction, | |||
| } | |||
| fn render(canvas: &mut WindowCanvas, | |||
| color: Color, | |||
| texture: &Texture, | |||
| player: &Player) -> Result<(), String> { | |||
| fn render( | |||
| canvas: &mut WindowCanvas, | |||
| color: Color, | |||
| texture: &Texture, | |||
| player: &Player | |||
| ) -> Result<(), String> { | |||
| canvas.set_draw_color(color); | |||
| canvas.clear(); | |||
| @@ -30,6 +43,24 @@ fn render(canvas: &mut WindowCanvas, | |||
| Ok(()) | |||
| } | |||
| fn update_player(player: &mut Player) { | |||
| use self::Direction::*; | |||
| match player.direction { | |||
| Left => { | |||
| player.position = player.position.offset(-player.speed, 0); | |||
| }, | |||
| Right => { | |||
| player.position = player.position.offset(player.speed, 0); | |||
| }, | |||
| Up => { | |||
| player.position = player.position.offset(0, -player.speed); | |||
| }, | |||
| Down => { | |||
| player.position = player.position.offset(0, player.speed); | |||
| }, | |||
| } | |||
| } | |||
| fn main() -> Result<(), String> { | |||
| let sdl_context = sdl2::init()?; | |||
| let video_subsystem = sdl_context.video()?; | |||
| @@ -49,7 +80,8 @@ fn main() -> Result<(), String> { | |||
| let mut player = Player { | |||
| position: Point::new(0,0), | |||
| sprite: Rect::new(0, 0, 26, 36), | |||
| speed: 5, | |||
| speed: 0, | |||
| direction: Direction::Right, | |||
| }; | |||
| let mut event_pump = sdl_context.event_pump()?; | |||
| @@ -62,22 +94,33 @@ fn main() -> Result<(), String> { | |||
| break 'running; | |||
| }, | |||
| Event::KeyDown { keycode: Some(Keycode::Left), .. } => { | |||
| player.position = player.position.offset(-player.speed, 0); | |||
| player.speed = PLAYER_MOVEMENT_SPEED; | |||
| player.direction = Direction::Left; | |||
| }, | |||
| Event::KeyDown { keycode: Some(Keycode::Right), .. } => { | |||
| player.position = player.position.offset(player.speed, 0); | |||
| player.speed = PLAYER_MOVEMENT_SPEED; | |||
| player.direction = Direction::Right; | |||
| }, | |||
| Event::KeyDown { keycode: Some(Keycode::Up), .. } => { | |||
| player.position = player.position.offset(0, -player.speed); | |||
| player.speed = PLAYER_MOVEMENT_SPEED; | |||
| player.direction = Direction::Up; | |||
| }, | |||
| Event::KeyDown { keycode: Some(Keycode::Down), .. } => { | |||
| player.position = player.position.offset(0, player.speed); | |||
| player.speed = PLAYER_MOVEMENT_SPEED; | |||
| player.direction = Direction::Down; | |||
| }, | |||
| Event::KeyUp { keycode: Some(Keycode::Left), repeat: false, .. } | | |||
| Event::KeyUp { keycode: Some(Keycode::Right), repeat: false, .. } | | |||
| Event::KeyUp { keycode: Some(Keycode::Up), repeat: false, .. } | | |||
| Event::KeyUp { keycode: Some(Keycode::Down), repeat: false, .. } => { | |||
| player.speed = 0; | |||
| }, | |||
| _ => {} | |||
| } | |||
| } | |||
| i = (i + 1) % 255; | |||
| update_player(&mut player); | |||
| render(&mut canvas, Color::RGB(i, 64, 255-i), &texture, &player)?; | |||