Space Race: a simple Rust game built on the Bevy framework

When I saw a post announcing the release of the Bevy framework (an open source game engine written in Rust), I thought it'd be a fun way to learn Rust. 

So I made Space Race, a 2 player game on top of the Bevy framework.

How to Play

The Astronaut moves around collecting jewels (using WASD keys). The Alien moves around trying to catch the Astronaut (using arrow keys). 


The Astronaut wins if they collect at least 5 jewels and make it back to home base before the Alien catches them. 


The Alien wins otherwise. 


Things I learned about Rust 

I had a lot of fun making & playing this game, and also got some hands on experience with Rust like working with
  • structs
  • enums
  • vectors
  • iterators
  • traits
    • dyn trait vs impl trait 
      • dyn trait is used for trait objects, which are fat pointers made up of 1) a pointer to the value 2) a pointer to a vtable. A vtable is a table of methods, where each method points to its implementation, generated once at compile time. Each instance of a type that implements a trait uses the same vtable. Then, Rust uses dynamic dispatch, selecting the right implementation of a method at runtime.
      • When a function takes in an `impl trait`, Rust generates a different version of the function for each type that implements the trait.   
      • Use trait objects if you create a collection of instances of mixed types that implement the same trait or want to reduce the size of your compiled code.
      • Use `impl` trait when you need speed (and want to avoid dynamic dispatch). 
  • the rand crate (the positions of the walls and the jewels in the game are generated randomly)
  • ownership and passing references 
  • atomics 
    • Space Race needed a static mutable bool variable, so I used an AtomicBool. 

Developing on Bevy 

Bevy was simple and intuitive to use. However, the documentation was lacking and there were bugs I ran into (e.g. when trying to combine a SpriteSheet and TextComponent). These are of course expected, since this framework is new. Developing on Bevy was a positive experience overall, and Bevy has a lot of potential. 

Space Race Code

Resources

Popular posts from this blog

Building a Toy Language Compiler in Go

tldr: Raft + Implementation Tips