Added several method implementations for menu

This commit is contained in:
Fabian Becker 2022-01-28 12:04:30 +01:00
parent c45cdd8573
commit 698b11855f

View File

@ -4,6 +4,11 @@ struct State {
mode: GameMode,
}
struct Player {
x: i32,
y: i32,
velocity: f32,
}
impl State {
fn new() -> State {
@ -11,8 +16,46 @@ impl State {
mode: GameMode::Menu,
}
}
fn play(&mut self, ctx: &mut BTerm) {
self.mode = GameMode::End;
}
fn restart(&mut self) {
self.mode = GameMode::Playing;
}
fn main_menu(&mut self, ctx: &mut BTerm) {
ctx.cls();
ctx.print_centered(5, "Welcome to Flappy Dragon");
ctx.print_centered(8, "(P) Play Game");
ctx.print_centered(9, "(Q) Quit Game");
if let Some(key) = ctx.key {
match key {
VirtualKeyCode::P => self.restart(),
VirtualKeyCode::Q => ctx.quitting = true,
_ => {}
}
}
}
fn dead(&mut self, ctx: &mut BTerm) {
ctx.cls();
ctx.print_centered(5, "You are dead!");
ctx.print_centered(8, "(P) Play Game");
ctx.print_centered(9, "(Q) Quit Game");
if let Some(key) = ctx.key {
match key {
VirtualKeyCode::P => self.restart(),
VirtualKeyCode::Q => ctx.quitting = true,
_ => {}
}
}
}
}
enum GameMode {
Menu,
@ -31,6 +74,8 @@ impl GameState for State {
}
fn main() -> BError {
let context = BTermBuilder::simple80x50().with_title("Flappy Dragon").build()?;
let context = BTermBuilder::simple80x50()
.with_title("Flappy Dragon")
.build()?;
main_loop(context, State::new())
}