Menu
CoddyTech

A Wall of Bricks

Part of the Projects section of Coddy's Godot journey. Lesson 5 of 36.

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.

Each row takes its color from a list. Set brick.color = COLORS[row] before you add the brick: it paints itself in _ready(), which runs inside add_child.

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)]
challenge icon

Challenge

Medium

Build a wall of 40 bricks.

  1. In main.gd, add const BRICK = preload("res://brick.tscn") and the COLORS line from above.
  2. In _ready, build 5 rows of 8 bricks at Vector2(68 + column * 72, 60 + row * 26).
  3. Set brick.color = COLORS[row] before you add each brick with add_child.

Press Play: five colored rows of bricks fill the top. Then Check.

Try it yourself

extends Node2D

func _ready():
	pass

All lessons in Projects

Practice on your own: Godot playground