Godot. Create enemies

Submitted by tech4life on

It’s time to create an enemy for the game. In the game we are working on, our enemy is a series of meteorites of different sizes and shapes that our player must dodge.

Let’s get to it!!!

The first step is to create the scene, in which we will have a Node2D, a Sprite2D, and a CollisionShape2D.

In the Sprite2D, we will load our meteorite image, as we always do with 2D game sprites. For the CollisionShape, we will set the shape to RectangleShape2D, and we will rotate and enlarge it to match the size of our sprite.

The next step is to add a group to the root node of the scene; in our case, we will call it the meteor group, which will be important later on.

Our scene needs a script, which we will add as usual to the root node, and in it, we will add the following code:

Let’s analyze it. In our game, the meteorites fall from the top of the screen and have a specific direction and speed.

To achieve this, we have exposed two variables with the speed and direction values that we can modify from Godot.

In the _ready function, we set the speed of our meteorite. To do this, we calculate a random value within a range defined by the speed we have indicated, to which we add a value that depends on the current level of the game.

To calculate the direction, we do something similar: we look for a random number. In some cases, it will fall straight, and in others, we will calculate a deviation.

Finally, in the _physics_process function, we update the x and y position of the meteorite, and when it goes off-screen, we remove it (queue_free).

To add more diversity to the game, we duplicated the recently created scene and modified the size and orientation of the meteorite. This way, when creating meteorites in our game, we can diversify a bit, and not all meteorites will be the same. We could even use different sprites if we want.

Now we will go to the script of our main scene and, at the beginning, create an array with the preloaded meteorite scenes:

In the same script, we have created a function to create the meteorites on the game screen.

Let’s analyze the code. We get the instance of one of the scenes from the meteorite array, calculating the index randomly. Then we position it randomly at the top of the screen, and to calculate the x (horizontal) position, we calculate a random number. As you can see, by making everything random, we eliminate the possibility of all meteorites coming from the same point.

We will call this function whenever we want; in our case, we have created an infinite timer that calls this function every second:

Finally, we just need to add the code for when a meteorite collides with the player.

In the player-related script, we will add this piece of code. In the _on_area_entered function, we will check if the object that entered (parameter area) belongs to the meteor group. In that case, we will emit a signal indicating that the player has died and remove the player from the game (queue_free).

Now we just need to handle this isDead signal somewhere in our main script and treat it accordingly. For example, by showing a game over message and the option to start again.

 

Nivel