Adding the Player

Hands-on Rust — by Herbert Wolverson (35 / 120)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Creating Different Game Modes | TOC | Creating Obstacles and Keeping Score 👉

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:

FirstGameFlappyAscii/flappy_player/src/main.rs

​ ​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.

​③​
The player’s vertical velocity. This is an f32, a floating-point number. Unlike integer types, floating-point numbers can contain a fractional portion expressed as a decimal. A floating-point number could contain the value 1.5; an equivalent integer would be equal to 1 or 2. Using whole integers to represent velocity can work, but gives very sudden — and unfair — falls. Using a floating-point number allows you to use fractional velocity numbers — and provides a much smoother…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.