Hero image!

Asteroids clone in Godot

November 06, 2023 - Drygast
Basics Game Dev GDScript Godot

To reiterate from my previous godot related post (kanelbulleclicker). I'm no expert with the Godot Engine; I'm just documenting my journey as I learn here. My experience is mostly trial-and-error with lots of research along the way. Through this "log", I hope to share what I've learned so far and provide a starting point for others who may be in a similar situation.

If you want to try a HTML version of this project before reading all of this, you can find it at /games/asteroids.

To see all code and download the project, take a look in my Azure repo at https://dev.azure.com/drygast/public/_git/GodotAsteroids, but I'll copy a few things here.

This game is somewhat finished, except for audio.

Animations

The whole reason for why this this project exists was that I wanted to take a look into animations from sprite maps and some particle effects. I was not aiming for perfektion or anything, just simply figuring out how they worked and apply it to a small game project.

SpriteMaps

The first thing I wanted animated was the main thrust of the spaceship. So basically - whenever the thrust is applied that makes the spaceship move, I wanted som sort of blue effect that shows that force is applied at the opposite direction the spaceship is facing.

Before I got to that point, I had to add some input mappings for the 4 actions. Thrust, rotate (left & right) and fire.

Input mapping

After I had my inputs, I added a spaceship as a new node (CharacterBody2D). After that I added a few other things, but most importantly the thruster (AnimatedSprite2D) behind the player.

Player

The actual sprite map of the thruster i simply an image with multipla stages of the animation.

Thruster sprite map

Next I had to import animation frames from the thruster image and that was done by clicking the "Add frames from sprite sheet" button. Adjust the numbers for horizontal and vertical frames based on the spritemap. In my case I hade 4 horizontal and 1 vertical. After importing you should see something like this:

Animation frames

After the import was done, I tweaked a number of different prioperties to get the effect that I was after.

AnimatedSprite2D

After this it was just a matter of reacting to the input and show/animate the thruster animation.

func _process(_delta):
	if Input.is_action_pressed("playerThrottle"):
		thruster.play("default")
		thruster.visible = true
	else:
		thruster.stop()
		thruster.visible = false

Obviously there was a lot of other stuff involved in order to make the spaceship move, but I'm not going to cover that here.

I also created a spritemap for explosion effects when a UFO hits the player, but the basic setup was very similar.

Particles

I also wanted a small "dust explosion" effect whenever an asteroid was destroyed. I went for a particle effect and created a basic particle image based of the color of the asteroids.

Dust particle

I then created a GPUParticles2d node and added my particle image to it.

Autoload

I hade to tweak a number of parameters in order for it to look OK.

Autoload

Now I needed to add a few things such as starting the particles when certain signals was received (like when a bullet collided with an asteroid).

func _on_asteroid_collision_with_bullet(asteroidPosition):
	var asteroidDustExplosion = asteroidDustExplosionScene.instantiate()
	asteroidDustExplosion.position = asteroidPosition
	self.add_child(asteroidDustExplosion)

For the effect itself, I had to add som code to make sure that it was gone when the timer expired.

func _on_timer_timeout():
	queue_free()

And after adding thee collisiondetecting for the asteroids (Area2D) nodes and a few signals I ended up with this little effect:

Dust explosion

Final thoughts

Now, obviously there is a lot more to this project than the small animations, but I think I've talked about the other things in previous articles. Overall - this was fun and I really want to dig deeper into another project to learn even more soon. Not sure what that project would be, but I've seen a few interesting side-scrollers or mobile games that I want to replicate. This is fun!