Creating Different Game Modes
Hands-on Rust — by Herbert Wolverson (34 / 120)
👈 Understanding the Game Loop | TOC | Adding the Player 👉
Games typically run in modes. Modes specify what the game should do on the current tick, for example, displaying the main menu or the game over screen. In computer science, this is often formalized as a state machine. It’s a good idea to start by defining your game’s basic mode structure, which acts as an outline for the rest of the program.
Flappy Dragon requires three modes:
- Menu: The player is waiting at the main menu.
- Playing: Game play is in progress.
- End: The game is over.
Transitions between these modes are relatively straightforward:
Game modes are best represented as an enum. Enumerations allow you to limit the value of a variable to one of a set of possible states. Underneath the prelude import, add a GameMode enumeration to represent the available game modes:
FirstGameFlappyAscii/flappy_states/src/main.rs
①enum GameMode {
② Menu,
Playing,
End,
}
①
GameMode is an enum, like the ones you used in the treehouse example.