Permalink
Cannot retrieve contributors at this time
Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign up
Fetching contributors…
| extern crate ggez; | |
| use ggez::conf; | |
| use ggez::event; | |
| use ggez::graphics::{self, DrawMode, Point2}; | |
| use ggez::{Context, GameResult}; | |
| use std::cmp; | |
| use std::env; | |
| use std::f32; | |
| use std::path; | |
| use std::time::{Duration, Instant}; | |
| const SCREEN_SIZE: (u32, u32) = (800, 600); | |
| const JUMPSPEED: f32 = -0.5; | |
| const G: f32 = 0.002; | |
| struct MainState { | |
| bird: Bird, | |
| last_update: Instant, | |
| walls: Vec<Wall>, | |
| } | |
| impl MainState { | |
| fn new(ctx: &mut Context) -> Self { | |
| let mut walls: Vec<Wall> = Vec::new (); | |
| walls.push(Wall::new(Point2::new(50.0,50.0), Point2::new(50.0,50.0))); | |
| MainState { | |
| bird: Bird::new(ctx), | |
| last_update: Instant::now(), | |
| walls: walls, | |
| } | |
| } | |
| } | |
| impl event::EventHandler for MainState { | |
| fn update(&mut self, _ctx: &mut Context) -> GameResult<()> { | |
| self.bird.update(self.last_update.elapsed().subsec_millis()); | |
| self.last_update = Instant::now(); | |
| Ok(()) | |
| } | |
| fn draw(&mut self, ctx: &mut Context) -> GameResult<()> { | |
| graphics::clear(ctx); | |
| for i in 0..self.walls.len() { | |
| self.walls[i].draw(ctx)?; | |
| } | |
| self.bird.draw(ctx)?; | |
| Ok(()) | |
| } | |
| fn key_down_event( | |
| &mut self, | |
| ctx: &mut Context, | |
| keycode: ggez::event::Keycode, | |
| _keymod: ggez::event::Mod, | |
| _repeat: bool, | |
| ) { | |
| if keycode == ggez::event::Keycode::Space { | |
| self.bird.bump(); | |
| } | |
| if keycode == ggez::event::Keycode::Escape { | |
| ctx.quit().expect("Should never fail"); | |
| } | |
| } | |
| } | |
| #[derive(Clone)] | |
| struct Wall { | |
| pos: Point2, | |
| gap: Point2, | |
| } | |
| impl Wall { | |
| fn new(pos: Point2, gap: Point2) -> Self { | |
| Wall { pos, gap } | |
| } | |
| fn draw(&self, ctx: &mut Context) -> GameResult<()> { | |
| // dummy entry | |
| graphics::set_color(ctx, graphics::WHITE); | |
| graphics::rectangle(ctx,DrawMode::Fill, graphics::Rect::new(50.0,50.0,50.0,50.0)); | |
| graphics::present(ctx); | |
| Ok(()) | |
| } | |
| } | |
| struct Bird { | |
| image: graphics::Image, | |
| pos: Point2, | |
| angle: f32, | |
| speed: f32, | |
| } | |
| impl Bird { | |
| fn new(ctx: &mut Context) -> Self { | |
| let image = graphics::Image::new(ctx, "/bird.png").unwrap(); | |
| Bird { | |
| image: image, | |
| pos: Point2::new(50.0, 100.0), | |
| angle: 0.0, | |
| speed: 0.0, | |
| } | |
| } | |
| fn bump(&mut self) { | |
| self.speed = JUMPSPEED; | |
| } | |
| fn update(&mut self, elapsed: u32) { | |
| if self.pos.y > 600.0 { | |
| self.pos.y = 100.0; | |
| self.speed = 0.0; | |
| self.angle = 0.0; | |
| } | |
| self.pos.y += self.speed * (elapsed as f32); | |
| self.speed += 0.5 * G * (elapsed as f32); | |
| if self.speed > 1.0 { | |
| self.angle = 1.0; | |
| } else { | |
| self.angle = self.speed; | |
| } | |
| } | |
| fn draw(&self, ctx: &mut Context) -> GameResult<()> { | |
| graphics::draw_ex( | |
| ctx, | |
| &self.image, | |
| graphics::DrawParam { | |
| src: graphics::Rect::one(), | |
| dest: self.pos, | |
| rotation: self.angle, | |
| scale: Point2::new(0.03, 0.03), | |
| offset: Point2::new(0.5, 0.5), | |
| shear: Point2::new(0.0, 0.0), | |
| color: None, | |
| }, | |
| )?; | |
| graphics::present(ctx); | |
| Ok(()) | |
| } | |
| } | |
| pub fn main() { | |
| let mut ctx = &mut ggez::ContextBuilder::new("Rusty Flap", "Stefan Mesken") | |
| .window_setup(ggez::conf::WindowSetup::default().title("Rusty Flap")) | |
| .window_mode(ggez::conf::WindowMode::default().dimensions(SCREEN_SIZE.0, SCREEN_SIZE.1)) | |
| .build() | |
| .expect("Failed to build ggez context"); | |
| if let Ok(manifest_dir) = env::var("CARGO_MANIFEST_DIR") { | |
| let mut path = path::PathBuf::from(manifest_dir); | |
| path.push("resources"); | |
| ctx.filesystem.mount(&path, true); | |
| } | |
| let state = &mut MainState::new(&mut ctx); | |
| match event::run(ctx, state) { | |
| // If we encounter an error, we print it before exiting | |
| Err(e) => println!("Error encountered running game: {}", e), | |
| // And if not, we print a message saying we ran cleanly. Hooray! | |
| Ok(_) => println!("Game exited cleanly!"), | |
| } | |
| } |