Editor

Canada Flag in Python Turtle

Intermediate · Flags · 60 lines · draws in about 11 seconds

This program draws the flag of Canada and fills the 11-pointed maple leaf from a list of points for its right half, mirrored to make the left.

Canada Flag in Python Turtle: the finished drawing
import turtle
# The flag is twice as long as it is high: red, white, red,
# with the white square in the middle half of the flag
HEIGHT = 280
LENGTH = HEIGHT * 2
RED = "#D52B1E"
# The right half of the maple leaf, from the bottom of the stem up to
# the top point. The leaf is symmetric, so the left half is a mirror image.
RIGHT_HALF = [
(5, -118), (2, -61), (59, -71), (52, -50), (108, -4), (93, 3), (105, 40),
(70, 33), (63, 50), (29, 14), (44, 88), (21, 75), (0, 117),
]
screen = turtle.Screen()
screen.title("Flag of Canada")
screen.bgcolor("#dde6ee")
t = turtle.Turtle()
t.speed(6)
def rectangle(x, y, width, height, color):
"""Draw a filled rectangle whose top-left corner is at (x, y)."""
t.penup()
t.goto(x, y)
t.setheading(0)
t.pendown()
t.color(color)
t.begin_fill()
for side in range(2):
t.forward(width)
t.right(90)
t.forward(height)
t.right(90)
t.end_fill()
# Red band, white square, red band
left = -LENGTH / 2
top = HEIGHT / 2
rectangle(left, top, LENGTH / 4, HEIGHT, RED)
rectangle(left + LENGTH / 4, top, LENGTH / 2, HEIGHT, "white")
rectangle(left + LENGTH * 3 / 4, top, LENGTH / 4, HEIGHT, RED)
# The maple leaf: up the right side, then back down the left side
t.penup()
t.goto(RIGHT_HALF[0])
t.pendown()
t.color(RED)
t.begin_fill()
for x, y in RIGHT_HALF:
t.goto(x, y)
for x, y in reversed(RIGHT_HALF):
t.goto(-x, y)
t.end_fill()
t.hideturtle()
turtle.done()

How the code works

Half a leaf as a list of points

Each pair is an (x, y) point on the right edge of the maple leaf, from the foot of the stem up to the top point. The points follow the official leaf, scaled to a flag 280 high.

RIGHT_HALF = [
    (5, -118), (2, -61), (59, -71), (52, -50), (108, -4), (93, 3), (105, 40),
    (70, 33), (63, 50), (29, 14), (44, 88), (21, 75), (0, 117),
]

Red, white, red

The flag is twice as long as it is high. Each red band is a quarter of the length and the white square in the middle is half of it.

rectangle(left, top, LENGTH / 4, HEIGHT, RED)
rectangle(left + LENGTH / 4, top, LENGTH / 2, HEIGHT, "white")
rectangle(left + LENGTH * 3 / 4, top, LENGTH / 4, HEIGHT, RED)

Mirroring the second half

The first loop goes up the right side through every point. The second loop comes back down through the same points in reverse order with -x, which is the left side, and the fill colours the whole leaf.

for x, y in RIGHT_HALF:
    t.goto(x, y)
for x, y in reversed(RIGHT_HALF):
    t.goto(-x, y)

Make it yours

Change the code above and press Run. Some ideas:

  • Add t.pencolor("black") after t.color(RED) in the leaf section to see the outline the turtle follows.
  • Change t.speed(6) to t.speed(1) and count the 11 points of the leaf as the turtle reaches them.
  • Change t.goto(-x, y) to t.goto(x, y) to see why the second half has to be mirrored.

What it uses

Every turtle command the site supports is listed on Python turtle online.