main.rs 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // This file is generated automatically. Do not edit it directly.
  2. // See the Contributing section in README on how to make changes to it.
  3. use tcod::colors::*;
  4. use tcod::console::*;
  5. // actual size of the window
  6. const SCREEN_WIDTH: i32 = 80;
  7. const SCREEN_HEIGHT: i32 = 50;
  8. const LIMIT_FPS: i32 = 20; // 20 frames-per-second maximum
  9. struct Tcod {
  10. root: Root,
  11. }
  12. fn handle_keys(tcod: &mut Tcod, player_x: &mut i32, player_y: &mut i32) -> bool {
  13. use tcod::input::Key;
  14. use tcod::input::KeyCode::*;
  15. let key = tcod.root.wait_for_keypress(true);
  16. match key {
  17. Key {
  18. code: Enter,
  19. alt: true,
  20. ..
  21. } => {
  22. // Alt+Enter: toggle fullscreen
  23. let fullscreen = tcod.root.is_fullscreen();
  24. tcod.root.set_fullscreen(!fullscreen);
  25. }
  26. Key { code: Escape, .. } => return true, // exit game
  27. // movement keys
  28. Key { code: Up, .. } => *player_y -= 1,
  29. Key { code: Down, .. } => *player_y += 1,
  30. Key { code: Left, .. } => *player_x -= 1,
  31. Key { code: Right, .. } => *player_x += 1,
  32. _ => {}
  33. }
  34. false
  35. }
  36. fn main() {
  37. tcod::system::set_fps(LIMIT_FPS);
  38. let root = Root::initializer()
  39. .font("arial10x10.png", FontLayout::Tcod)
  40. .font_type(FontType::Greyscale)
  41. .size(SCREEN_WIDTH, SCREEN_HEIGHT)
  42. .title("Rust/libtcod tutorial")
  43. .init();
  44. let mut tcod = Tcod { root };
  45. let mut player_x = SCREEN_WIDTH / 2;
  46. let mut player_y = SCREEN_HEIGHT / 2;
  47. while !tcod.root.window_closed() {
  48. tcod.root.set_default_foreground(WHITE);
  49. tcod.root.clear();
  50. tcod.root
  51. .put_char(player_x, player_y, '@', BackgroundFlag::None);
  52. tcod.root.flush();
  53. // handle keys and exit game if needed
  54. let exit = handle_keys(&mut tcod, &mut player_x, &mut player_y);
  55. if exit {
  56. break;
  57. }
  58. }
  59. }