Minigame Monday: Asteroid Universe

 Asteroid Universe:

  • The Concept:

Do you know what shape the universe in the game asteroids is? I found out when I was younger by watching the Royal Institution Christmas Lectures that it's actually a donut. If you project the game onto a donut, you'll find that if you go off the top, you come back on the bottom, and if you go off the side you'll come back on the other side, just like the game. But what if the game universe was a different shape? This week's minigame experiments with that, allowing you to change the shape of the universe as you play.

  • The Challenge:

So to change the shape of the asteroid universe, first I needed to recreate the game. I started with the ship physics. Controlling rotation was simple, drawing the rotated image less so. The issue with rotating the image is that pygame images are by default positioned at their top left, so if you rotate them, they rotate around that, rather than around their center. This needs to be accounted for when rotating them.

After adding a momentum variable that was modified by the ship accelerating, I ran into another challenge with brakes for the ship. Adding a change in momentum in the direction of the back of the ship was easy, but that allowed you to accelerate backwards, which wasn't what I wanted, I just wanted the back button to slow the momentum. To do this, I made it so the back key only applies a force if the direction of the momentum is in the opposite semicircle; if the momentum is in the direction the ship is facing, the breaks work, but if the momentum is in the other direction, they don't. This gave me more or less the effect I wanted; if you spun out of control, the breaks wouldn't be that effective, but I guess that's sort of realistic. Talking of realistic, I checked afterwards how the original asteroids approacted slowing down. It turns out there is just a very strong friction force acting on the spaceship, as of course space is known for!

Next, time for the bullets and asteroids. In both cases, I took an object oriented approach to this, which served me well. Each bullet or asteroid stores its position, momentum and rotation, allowing me to check through a list of the objects for drawing, reading the details as needed. Bullets also stored their time to live, and asteroids a boolean to determine if they were big asteroids or small asteroids.

Finally, it was time to add the shapes of the universe. It's good game design (and programming in general) to make sure functions are as multipurpose as possible, so I added a HandleEdges function. This function was designed so it would function independantly of what called it, so it would work the same for the ship, bullets and the asteroids. Each would call it every time they moved, and if the movement put them off an edge, the function would work out where they should be put, and what their new momentum should be. It does this by calling sub-functions, based on what the current universe shape is.

  • The Result:

I'm happy with how this game turned out, mostly just because I managed to recreate asteroids pretty well. Although the game plays slightly differently, it still has much of the feel of the original, and I'm especially happy with how the asteroids look when they split. Switching the universe also works well, but I realise that for the shapes other than donut I should have considered rotation of the objects, as well as the position and momentum. I'd also have loved to create a spherical universe, but I think that might be a bit difficult in terms of maths, and might even need some distortion to work properly. See which universe shape is your favourite at https://reddragonmakesgames.itch.io/asteroid-universe, and the code is on github.

Comments

Popular posts from this blog

Armageddump (Boss Rush Jam 2023)

Introducing: Minigame Monday! (3D Minesweeper)

Minigame Monday: Covert Behaviour