Clear the Wall
Part of the Projects section of Coddy's Godot journey. Lesson 10 of 36.
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
MediumWin the game by breaking every brick.
- In
main.gd, addvar bricks_left = 40. - In
_on_brick_broken, take one offbricks_left. - When none are left, show
You win!in$HUD/Messageand pause the game.
Press Play and clear the wall: You win! shows and the ball stops. 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 _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