Menu
CoddyTech

Breakout Game Over

Part of the Projects section of Coddy's Godot journey. Lesson 9 of 36.

Three misses should end the game. At zero lives, show Game Over and pause with get_tree().paused = true. The paddle and ball are Pausable, so they freeze. Main is Always, so it keeps running and can read Enter:

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

Unpause before you reload. The pause belongs to the tree, not the scene, so a reloaded scene would start frozen.

challenge icon

Challenge

Hard

End the game when the last life is gone.

  1. In _on_missed, when lives is 0, show Game Over in $HUD/Message and pause the game.
  2. Add _process: when the game is paused and action was just pressed, unpause it, then reload the scene.

Press Play and miss three balls: Game Over shows and the ball stops. Enter starts again. Then Check.

Try it yourself

extends Node2D

const BRICK = preload("res://brick.tscn")
const COLORS = [Color(1, 0.42, 0.42), Color(1, 0.78, 0.34), Color(0.49, 0.95, 0.75), Color(0.369, 0.635, 1), Color(0.75, 0.82, 0.95)]

var score = 0
var lives = 3

func _ready():
	for row in 5:
		for column in 8:
			var brick = BRICK.instantiate()
			brick.position = Vector2(68 + column * 72, 60 + row * 26)
			brick.color = COLORS[row]
			add_child(brick)
	$Ball.brick_broken.connect(_on_brick_broken)
	$HUD/Score.text = "Score: 0"
	$Ball.missed.connect(_on_missed)
	$HUD/Lives.text = "Lives: 3"

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

func _on_missed():
	lives -= 1
	$HUD/Lives.text = "Lives: %d" % lives

All lessons in Projects

Practice on your own: Godot playground