5. Turtle square progressions
5.1. Sequencing: steps to draw a square

t.seth(0)
t.goto(20, 30)
.t.pu()
, and pendown, t.pd()
, are used either side of t.goto(20, 30)
to avoid line drawing when repositioning the turtle.t.fd(50)
.t.lt(90)
.t.fd(50)
and t.lt(90)
are then repeated 3 more times for the other three sides.import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("Grsquareid")
s.setup(width=800, height=600, startx=0, starty=0)
t = turtle.Turtle()
t.speed(5)
t.pu()
t.goto(20, 30)
t.pd()
t.seth(0)
t.fd(50)
t.lt(90)
t.fd(50)
t.lt(90)
t.fd(50)
t.lt(90)
t.fd(50)
t.lt(90)
s.exitonclick()
Tasks
From the code above, list the 8 lines that do the actual drawing.
From the 8 lines, list the simplest amount of code that is repeated.
Using sequencing only, draw a square of side length 500 at (-250, -250).
From the code above, list the lines that do the actual drawing.
t.fd(50)
t.lt(90)
t.fd(50)
t.lt(90)
t.fd(50)
t.lt(90)
t.fd(50)
t.lt(90)
From the 8 lines, list the simplest amount of code that is repeated.
t.fd(50)
t.lt(90)
Using sequencing only, draw a square of side length 500 at (-250, -250).
import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("square")
s.setup(width=800, height=600, startx=0, starty=0)
t = turtle.Turtle()
t.speed(5)
t.pu()
t.goto(-250, -250)
t.pd()
t.seth(0)
t.fd(500)
t.lt(90)
t.fd(500)
t.lt(90)
t.fd(500)
t.lt(90)
t.fd(500)
t.lt(90)
s.exitonclick()
5.2. Iteration: using a for-loop to draw a square
t.fd(50)
and t.lt(90)
are placed in a for-loop with 4 repeats for the 4 sides.import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("square")
s.setup(width=800, height=600, startx=0, starty=0)
t = turtle.Turtle()
t.speed(5)
t.pu()
t.goto(20, 30)
t.pd()
t.seth(0)
for _ in range(4):
t.fd(50)
t.lt(90)
s.exitonclick()
Tasks
1. Identify the lines of code that replaced the 8 steps: t.fd(50); t.lt(90); t.fd(50); t.lt(90); t.fd(50); t.lt(90); t.fd(50); t.lt(90)
Identify the lines of code that replaced the 8 steps.
for _ in range(4):
t.fd(50)
t.lt(90)
5.3. Definitions: using a def block to draw a square
- square(t, length=50, start_pos=(0, 0), start_h=0)
- t - the turtle object to draw the squarelength - side length; default 50start_pos - start position; default (0, 0)start_h - start heading; default 0
square(t)
draws a default square.square(t, length=50, start_pos=(20, 30))
draws a square of length 50 at (x=20, y=30).square(t, length=250, start_pos=(-300, -200), start_h=20)
draws a square of length 250 at (x=-300, y=-200) angled 20 degrees.import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("square")
s.setup(width=800, height=600, startx=0, starty=0)
t = turtle.Turtle()
t.speed(5)
def square(t, length=50, start_pos=(0, 0), start_h=0):
t.pu()
t.goto(start_pos)
t.pd()
t.seth(start_h)
for _ in range(4):
t.fd(length)
t.lt(90)
square(t)
square(t, length=50, start_pos=(20, 30))
square(t, length=250, start_pos=(-300, -200), start_h=20)
s.exitonclick()
Tasks
Modify
square(t, length=50, start_pos=(0, 0), start_h=0)
to draw a square of length 200 at (-300, -100).Modify
square(t, length=50, start_pos=(0, 0), start_h=0)
to draw a square of length 30 at (70, 100) with heading 30 degrees.
Modify square(t, length=50, start_pos=(0, 0))
to draw a square of length 200 at (-300, -100).
square(t, length=200, start_pos=(-300, -100))
Modify square(t, length=50, start_pos=(0, 0), start_h=0)
to draw a square of length 30 at (70, 100) with heading 30 degrees.
square(t, length=30, start_pos=(70, 100), start_h=30)
5.4. Adding pen colour and fill colour parameters

- square(t, length=50, start_pos=(0, 0), start_h=0, penw=1, penc='black', fillc=None)
- t - the turtle object to draw the squarelength - side length; default 50start_pos - start position; default (0, 0)start_h - start heading; default 0penw - pensize; default 1penc - pencolor; default “black”fillc - fillcolor; default None
square(t, length=250, start_pos=(-100, -150), start_h=0, penw=2, penc="black", fillc="light green")
draws a square of length 250 at (x=-100, y=-150) with a black pencolor, a light green fillcolor, with a pensize of 2.import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("square")
s.setup(width=800, height=600, startx=0, starty=0)
t = turtle.Turtle()
t.speed(0)
def square(t, length=50, start_pos=(0, 0), start_h=0, penw=1, penc="black", fillc=None):
t.pu()
t.goto(start_pos)
t.pd()
t.seth(start_h)
t.pensize(penw)
t.pencolor(penc)
if fillc is not None:
t.fillcolor(fillc)
t.begin_fill()
for _ in range(4):
t.fd(length)
t.lt(90)
if fillc is not None:
t.end_fill()
square(t, length=250, start_pos=(-100, -150), start_h=0, penw=2, penc="black", fillc="light green")
s.exitonclick()
Tasks
Modify
square(t, length=50, start_pos=(0, 0), start_h=0, penw=1, penc="black", fillc=None)
to draw a square of length 200 at (-300, -100) with red outline of thickness 5.Modify
square(t, length=50, start_pos=(0, 0), start_h=0, penw=1, penc="black", fillc=None)
to draw a square of length 30 at (70, 100) with green outline of thickness 3 and a yellow fill.
Modify square(t, length=50, start_pos=(0, 0), start_h=0, penw=1, penc="black", fillc=None)
to draw a square of length 200 at (-300, -100) with red outline of thickness 5.
square(t, length=200, start_pos=(-300, -100), start_h=0, penw=5, penc="red", fillc=None)
Modify square(t, length=50, start_pos=(0, 0), start_h=0, penw=1, penc="black", fillc=None)
to draw a square of length 30 at (70, 100) with green outline of thickness 3 and a yellow fill.
square(t, length=30, start_pos=(70, 100), start_h=0, penw=3, penc="green", fillc="yellow")
5.5. Practice Questions
Exercises
Using sequencing only, draw a square of side length 500 at (-250, -250).
Using a repeat loop (without a function), draw a square of side length 50 at (-25, -25).
Use the definition provided above to draw a square of length 400 at (x=-200, y=-200) with a purple pencolor, a bisque fillcolor, and a pensize of 10.