rust-flappy-dragon/src/main.rs

72 lines
1.5 KiB
Rust

#![warn(clippy::all, clippy::pedantic)]
use bracket_lib::prelude::*;
struct State {
mode: GameMode
}
enum GameMode {
Menu,
Playing,
Dead,
}
impl State {
fn new() -> Self {
State {
mode: GameMode::Menu,
}
}
fn menu(&mut self, ctx: &mut BTerm) {
ctx.cls();
ctx.print_centered(5, "Welcome to Flappy Dragon");
ctx.print_centered(8, "(P) Play");
ctx.print_centered(9, "(Q) Quit");
if let Some(key) = ctx.key {
match key {
VirtualKeyCode::P => self.restart(),
VirtualKeyCode::Q => ctx.quitting = true,
_ => {},
}
}
}
fn restart(&mut self) {
self.mode = GameMode::Playing;
}
fn play(&mut self, ctx: &mut BTerm) {
ctx.cls();
ctx.print(1, 1, "Playing...");
self.mode = GameMode::Dead;
}
fn dead(&mut self, ctx: &mut BTerm) {
ctx.cls();
ctx.print(1, 1, "You died!");
self.mode = GameMode::Menu;
}
}
impl GameState for State {
fn tick(&mut self, ctx: &mut BTerm) {
match self.mode {
GameMode::Menu => self.menu(ctx),
GameMode::Playing => self.play(ctx),
GameMode::Dead => self.dead(ctx),
}
}
}
fn main() {
let ctx = BTermBuilder::simple80x50()
.with_title("Flappy Dragon")
.build()
.unwrap();
main_loop(ctx, State::new()).unwrap();
}