Build a complete game with the Godot game engine
Firing Bolts
So far you have a bunch of aliens invading but no way to fight back. The next step, then, is to enhance the turret's defense system and actually make it shoot something when the player hits fire.
Create another Area2D-based scene and call it Bolt. Give it an AnimatedSprite node and add two animations: a default animation with an image of a bullet (mine is an orangey oblong). Call this animation bolt. The second animation is another "explosion" from Open Game Art.
When you fire the bolt, it will stop in its tracks and shift to the animation of the explosion when it hits an alien in the same way the turret explodes when it hits an alien. Lines 14 to 21 in Listing 15 show what that would look like.
Listing 15
Bolt.gd
01 extends Area2D 02 03 var speed = 400 04 05 func _process(delta): 06 position.y -= speed * delta 07 08 func start (bolt_position): 09 position = bolt_position 10 11 func _on_VisibilityNotifier2D_screen_exited(): 12 queue_free() 13 14 func _on_Bolt_area_shape_entered(area_id, area, area_shape, self_shape): 15 area.destroy() 16 speed = 0 17 $CollisionShape2D.set_deferred("disabled", true) 18 $AnimatedSprite.play("explosion") 19 yield($AnimatedSprite, "animation_finished") 20 hide() 21 queue_free()
Use Bolt's area_shape_entered() signal (line 14) to determine which alien the bolt collided with, as it delivers the information you need through its area
parameter. You can then call a new method, destroy()
, in Enemy.gd
that removes the blasted enemy from the node tree.
The collision layer the turret is on must be different from the one the bolt is on; otherwise the bolt will collide with the turret. Open the turret scene and select the Turret node. Click to unfold the Collision section in the node's properties in the Inspector dock on the right, and take a look at the Layer grid. There are 20 layers that Area2D nodes can be on, and by default new Area2D nodes start on layer 1. For the turret that is fine, but now do the same for Bolt and deactivate layer 1 by clicking on it and activate layer 2. Like that, collisions for bolt only happen with other nodes on layer 2.
Your enemies will also be on layer 1. Select the Enemy scene and the top Enemy node and again open Collision. As you need to be able to collide with Turret on layer 1 and Bolt on layer 2, leave layer 1 as active, but also activate layer 2.
To instantiate a Bolt scene when a player hits fire, create a custom signal in Turret.gd
by adding the line signal fire
to the top of the script and then emit the signal with the line emit_signal("fire")
underneath the line that checks for the input from the "fire" button (Listing 4, line 19). While you are at it, you may want to add a timer to Turret so that there is a short delay (half a second works well) between each shot (Figure 11).
Back to the fire signal, open the Main scene and connect the fire signal from Turret to Main. You must write the body of the _on_Turret_fire()
function Godot creates in Main.gd
, making it do the following:
- Preload the Bolt scene.
- Add it as a child instanced node to Main.
- Call the
start()
method inBolt.gd
(Listing 15, line 8).
Listing 16 will do the trick.
Listing 16
_on_Turret_fire() (Main.gd)
01 func _on_Turret_fire(): 02 var bolt = preload("res://Bolt.tscn").instance() 03 add_child(bolt) 04 bolt.start(Vector2($Turret.position.x, $Turret.position.y - 20))
When a player hits fire, a bolt node is created and placed on the tip of the turret's cannon and starts moving upwards (Listing 15, lines 5 and 6) at 400 pixels per second. If it flies off the screen without hitting anything, it is removed in Listing 15 lines 11 and 12. If it hits an alien, you call Enemy.gd
's destroy()
function to remove the alien (line 15), stop the bolt (line 16), and remove the bolt's capacity to collide with anything else (line 17). Then you play the explosion's animation and pause the script until it's finished (lines 18 and 19). Finally, you remove the bolt from the game (lines 20 and 21).
Conclusion
Godot has many other options that this article has not covered. We have not mentioned sound design, exporting your game to work natively on different platforms (Godot supports Linux, Windows, macOS, Android, iOS, and HTML5), or 3D game design.
If you need more ideas, try increasing the difficulty of the Spaced Marauders game by having the aliens fire back and moving quicker as you pick them off. You can also create new levels in which the aliens start lower down, move faster, or fire more. Or you could try adding a scoreboard. If you get stuck, know that I will gradually add all these changes and more to the game, and you can find all the code and assets under a GPL license online [2].
Infos
- Godot game engine: https://godotengine.org/
- Assets and code for Spaced Marauders: https://gitlab.com/linux-magazine/spaced-marauders
- GDScript: https://docs.godotengine.org/en/stable/getting_started/scripting/gdscript/index.html
- GDScript style guide: https://docs.godotengine.org/en/stable/getting_started/scripting/gdscript/gdscript_styleguide.html
- GDScript API: https://docs.godotengine.org/en/stable/classes/index.html
- GDScript inbuilt constants and variables: https://docs.godotengine.org/en/stable/classes/class_@globalscope.html
- Open Game Art: https://opengameart.org/
« Previous 1 2 3 4
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.