1use crossterm::{
2 event::{self, KeyCode},
3 style::Color,
4 terminal, Result,
5};
6use winterm::Window;
7
8struct Player {
9 x: u16,
10 y: u16,
11}
12
13fn main() -> Result<()> {
14 println!("Use arrows or WASD to move.");
15 println!("[Press any key to continue]");
16 terminal::enable_raw_mode()?;
17 event::read()?;
18
19 let mut window = Window::new(25, 25)?;
20 let background_color = Color::Rgb {
21 r: 100,
22 g: 100,
23 b: 100,
24 };
25 for y in 0..window.height() {
26 for x in 0..window.width() {
27 window.set_pixel(y, x, background_color);
28 }
29 }
30 let mut player = Player { x: 12, y: 12 };
31 loop {
32 window.poll_events()?;
33 if window.get_key(KeyCode::Esc) {
34 break;
35 }
36 window.set_pixel(player.y, player.x, background_color);
37 if player.y > 0 && (window.get_key(KeyCode::Up) || window.get_key(KeyCode::Char('w'))) {
38 player.y -= 1;
39 }
40 if player.y < window.height() - 1
41 && (window.get_key(KeyCode::Down) || window.get_key(KeyCode::Char('s')))
42 {
43 player.y += 1;
44 }
45 if player.x > 0 && (window.get_key(KeyCode::Left) || window.get_key(KeyCode::Char('a'))) {
46 player.x -= 1;
47 }
48 if player.x < window.width() - 1
49 && (window.get_key(KeyCode::Right) || window.get_key(KeyCode::Char('d')))
50 {
51 player.x += 1;
52 }
53 window.set_pixel(player.y, player.x, Color::Red);
54 window.redraw()?;
55 }
56 Ok(())
57}