Lives
Part of the Game Rules section of Coddy's Godot journey. Lesson 11 of 29.
Many games give you a few lives. Each mistake costs one, and the game ends when they run out. In code, lives are a number:
var lives = 3In this room, the player sends its hurt signal when it touches the red spikes. It then goes back to the green square. _ready() already shows the lives and connects hurt to _on_player_hurt().
To lose a life, take one away, then show the new number.
Challenge
EasyIn _on_player_hurt():
- Take one away from
lives. - Update
$HUD/Lives, so after one hit it showsLives: 2.
Try it yourself
extends Node2D
var lives = 3
func _ready():
$HUD/Lives.text = "Lives: %d" % lives
$Player.hurt.connect(_on_player_hurt)
func _on_player_hurt():
pass
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Game Rules
Practice on your own: Godot playground