Creating Obstacles and Keeping Score

The other key part of Flappy Dragon is dodging obstacles. Let’s add walls to the game, with gaps through which the dragon may fly. Add another struct to your program:

 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.

You’ll need to define a constructor for the obstacle:

 impl​ Obstacle {
 fn​ ​new​(x: i32, score: i32) ​->​ Self {
 let​ ​mut​ random = ​RandomNumberGenerator ...

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.