Browse Source

Add movement

master
Noëlle Anthony 4 years ago
parent
commit
4c73e872a9
1 changed files with 15 additions and 1 deletions
  1. 15
    1
      src/main.rs

+ 15
- 1
src/main.rs View File

@@ -10,6 +10,7 @@ use std::time::Duration;
struct Player {
position: Point,
sprite: Rect,
speed: i32,
}

fn render(canvas: &mut WindowCanvas,
@@ -45,9 +46,10 @@ fn main() -> Result<(), String> {
let texture_creator = canvas.texture_creator();
let texture = texture_creator.load_texture("assets/bardo.png")?;

let player = Player {
let mut player = Player {
position: Point::new(0,0),
sprite: Rect::new(0, 0, 26, 36),
speed: 5,
};

let mut event_pump = sdl_context.event_pump()?;
@@ -59,6 +61,18 @@ fn main() -> Result<(), String> {
Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
break 'running;
},
Event::KeyDown { keycode: Some(Keycode::Left), .. } => {
player.position = player.position.offset(-player.speed, 0);
},
Event::KeyDown { keycode: Some(Keycode::Right), .. } => {
player.position = player.position.offset(player.speed, 0);
},
Event::KeyDown { keycode: Some(Keycode::Up), .. } => {
player.position = player.position.offset(0, -player.speed);
},
Event::KeyDown { keycode: Some(Keycode::Down), .. } => {
player.position = player.position.offset(0, player.speed);
},
_ => {}
}
}

Loading…
Cancel
Save