Hands-on Rust: flappy_player y position. (Page 59)

In the flappy bird example in the book, the example code uses i32 as the type for the player’s y position. This has a possible unintended effect in the gravity_and_move function: The player position will not be modified until the speed is greater than 1.0. This is because the line:

self.y += self.velocity as i32;

will convert the velocity to an i32 (rounding towards 0) before updating the player’s y position.

This has a big effect in playability, as every time you go press the spacebar, your velocity changes to -2.0, and then when it gets from -1 to 1, your y position will not change at all.

A better solution would be to set the y position as an f32, and cast it to int only when you want to draw it on the terminal.

1 Like