Turtle Graphics: Where code comes to life. Control the turtle, draw shapes, and master loops!
Python Turtle Reference Sheet (Seconds)
Moving
Driving instructions.
-
forward(100)Move forward 100 steps.
Shortcut:fd(100) -
backward(50)Move back 50 steps.
Shortcut:bk(50) -
left(90)Turn left 90 degrees.
Shortcut:lt(90) -
right(45)Turn right 45 degrees.
Shortcut:rt(45)
Pen & Looks
Pen Control
penup() # Don't draw
goto(50, 100) # Teleport
pendown() # Draw again
goto(50, 100) # Teleport
pendown() # Draw again
Styling
pensize(5) # Thickness
color("red") # Line color
speed(5) # 1=slow, 10=fast
color("red") # Line color
speed(5) # 1=slow, 10=fast
Shapes
circle(50) # Radius 50
dot(20) # Filled dot
dot(20) # Filled dot
Loops & Angles
Don't repeat yourself! Use a for loop.
Drawing a Square
for i in range(4):
forward(100)
right(90)
forward(100)
right(90)
Which Angle?
Turtle turns the RED angle (External).
The Angle Formula:
Pentagon: 360 / 5 = 72
360 / Sides = Angle
Triangle: 360 / 3 = 120Pentagon: 360 / 5 = 72
Functions
Level UpTeach the turtle a new word (command).
1. Define It
def draw_square():
for i in range(4):
forward(50)
right(90)
for i in range(4):
forward(50)
right(90)
2. Call It
draw_square() # Draws one
penup()
forward(100)
pendown()
draw_square() # Draws another
penup()
forward(100)
pendown()
draw_square() # Draws another
Filling
Colour inside the lines.
color("black", "yellow")
# (Line Colour, Fill Colour)
begin_fill() # Start
for i in range(4):
forward(100)
right(90)
end_fill() # Finish
# (Line Colour, Fill Colour)
begin_fill() # Start
for i in range(4):
forward(100)
right(90)
end_fill() # Finish
You must close the shape for the fill to look right!
Turtle Debugging
-
Where am I facing?
Before moving, check your direction.
Usesetheading(0)to face East (Right) if lost. -
Turtle won't move?
Check your spelling!
fowardvsforward. -
Weird Shape?
Use External Angles (the turn), not the angle inside.
Triangle turn is 120 (not 60). -
No Line Appearing?
Did you use
penup()? Remember topendown()to start drawing again. -
Window closes instantly?
Add
done()at the very end of your file.
Dashed Line
Pen Up/Down practice.
for i in range(10):
forward(10) # Draw
penup()
forward(10) # Skip
pendown()
forward(10) # Draw
penup()
forward(10) # Skip
pendown()
Staircase
Alternating turns.
for i in range(5):
forward(30)
left(90)
forward(30)
right(90)
forward(30)
left(90)
forward(30)
right(90)
Gold Star
Sharp angles.
color("gold")
begin_fill()
for i in range(5):
forward(100)
right(144)
end_fill()
begin_fill()
for i in range(5):
forward(100)
right(144)
end_fill()
Tip: Experiment! Change numbers and see what happens.