Menu
CoddyTech

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 icon

Challenge

Medium

In main.gd:

  1. Add const BRICK = preload("res://brick.tscn") and a COLORS list (below).
  2. In _ready, build 5 rows of 8 bricks at Vector2(68 + column * 72, 60 + row * 26), each with brick.color = COLORS[row], and add each with add_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
quiz iconTest yourself

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