Breakout Lives
Part of the Projects section of Coddy's Godot journey. Lesson 8 of 33.
When the paddle misses, the ball falls out of the bottom. Give the player three lives.
In ball.gd, a ball below y = 380 was missed. Emit a missed signal and serve the ball again above the paddle.
if position.y > 380:
missed.emit()
position = Vector2(320, 290)
velocity = Vector2(110, -280)In main.gd, count the lives down. At zero, 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
Hard- In
ball.gd, addsignal missed. Whenposition.yis over 380, emit it and put the ball back at(320, 290)with velocity(110, -280). - In
main.gd, start withvar lives = 3and showLives: 3in$HUD/Lives. Each miss takes a life and updates the label. - At 0 lives, show
Game Overin$HUD/Messageand pause the game. - While paused, Enter (the
actionkey) unpauses and reloads the scene.
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
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"
func _on_brick_broken():
score += 10
$HUD/Score.text = "Score: %d" % score
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