Skip to content

Brick Wall

By the end of this lab you'll be able to:

  • Write a brick() function and call it from nested loops
  • Apply the offset row pattern to stagger alternate rows
  • Use pencolor and fillcolor separately to add outlines to filled shapes

Eight rows of warm red-brown bricks fill the canvas, each row staggered by half a brick — just like a real wall laid by a bricklayer.

Welcome to the Brick Wall!

Monty waving welcome Brick laying has a secret: alternate rows are shifted by half a brick so cracks don't line up and the wall stays strong. You'll code that same pattern right now! Let's code it together!

How the Wall Works

The brick(x, y, w, h, color) function draws one filled rectangle with a dark outline. The offset pattern: if row % 2 == 1 (an odd row), shift the starting x by half a brick width. This staggers the rows so each brick spans the joint of two bricks below it.

Sample Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import turtle
monty = turtle.Turtle()
monty.speed(0)
monty.hideturtle()

def brick(x, y, w, h, color):
    monty.penup()
    monty.goto(x, y)
    monty.pendown()
    monty.fillcolor(color)
    monty.pencolor('black')
    monty.begin_fill()
    for side in [w, h, w, h]:
        monty.forward(side)
        monty.left(90)
    monty.end_fill()

bw, bh = 70, 28
colors = ['firebrick', 'brown', 'darkred']

for row in range(8):
    offset = bw // 2 if row % 2 == 1 else 0
    for col in range(-3, 5):
        x = col * bw + offset - 180
        y = -150 + row * bh
        brick(x, y, bw - 3, bh - 3, colors[(row + col) % 3])

What Do You Think Will Happen?

Monty thinking Odd rows (row % 2 == 1) are shifted right by bw // 2 = 35 pixels. Will the bricks in row 1 (odd) line up with the joints in row 0, or with the middles of row 0's bricks? Make your guess — then click Run to find out!

Try It Now


  

Row 1's bricks line up with the middles of row 0's bricks — that's what makes it a proper offset bond. Were you right?

How It Works

monty.fillcolor(color) and monty.pencolor('black') set the fill and outline colors independently. This is how you draw filled shapes with a visible border — monty.color(c) sets both to the same color, but fillcolor/pencolor let you separate them.

offset = bw // 2 if row % 2 == 1 else 0 is Python's ternary expression: a compact if/else on one line. It reads: "set offset to half a brick if the row is odd, otherwise zero."

Explanation Table

Line What it does
monty.fillcolor(color) Set fill color separately from outline
monty.pencolor('black') Set outline color to black
offset = bw // 2 if row % 2 == 1 else 0 Ternary: offset odd rows by half a brick
for side in [w, h, w, h] Draw four sides in one loop
colors[(row + col) % 3] Cycle three brick shades across the wall

Learning Check

Spot the Bug!

Monty warning The program below draws a grid of bricks but they are NOT staggered — all rows line up! Fix the one line that computes the offset so alternate rows shift by half a brick width.


  

Change offset = 0 to offset = bw // 2 if row % 2 == 1 else 0.

Experiments

  1. Change brick colors. Replace ['firebrick','brown','darkred'] with ['lightyellow','beige','wheat'] for a light stone wall. You'll know it worked when the wall color changes completely.

  2. Make taller bricks. Change bh = 28 to bh = 40. You'll know it worked when there are fewer rows of taller bricks.

  3. Try running-bond offset. Change the offset to bw // 3 for a different pattern called running bond. You'll know it worked when the stagger is one-third of a brick instead of one-half.

  4. Add mortar. Change bh - 3 to bh - 5 and bw - 3 to bw - 6. The larger gaps show more "mortar" between bricks. You'll know it worked when the dark gaps become wider.

Incredible Work!

Monty celebrating You coded a brick wall complete with offset rows, separate fill and outline colors, and a three-shade color cycle — that's real craft in Python! Up next: Stars of Different Sizes — the geometry of a 5-pointed star.