Menu
CoddyTech

Dodge: Game Over

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

At zero lives, the game goes on. The score keeps counting and the lives go below zero.

End the game the way you did in the spike room. Show the hidden GameOver label and pause the game. The pause stops both timers, so the score stays at the seconds you survived. The blocks stop where they are.

if lives == 0:
	$HUD/GameOver.visible = true
	get_tree().paused = true
challenge icon

Challenge

Medium

In _on_player_hurt(), after the label line, check if lives is 0. If so, show $HUD/GameOver and pause the game.

Try it yourself

extends Node2D

var block_scene = preload("res://block.tscn")
var score = 0
var lives = 3

func _ready():
	$HUD/Score.text = "Score: %d" % score
	$HUD/Lives.text = "Lives: %d" % lives
	$SpawnTimer.timeout.connect(_on_spawn_timer_timeout)
	$ScoreTimer.timeout.connect(_on_score_timer_timeout)
	$Player.hurt.connect(_on_player_hurt)

func _on_spawn_timer_timeout():
	var block = block_scene.instantiate()
	block.position = Vector2(randf_range(40, 600), -30)
	add_child(block)

func _on_score_timer_timeout():
	score += 1
	$HUD/Score.text = "Score: %d" % score

func _on_player_hurt():
	lives -= 1
	$HUD/Lives.text = "Lives: %d" % lives
quiz iconTest yourself

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