Asteroids game (Rust + WebAssembly)


To explore Rust + wasm I made a version of the arcade game Asteroids. This version aims for a more physics-based feel; for example, the asteroids split where you hit them and move accordingly. This is my first such Rust project, and building this in Rust was a joy.

Play it here.

Code is here.

All feedback is welcome. Happy blasting!



wow, though game...

Loved it.
Is it possible to make it so that when asteroids collide with each other they break. I think that would make the physics more realistic

1 Like

This is awesome. What are the rules for blasts striking asteroids? It seems like there's only one face that per asteroid that can be hit to break it up and that must be hit dead center.

1 Like

All hits should have some effect. A hit splits the asteroid through the line of the blast, resulting in two or more pieces; if a piece is small enough then it explodes. Bigger asteroids can't be destroyed in a single hit; they must be broken into smaller and smaller pieces. Does that help?

Love it! Although... maybe your own bullets shouldn't kill you?

1 Like

Why not? Bullets have no eyes.


To be more realistic, the ship shouldn't have more friction than the asteroids. Without friction or gravity, the only way to decelerate is to accelerate backward (with only one motor, accelerating and decelerating take exactly the same time).

The bullets should not disappear suddenly (unless there is a self-destruct mechanism inside?), but the game would be very difficult... Maybe for a "realistic/hard mode"?

1 Like

Amazing work. But frame rate with enabled post-processing is quite low.

Great points! :slight_smile: I like to imagine the ship has small retrorockets that automatically stabilize the ship after a maneuver. (That's how I justify the friction in space to myself anyway.)


If the frame rate is low, then you can click the game screen to toggle the effects on/off. Is the frame rate smooth for you with effects off?

I think I'll have the page check for a low frame rate and turn off the effects automatically.

1 Like

With effects off game works fine. Page check will be a good feature.

1 Like

Mainly for gameplay reasons. With the screen wraparound it's awfully easy to shoot yourself.

1 Like

I thought this was very neat. Good job!

1 Like

Nice game! My only issue is that my screen (and thus my browser page) is smaller than the game's frame. So when I push up to use the thruster, the browser treats that as a scroll event, and scrolls the whole game. In Chrome. :frowning:

But I managed to play successfully without the thruster, using only the left, right, f and s keys. But detecting the browser frame size would be nice.

1 Like

Just press ctrl minus or hold ctrl and scroll down with a mouse to zoom out. That's how I handled it on my screen.

Dang this is cool, but damn near impossible to play on my Atreus keyboard. Any chance you could enable h j k l movements? Holding the layer key down for arrow keys means I can't press the F to fire unless I change layers again, lol.

I just started the Web Assembly with Rust book so I look forward to checking out the code!

1 Like

Sure thing! I added i, j, and l as alternatives to the arrow keys. If you have any trouble, feel free to create an issue or add a comment to this one. Thanks for letting me know!

1 Like

I like this game but have to agree it's just too easy to hit yourself, in combination with all the asteroid fragments that fly around after a while this makes the game unplayable for me quite quickly :blush:

Regarding "friendly fire", I feel like I've seen many versions of Asteroids make your bullets expire after a certain distance, so you can still do intentional wraparound without nearly as high a risk of shooting yourself.

Still very cool how close this is to a polished game considering how little code it takes.