Breakout Lives
Part of the Projects section of Coddy's Godot journey. Lesson 8 of 36.
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, listen for it the way you listen for brick_broken. The function it calls takes a life and shows the new count, as the score function does with points.
Challenge
HardGive the player three lives.
- In
ball.gd, addsignal missed. Belowy = 380, emit it and serve again as shown above. - In
main.gd, addvar lives = 3and showLives: 3in$HUD/Lives. - Connect
$Ball.missedto_on_missed, which takes a life and shows it.
Press Play and miss the ball: the label shows Lives: 2 and a new ball comes. 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
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
All lessons in Projects
Practice on your own: Godot playground