Turtle
Python Turtle is based on the Logo Programming Language that has been used as a popular way of teaching children programming for many years.
HEALTH WARNING: Do NOT save your python file as turtle.py – this will break it!
Getting started
You will need to import the Turtle library into the script.
from turtle import *
Basic Controls
Command | Example | Scratch Equiv. | Description |
---|---|---|---|
pendown() |
pendown() | ![]() | Puts the pen down so when its moved it WILL draw a line. |
penup() |
penup() | ![]() | Lifts the pen so it can be moved without drawing. |
pencolor() |
pencolor("red") | ![]() | you can set the color of the pen and its fill using any of the Tkinter Colors. For more information about colour and fills see below. |
pensize() |
pensize(5) | ![]() | The width of the pen. |
hideturtle |
hideturtle() | Worth doing for complex shapes where you want to speed up the drawing. | |
showturtle |
showturtle() | Shows the turtle if it has been hidden. Also note that shape(“Turtle”) will change the pointer – you can pick from: arrow, turtle, circle, square, triangle, classic. | |
speed() |
speed(0) | Makes the turtle move faster, which is good for complex shapes. Give a number between 0 and 10 where 6 is the standard speed. 0 is no animation, 1 is slowest and 10 is fastest. | |
clear() |
clear() | Delete the turtle’s drawings from the screen. Do not move turtle. Use reset() if you want to move the turtle as well. |
Turtle Motion
The turtle can be made to draw using the following simple commands. It starts from 0,0, which is the centre of the window (aka origin or home position).
Command | Example | Scratch Equiv. | Description |
---|---|---|---|
forward() |
forward(100) | ![]() | Moves forward the given number of pixels. |
backward() |
backward(100) | ![]() | Scratch does not have a backwards step so we need to use negative but Turtle has backward. |
left() |
left(45) | ![]() | Left turns the pointer clockwise the given number of degrees. |
right() |
right(90) | ![]() | Right turns the pointer anti-clockwise the given number of degrees. |
goto() |
goto(160,50) | ![]() | Goes to the position indicated by the x and y co-ordinate. |
home() |
home() | ![]() | Puts the turtle back to the middle of the window |
circle() |
circle(100,360,6) | Draws a circle with: a radius (e.g. 100); for an angle (e.g. 360 is full circle and 180 would be half); in the number of steps given (e.g. 8 for hexagon). | |
dot() |
dot() | Draw a dot of size 1 unless you provide a bigger size e.g dot(10). |
Advanced Colors
As shown above you can set the line color of the pen. You can also use HTML colours e.g. “#33BB55” or RGB colours represented by a tuple e.g. color(100,73,45). If you are using RGB you need to ensure that the colormode() is set to 255 e.g.
colormode(255)
color(200,34,13)
Filling Shapes
By default the turtle can draw a line or a circle with a transparent fill. To fill an object you should first set the fillcolor.
fillcolor("yellow")
The include begin_fill and end_fill statements to tell Python when to start filling a shape and when to stop. This example will create a circle with a black outline and red fill.
color("black", "red") begin_fill() circle(80) end_fill()
This approach will also work with multiple lines enclosing a shape e.g. a square with a blue outline and a yellow fill.
color("blue", "yellow") begin_fill() for x in range (0,4)
forward(100)
right(90) end_fill()
Canvas Settings
You might want to change the size or background color of the window that opens.
setup(1000,400)
bgcolor("black")
Random Colours
One method for picking involves making a choice from a predetermined list of colours:
from random import choices colors = ["red", "blue", "green"] color(choices(colors))
Alternativly, you could make a random color completly:
from random import randint randomColor = (randint(0,255),randint(0,255),randint(0,255)) color(randomColor)
Headings
- heading() – returns the current direction of the turtle based on 0 being East.
- towards() – gets the bearing to the coordinates given..
Fun Equations
Calculate the XY coordinates of a circle given its radius r, origin (cx,cy) and current angle (a). Place this in a forloop from 0 to 360 to draw a circle. x = cx + r * cos(a) y = cy + r * sin(a)
Advanced Usage
Once the library has been imported it is possible to either use the functions immediatly e.g. forward() but you can create an instance of Turtle and use it e.g.
terry = Turtle() terry.forward()
This approach allows for multiple drawing turtles at the same time!