Clear the Wall
Part of the Projects section of Coddy's Godot journey. Lesson 9 of 33.
The last step is a way to win: break every brick.
Count the bricks that are left. Start at 40 and take one off each time brick_broken arrives. At zero the wall is clear:
bricks_left -= 1
if bricks_left == 0:
$HUD/Message.text = "You Win!"
get_tree().paused = trueTake one off before you test for zero. If you test first, the test sees 1 on the last brick and never sees 0.
The Enter key from Game Over starts a new game here too. Your Breakout is complete.
Challenge
MediumIn main.gd:
- Add
var bricks_left = 40. - Each broken brick takes one off.
- When none are left, show
You Win!in$HUD/Messageand pause the game.
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 _process(delta):
if get_tree().paused and Input.is_action_just_pressed("action"):
get_tree().paused = false
get_tree().reload_current_scene()
func _on_brick_broken():
score += 10
$HUD/Score.text = "Score: %d" % score
func _on_missed():
lives -= 1
$HUD/Lives.text = "Lives: %d" % lives
if lives == 0:
$HUD/Message.text = "Game Over"
get_tree().paused = true
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