Publicerad: 9 November 2021
Flappy Bird ASCII
 
        En Flappy Bird ASCII i Rustlang.
Koden
Struct i Rust - struct
        struct Player {
    x: i32,
    y: f32,
    velocity: f32,
    frame: usize, // Usize to index arrays
}
        Konstruktors i Rust - impl
        impl Player {
  fn new(x: i32, y: i32) -> Self {
    Self {
      x,
      y,
      velocity: 0.0,
    }
  }
  fn gravity_and_move(&mut self) {
     // Increment gravity
     if self.velocity < 2.0 {
      self.velocity += 0.2;
    }
    // Apply gravity
    self.y += self.velocity as i32;
    if self.y < 0 {
      self.y = 0;
    }
    // Move the player
    self.x += 1;
  }
  fn flap(&mut self) {
    self.velocity = -2.0;
  }
  fn render(&mut self, ctx: &mut BTerm) {
    ctx.set(
      0,
      self.y,
      YELLOW,
      BLACK,
      to_cp437('@')
    );
  }
}
        ... Och mycket mer!
Baserat på boken "Hands-on Rust".
Med grafik!
