Editor

American Flag in Python Turtle

Intermediate · Flags · 83 lines · draws in about 20 seconds

This program draws the flag of the United States with 13 stripes and 50 stars at the official proportions, using a nested loop that staggers rows of six and five stars.

American Flag in Python Turtle: the finished drawing
import turtle
# Official proportions: the flag is 1.9 times as wide as it is high
HEIGHT = 300
WIDTH = HEIGHT * 1.9
STRIPE = HEIGHT / 13
UNION_WIDTH = HEIGHT * 0.76
UNION_HEIGHT = STRIPE * 7
# Star spacing and size, also from the official specification
GAP_X = HEIGHT * 0.063
GAP_Y = HEIGHT * 0.054
STAR_RADIUS = HEIGHT * 0.0616 / 2
RED = "#B22234"
BLUE = "#3C3B6E"
screen = turtle.Screen()
screen.title("American Flag")
screen.bgcolor("#dde6ee")
t = turtle.Turtle()
t.speed(10)
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()
def star(x, y, radius, color):
"""Draw a filled five-pointed star centred at (x, y)."""
edge = radius * 0.7265
t.penup()
t.goto(x, y + radius)
t.setheading(288)
t.pendown()
t.color(color)
t.begin_fill()
for point in range(5):
t.forward(edge)
t.left(72)
t.forward(edge)
t.right(144)
t.end_fill()
# 13 stripes, red and white, starting and ending with red
left = -WIDTH / 2
top = HEIGHT / 2
for stripe in range(13):
if stripe % 2 == 0:
color = RED
else:
color = "white"
rectangle(left, top - stripe * STRIPE, WIDTH, STRIPE, color)
# The blue union in the top-left corner
rectangle(left, top, UNION_WIDTH, UNION_HEIGHT, BLUE)
# 50 stars in 9 rows: rows of 6 and rows of 5 take turns
for row in range(9):
y = top - (row + 1) * GAP_Y
if row % 2 == 0:
columns = range(1, 12, 2)
else:
columns = range(2, 11, 2)
for column in columns:
star(left + column * GAP_X, y, STAR_RADIUS, "white")
t.hideturtle()
turtle.done()

How the code works

Sizes from the official flag

Every size is a fraction of the height, taken from the official specification: the flag is 1.9 times as wide as it is high, the blue union covers 7 stripes, and each star is 0.0616 of the height across.

HEIGHT = 300
WIDTH = HEIGHT * 1.9
STRIPE = HEIGHT / 13
UNION_WIDTH = HEIGHT * 0.76
UNION_HEIGHT = STRIPE * 7

# Star spacing and size, also from the official specification
GAP_X = HEIGHT * 0.063
GAP_Y = HEIGHT * 0.054
STAR_RADIUS = HEIGHT * 0.0616 / 2

A star from ten short lines

The turtle starts at the top point and turns left 72 degrees at each inner corner and right 144 degrees at each tip, five times over. Tracing the outline, instead of crossing lines, lets begin_fill() fill the whole star.

def star(x, y, radius, color):
    """Draw a filled five-pointed star centred at (x, y)."""
    edge = radius * 0.7265
    t.penup()
    t.goto(x, y + radius)
    t.setheading(288)
    t.pendown()
    t.color(color)
    t.begin_fill()
    for point in range(5):
        t.forward(edge)
        t.left(72)
        t.forward(edge)
        t.right(144)
    t.end_fill()

Thirteen stripes in a loop

stripe % 2 == 0 is true for stripes 0, 2, 4 and so on, so red and white take turns and the first and last stripes are red.

for stripe in range(13):
    if stripe % 2 == 0:
        color = RED
    else:
        color = "white"
    rectangle(left, top - stripe * STRIPE, WIDTH, STRIPE, color)

Nine rows of stars

Rows of six sit in the odd columns and rows of five in the even columns between them. The inner loop draws one row, and the outer loop moves down one gap at a time.

for row in range(9):
    y = top - (row + 1) * GAP_Y
    if row % 2 == 0:
        columns = range(1, 12, 2)
    else:
        columns = range(2, 11, 2)
    for column in columns:
        star(left + column * GAP_X, y, STAR_RADIUS, "white")

Make it yours

Change the code above and press Run. Some ideas:

  • Change t.speed(10) to t.speed(0) to see the finished flag at once.
  • Change "white" in the last star(...) call to "gold" for golden stars.
  • Change HEIGHT = 300 to HEIGHT = 200 for a smaller flag: every stripe, star and gap shrinks with it.

What it uses

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