Browse Source

Switch to player updates

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

+ 52
- 9
src/main.rs View File

use sdl2::rect::{Point, Rect}; use sdl2::rect::{Point, Rect};
use std::time::Duration; use std::time::Duration;


const PLAYER_MOVEMENT_SPEED: i32 = 10;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Direction {
Up,
Down,
Left,
Right,
}

#[derive(Debug)] #[derive(Debug)]
struct Player { struct Player {
position: Point, position: Point,
sprite: Rect, sprite: Rect,
speed: i32, 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.set_draw_color(color);
canvas.clear(); canvas.clear();


Ok(()) 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> { fn main() -> Result<(), String> {
let sdl_context = sdl2::init()?; let sdl_context = sdl2::init()?;
let video_subsystem = sdl_context.video()?; let video_subsystem = sdl_context.video()?;
let mut 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,
speed: 0,
direction: Direction::Right,
}; };


let mut event_pump = sdl_context.event_pump()?; let mut event_pump = sdl_context.event_pump()?;
break 'running; break 'running;
}, },
Event::KeyDown { keycode: Some(Keycode::Left), .. } => { 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), .. } => { 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), .. } => { 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), .. } => { 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; i = (i + 1) % 255;
update_player(&mut player);


render(&mut canvas, Color::RGB(i, 64, 255-i), &texture, &player)?; render(&mut canvas, Color::RGB(i, 64, 255-i), &texture, &player)?;



Loading…
Cancel
Save