Search⌘ K
AI Features

Keeping Score and Obstacle State

Explore how to maintain player scores and obstacle states in your Rust game. Learn to extend the State struct, update scores dynamically, render obstacles, and reset game state effectively to create a complete playable experience.

Flappy Dragon keeps a count of how many obstacles the player avoids and uses this number for the score. The State object needs to include the player’s current score and the current obstacle. We will extend the State struct and constructor to include these:

Rust 1.40.0
struct State {
player: Player,
frame_time: f32,
obstacle: Obstacle,
mode: GameMode,
score: i32,
}
impl State {
fn new() -> Self {
State {
player: Player::new(5, 25),
frame_time: 0.0,
obstacle: Obstacle::new(SCREEN_WIDTH, 0),
mode: GameMode::Menu,
score: 0,
}
}

Including the obstacle and score in the play

...