Adding the Player

In Flappy Dragon, the player battles gravity while trying to avoid obstacles and stay alive. To keep the dragon in flight, the player needs to press the SPACEBAR to flap the dragon’s wings and gain upward momentum. For this to work, you need to store the current game attributes of the dragon. Add a new struct to your main.rs file, after the enum declaration:

 struct​ Player {
x: i32,
y: i32,
velocity: f32,
 }

The x position of the player. This is a world-space position—specified in terminal characters. The player will always render on the left of the screen. x represents progress through the level.

The vertical position of the player in screen-space.

Get Hands-on Rust now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.