Turtle Reference
The turtle
built-in module is used for drawing graphics in Python.
turtleBeads.py
is a custom module that adds a few commands to easily draw
certain shapes centered on the current turtle position, like circles, ellipses,
and polygons. This page covers the core commands from both modules and
briefly describes how to use them. If you need more details, remember that you
can get help for any Python function by calling the built-in help
function,
for example, you could get help for the turtle penup
command by running:
help(penup)
You can download the latest version of turtleBeads.py
at this link:
Getting Started
To draw with turtle graphics, you will need to import the built-in turtle
module. To do this, add a line of code at the top of your file that looks
like this:
from turtle import *
The '*' here means "everything," and this style of import makes the functions
in the turtle
module directly available.
If you also want to use turtleBeads
functions, you should add:
from turtleBeads import *
to the top of your file as well. Make sure the turtleBeads.py
file is in the
same directory as the file that you are writing your code in, otherwise Python
won't be able to import it and you will get an error.
Reference
turtle
commands
fd
/ forward
fd(100)
bk
/ back
/ backward
bk(100)
lt
/ left
and rt
/ right
lt(90)
circle
circle(100)
circle(-50, 90)
speed
speed(3)
seth
/ setheading
setheading(180)
setpos
/ setposition
/ goto
, setx
, sety
setx
and sety
change either the x or y coordinate without affecting the other. The turtle will still draw while moving unless the pen is retracted. (0, 0) is in the center of the window, +x is to the East, and +y is to the North.setpos(100, 50)
sety(0)
pu
/ penup
penup()
pd
/ pendown
pendown()
pencolor
pencolor("Blue")
pencolor(0,0.3,0.7)
pensize
pensize(3)
begin_fill
, end_fill
begin_fill
when the turtle is in position to draw a shape, and end_fill
once the shape is complete. The region enclosed by the turtle between these two calls will be filled with the current fill color when the end_fill
happens.begin_fill()
fd(100)
lt(90)
fd(100)
end_fill()
fillcolor
fillcolor("Forest Green")
fillcolor(0,0.4,0)
color
color("Peach Puff")
color("Misty Rose", "Lavender")
write
drawText
instead. Writes text to the screen, North of the current turtle position (cannot be rotated). Accepts optional arguments to specify whether to move the turtle, how to align the text, and what font to use.write("Test")
write("Test", True, "left", ("Arial", 12, "bold))
pos
/ position
, xcor
, ycor
pos
and position
return the current position of the turtle as a pair of x and y coordinates. xcor
and ycor
return the individual x- and y-coordinates as numbers if you need to do math with them.print(position())
x = xcor()
heading
heading
returns the current direction that the turtle is facing, in degrees. 0 is due east, and 90 is north.h = heading()
towards
towards
returns the direction that the turtle would have to point in (for example by using seth
) in order to move towards a specific position.t = towards(30, 40)
seth(t)
distance
distance
returns the distance between the current position and the given x/y position.dist = distance(50, 50)
angle = towards(50, 50)
seth(angle)
fd(distance)
title
title("Drawing")
bgcolor
bgcolor("Light Sky Blue")
reset
reset()
turtleBeads
commands
teleport
setpos
to move to the given x/y position, and then extends the pen. Use to move the turtle to specific coordinates without drawing.teleport(50, -20)
leap
leap(30)
leap(-25)
hop
leap
, but moves sideways (positive to the left or negative to the right) without changing the turtle's orientation. Like leap
, it does not draw while moving.hop(15)
hop(-5)
drawCircle
circle
function, but centers it on the current turtle position, and puts the turtle back where it started when it's done. The argument specifies the radius of the circle.drawCircle(50)
drawEllipse
drawCircle
, the turtle ends up where it started.drawEllipse(100, 0.5)
drawEllipse(50, 2)
drawDot
drawCircle
, but cannot have separate pen and fill colors.drawDot(15)
drawSquare
drawSquare(50)
drawRectangle
drawRectangle(50, 100)
drawPolygon
drawPolygon(60, 3)
drawPolygon(30, 12)
fontsize
fontsize(32)
align
align("left")
drawText
drawText("Hello")
setupTurtle
setupTurtle()
noTrace
, doTrace
, showPicture
noTrace
to turn off animation completely, and doTrace
to turn it back on. When animation is turned off, use showPicture
to update the display. This can be even faster than speed 0 for complicated pictures.speed(3)
circle(110)
noTrace()
circle(100)
showPicture()
doTrace()
circle(90)