Python Turtle Graphics
Turtle graphics are a classic way to learn programming with pictures. A small arrow, the turtle, sits in the middle of a window. You tell it to move forward, turn and change colour, and it draws a line wherever it goes. Loops, functions and variables suddenly produce stars, spirals and houses. In our editor the drawing appears in a graphics window over the code, and turtle programs from books and courses run unchanged.
Moving and turning
import turtle
t = turtle.Turtle()
t.forward(100) # move 100 steps
t.left(90) # turn left 90 degrees
t.forward(100)
t.right(45)
t.backward(50)
print(t.position(), t.heading())
turtle.done()(64.64,64.64) 45.0
The window's centre is the point (0, 0), with x going right and y going up. The turtle starts there facing right, which is heading 0; left increases the heading and right decreases it. position reports where the turtle is. turtle.done() shows the finished picture; it is the last line of most turtle programs.
Pen, colours and filling
penup lifts the pen so the turtle moves without drawing, and pendown puts it back. color(pen, fill) sets both colours, and shapes drawn between begin_fill and end_fill are filled:
from turtle import *
speed(0)
pensize(3)
color("navy", "gold")
begin_fill()
for _ in range(5): # a star: five lines, turning 144 degrees
forward(150)
right(144)
end_fill()
penup()
goto(0, -60)
write("Star!", align="center", font=("Arial", 16, "bold"))
hideturtle()
done()from turtle import * lets you call the commands without the turtle. prefix; they control one default turtle. Colours can be names such as "red" and "skyblue", or (r, g, b) values from 0 to 1. speed(0) draws as fast as possible; speeds from 1 (slow) to 10 (fast) animate the drawing.
Patterns with loops and functions
import turtle
screen = turtle.Screen()
screen.bgcolor("black")
pen = turtle.Turtle()
pen.speed(0)
colours = ["red", "orange", "yellow", "green", "cyan", "magenta"]
def square(size):
for _ in range(4):
pen.forward(size)
pen.left(90)
for i in range(36):
pen.color(colours[i % len(colours)])
square(120)
pen.left(10)
pen.hideturtle()
turtle.done()Turning a little after each square, 36 times, gives a full circle of squares. circle(radius) draws a circle, and dot(size) a filled dot.
Several turtles, speed and keys
Each turtle.Turtle() is a separate turtle with its own position and colour, so several can draw at once. Screen().onkey(function, "Up") and listen() call your functions when keys are pressed: click the graphics window first, then press the keys. onclick(function) calls a function with the x and y of a mouse click, ondrag lets you drag a turtle around, and ontimer runs a function after a delay, which is how animations and games repeat. This is Python's own turtle module, so undo, stamp, shapes and every other command work as in any book.
Practice exercises
- Draw a square, a triangle and a hexagon with loops (the turn is 360 divided by the number of sides).
- Draw a filled red circle with a black outline.
- Write a function
polygon(sides, size)and draw polygons with 3 to 8 sides in different colours. - Make a turtle that moves forward 10 steps every time you press the Up key.