Using the AnimatedSprite, CanvasLayer, and Label nodes for graphics and UI

Handling basic input and signals to drive game logic

In-game generation of instances through script

In this hour, you will use what you learned in the previous hours to create an actual game. It will be a simple 2D, top-down, side-scrolling, shoot-’em-up type of game where you control a spaceship and shoot at incoming asteroids. The game will keep track of the number of asteroids you have shot down and end when an asteroid collides with your ship. In the process of making the game, you will learn some concepts we only talked about briefly or haven’t mentioned yet, like input, project settings, animated Sprites, timers, areas, and UI. Don’t worry though; we will explain them as we go, and later chapters will cover them in more depth.

Concept and Design

The spaceship is controlled with the arrow keys and can shoot lasers by pressing the spacebar. Asteroids spawn at random locations outside the screen and fly toward the spaceship from right to left. When the spaceship hits an asteroid with its laser, the asteroid explodes and the player scores a point. If an asteroid manages to touch the spaceship, both explode and the game is over. The game (see Figure 5.1) can be restarted by pressing the enter key, and can be exited at any time by pressing the escape key.

We will model the interactions of the spaceship, the laser shots, and the asteroids using Area2D nodes and signals, which we briefly mentioned at the end of Hour 4. We will also make a simple UI (user interface) showing the current score using Godot’s UI nodes.

We will make the ship move on a static screen with a non-moving camera, simulating the movement of the ship through space by an infinitely scrolling background and asteroids that move in and out of the screen from right to left. The advantage of this method over an actually moving spaceship is that we do not need to create huge horizontal levels, which can be difficult to manage. We can also more flexibly and precisely time and randomize the appearance of asteroids. If you want to extend the game with enemy ships, power-ups, and bosses, you can spawn them in an easier and timed fashion by using the static screen method.

Game Components as Scenes

Even a simple game like we are going to make has a lot of complexity and interaction between different game parts. To tackle this complexity, we can use many of Godot’s helpful concepts to manage our game components. We can build our spaceship, the asteroids, and the laser shots as separate scenes that communicate indirectly through signals with each other. This allows us to work on each part in isolation. We will create the scenes shown in Table 5.1.

TABLE 5.1 Scenes Needed for the Game

Scene

Description

player

The player scene consists of an Area2D and a CollisionShape2D node used to detect collisions with asteroids, an AnimatedSprite node representing the spaceship on screen, and a Timer node that is used to limit the shooting rate of the player. A player script is attached to the root node of the player scene that contains the movement, the shooting, and destruction logic of the spaceship.

shot

This scene represents a laser shot of the spaceship and consists, similarly to the player scene, of an Area2D, CollisionShape2D, and AnimatedSprite node. Its attached script handles collisions with asteroids and moves the shot forward every frame.

asteroid

The asteroid scene also consists of an Area2D, a CollisionShape2D, and an AnimatedSprite. The asteroid’s script handles collision with the player and the laser shots, as well as moving the asteroid forward every frame.

explosion

Consisting of a Sprite and a Timer node, the explosion scene is instanced into the game when an asteroid or the spaceship is destroyed. The Timer node makes sure that the explosion is only shown briefly.

stage

The stage scene is our main scene, which contains instances of all of the above scenes. Additionally, it contains UI text for the score, the “game over” message, and an infinitely scrolling background Sprite. The stage scene root has a script attached that spawns new asteroids periodically, detects if an asteroid is destroyed, and updates the score accordingly. It also detects if the spaceship crashed into an asteroid and shows the “game over” message.