extern crate gfx4games; use gfx4games::events::Key; use gfx4games::events::MainLoop; use gfx4games::events::run; use gfx4games::graphics; use gfx4games::graphics::Point2; use gfx4games::graphics::Vector2; use gfx4games::graphics::context::Context; use gfx4games::graphics::text::Text; use gfx4games::rect::Rect; struct MainState { rect: Rect, rect_vel: Vector2, } impl MainState { pub fn new() -> MainState { MainState { rect: Rect::new(-30.0, 0.0, 5.5, 5.5), rect_vel: Vector2::new(0.0, 0.0), } } } impl MainLoop for MainState { fn draw(&mut self, ctx: &mut Context) { self.rect.draw(ctx); let mut text = Text::new( "Use arrow keys to move".to_string(), Point2::new(10.0, 30.0), ); text.draw(ctx); } fn update(&mut self, _ctx: &mut Context, _dt: f32) { self.rect.set_color(graphics::INDIGO); self.rect.dx += self.rect_vel.x; self.rect.dy += self.rect_vel.y; } fn button_down(&mut self, key: Key) { match key { Key::Right => { self.rect_vel.x = 1.0; } Key::Left => { self.rect_vel.x = -1.0; } Key::Up => { self.rect_vel.y = 1.0; } Key::Down => { self.rect_vel.y = -1.0; } _ => {} } } fn button_release(&mut self, key: Key) { match key { Key::Right => { self.rect_vel.x = 0.0; } Key::Left => { self.rect_vel.x = 0.0; } Key::Up => { self.rect_vel.y = 0.0; } Key::Down => { self.rect_vel.y = 0.0; } _ => {} } } } fn main() { let mut state = MainState::new(); let mut ctx = Context::new("Input Example".to_string(), 512, 512); println!("Use arrow keys to move rectangle"); run(&mut ctx, &mut state, 120); }