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

struct Player { struct Player {
position: Point, position: Point,
sprite: Rect, sprite: Rect,
speed: i32,
} }


fn render(canvas: &mut WindowCanvas, fn render(canvas: &mut WindowCanvas,
let texture_creator = canvas.texture_creator(); let texture_creator = canvas.texture_creator();
let texture = texture_creator.load_texture("assets/bardo.png")?; let texture = texture_creator.load_texture("assets/bardo.png")?;


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


let mut event_pump = sdl_context.event_pump()?; let mut event_pump = sdl_context.event_pump()?;
Event::KeyDown { keycode: Some(Keycode::Escape), .. } => { Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
break 'running; 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