Creating Obstacles
Learn how to add walls as obstacles for the player.
We'll cover the following...
The other key objective of Flappy Dragon is dodging obstacles. Let’s add walls to the game, with gaps that the dragon can fly through. Begin by adding another struct
to the program:
Press + to interact
struct Obstacle {x: i32,gap_y: i32,size: i32}
Obstacles have an x
value, defining their position in world-space (to match the player’s world-space x value). The gap_y
variable defines the center of the gap through which the dragon may pass. size
defines the length of the gap in the obstacle.
We’ll need to define a constructor for the obstacle:
Press + to interact
impl Obstacle {fn new(x: i32, score: i32) -> Self {let mut random = RandomNumberGenerator::new();Obstacle {x,gap_y: random.range(10, 40),size: i32::max(2, 20 - score)}}
Computers aren’t great at generating genuinely ...