Basic input and menu
This commit is contained in:
parent
22f3c6a3c0
commit
ca7c815c45
1776
Cargo.lock
generated
Normal file
1776
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
|
@ -6,3 +6,4 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
bracket-lib = "0.8.1" # Meta-crate holding the entirety of bracket-lib (and exposing it). Use this for the full r...
|
||||
|
|
72
src/main.rs
72
src/main.rs
|
@ -1,3 +1,71 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
#![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();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue