Menu
CoddyTech

Out of Hearts

Part of the Projects section of Coddy's Godot journey. Lesson 32 of 33.

When the last heart is gone, the game is over. Test for zero right after hiding the heart:

if health == 0:
	$HUD/Message.text = "Game Over"
	get_tree().paused = true

The hero and the guard are Pausable, so they freeze. Main is Always, so its _process keeps running. It starts a new game when the player presses Enter:

func _process(delta):
	if get_tree().paused and Input.is_action_just_pressed("action"):
		get_tree().paused = false
		get_tree().reload_current_scene()

Reloading resets everything: three hearts, the key on the floor and the door locked.

challenge icon

Challenge

Medium
  1. When health reaches 0, show Game Over in $HUD/Message and pause the game.
  2. While paused, Enter (action) unpauses and reloads the scene.

Try it yourself

extends Node2D

var has_key = false
var health = 3

func _ready():
	$Key.body_entered.connect(_on_key_body_entered)
	$DoorZone.body_entered.connect(_on_door_zone_body_entered)
	$Enemy.body_entered.connect(_on_enemy_body_entered)

func _on_key_body_entered(body):
	has_key = true
	$Key.queue_free()
	$HUD/KeyIcon.visible = true

func _on_door_zone_body_entered(body):
	if has_key:
		$Door.queue_free()
		$DoorZone.queue_free()

func _on_enemy_body_entered(body):
	health -= 1
	$HUD/Hearts.get_child(health).visible = false
	$Hero.flash()
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Projects

Practice on your own: Godot playground