Menu
CoddyTech

Game Over

Part of the Game Rules section of Coddy's Godot journey. Lesson 12 of 29.

The HUD has a big GameOver label in the middle of the screen. Its visible is off in the scene, so it starts hidden.

After a life is lost, check whether any are left:

if lives == 0:
	$HUD/GameOver.visible = true

Two equals signs compare. lives == 0 asks whether lives is 0. One equals sign sets a value, and Godot does not allow it inside an if.

challenge icon

Challenge

Easy

Show Game Over when the last life is lost.

  1. In _on_player_hurt(), under the label line, check if lives is 0.
  2. If it is, show $HUD/GameOver.

Press Play and hit the spikes three times: Game Over appears. 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():
	lives -= 1
	$HUD/Lives.text = "Lives: %d" % lives

All lessons in Game Rules

Practice on your own: Godot playground