A Wall of Bricks
Part of the Projects section of Coddy's Godot journey. Lesson 5 of 33.
The bricks come next: 5 rows of 8, built by main.gd when the game starts. A brick is a scene, brick.tscn, that you preload and instantiate.
A for loop runs once for each number. for row in 5: runs with row as 0 to 4. A loop inside a loop visits every column of every row:
for row in 5:
for column in 8:
var brick = BRICK.instantiate()
brick.position = Vector2(68 + column * 72, 60 + row * 26)
add_child(brick)Columns are 72 pixels apart and rows 26. That leaves a small gap between the 64 by 20 bricks.
Set each brick's color before you add it, one per row: brick.color = COLORS[row].
Challenge
MediumIn main.gd:
- Add
const BRICK = preload("res://brick.tscn")and aCOLORSlist (below). - In
_ready, build 5 rows of 8 bricks atVector2(68 + column * 72, 60 + row * 26), each withbrick.color = COLORS[row], and add each withadd_child.
COLORS is [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)].
Try it yourself
extends Node2D
func _ready():
pass
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Projects
1Project: Breakout
Breakout PaddleKeep the Paddle on ScreenPractice on your own: Godot playground