Advertisement
NLinker

ggez example with nonblocking input

Jul 10th, 2019
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.62 KB | None | 0 0
  1. use ggez::event::{EventHandler, KeyCode, KeyMods};
  2. use ggez::{graphics, nalgebra as na, timer};
  3. use ggez::input::keyboard;
  4. use ggez::{Context, GameResult};
  5.  
  6. struct MainState {
  7.     position_x: f32,
  8. }
  9.  
  10. impl EventHandler for MainState {
  11.     fn update(&mut self, ctx: &mut Context) -> GameResult {
  12.         // Increase or decrease `position_x` by 0.5, or by 5.0 if Shift is held.
  13.         if keyboard::is_key_pressed(ctx, KeyCode::Right) {
  14.             if keyboard::is_mod_active(ctx, KeyMods::SHIFT) {
  15.                 self.position_x += 4.5;
  16.             }
  17.             self.position_x += 0.5;
  18.         } else if keyboard::is_key_pressed(ctx, KeyCode::Left) {
  19.             if keyboard::is_mod_active(ctx, KeyMods::SHIFT) {
  20.                 self.position_x -= 4.5;
  21.             }
  22.             self.position_x -= 0.5;
  23.         }
  24.         Ok(())
  25.     }
  26.  
  27.     fn draw(&mut self, ctx: &mut Context) -> GameResult {
  28.         graphics::clear(ctx, [0.1, 0.2, 0.3, 1.0].into());
  29.         // Create a circle at `position_x` and draw
  30.         let circle = graphics::Mesh::new_circle(
  31.             ctx,
  32.             graphics::DrawMode::fill(),
  33.             na::Point2::new(self.position_x, 380.0),
  34.             100.0,
  35.             2.0,
  36.             graphics::WHITE,
  37.         )?;
  38.         graphics::draw(ctx, &circle, graphics::DrawParam::default())?;
  39.         graphics::present(ctx)?;
  40.         timer::yield_now();
  41.         Ok(())
  42.     }
  43.  
  44.     fn key_down_event(&mut self, ctx: &mut Context, key: KeyCode, mods: KeyMods, _: bool) {
  45.         match key {
  46.             // Quit if Shift+Ctrl+Q is pressed.
  47.             KeyCode::Q => {
  48.                 if mods.contains(KeyMods::SHIFT & KeyMods::CTRL) {
  49.                     println!("Terminating!");
  50.                     ggez::quit(ctx);
  51.                 } else if mods.contains(KeyMods::SHIFT) || mods.contains(KeyMods::CTRL) {
  52.                     println!("You need to hold both Shift and Control to quit.");
  53.                 } else {
  54.                     println!("Now you're not even trying!");
  55.                 }
  56.             }
  57.             _ => (),
  58.         }
  59.     }
  60. }
  61.  
  62. fn main() {
  63.     // create an instance of game state
  64.     let state = &mut MainState { position_x: 10.0 };
  65.  
  66.     // create a context to access hardware (also creates event loop)
  67.     let c = ggez::conf::Conf::new();
  68.     let (ref mut ctx, ref mut event_loop) = ggez::ContextBuilder::new("hello_ggez", "awesome_person")
  69.         .conf(c)
  70.         .build()
  71.         .unwrap();
  72.  
  73.     // start game loop
  74.     match ggez::event::run(ctx, event_loop, state) {
  75.         Ok(_) => println!("Goodbye"),
  76.         Err(e) => println!("It all went wrong! {}", e),
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement