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
EasyMake the spikes cost a life.
- In
_on_player_hurt(), take one away fromlives. - Then update
$HUD/Livesthe same way_ready()does.
Press Play and walk onto the spikes: the label shows Lives: 2. Then Check.
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
All lessons in Game Rules
Practice on your own: Godot playground