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
for example, you could get help for the turtle
penup command by running:
You can download the latest version of
turtleBeads.py at this link:
To draw with turtle graphics, you will need to import the built-in
module. To do this, add a line of code at the top of your file that looks
from turtle import *
The '*' here means "everything," and this style of import makes the functions
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.
setychange 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.
begin_fillwhen the turtle is in position to draw a shape, and
end_fillonce the shape is complete. The region enclosed by the turtle between these two calls will be filled with the current fill color when the
begin_fill() fd(100) lt(90) fd(100) end_fill()
color("Misty Rose", "Lavender")
drawTextinstead. 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", True, "left", ("Arial", 12, "bold))
positionreturn the current position of the turtle as a pair of x and y coordinates.
ycorreturn the individual x- and y-coordinates as numbers if you need to do math with them.
x = xcor()
headingreturns the current direction that the turtle is facing, in degrees. 0 is due east, and 90 is north.
h = heading()
towardsreturns 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)
distancereturns the distance between the current position and the given x/y position.
dist = distance(50, 50) angle = towards(50, 50) seth(angle) fd(distance)
bgcolor("Light Sky Blue")
setposto move to the given x/y position, and then extends the pen. Use to move the turtle to specific coordinates without drawing.
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.
circlefunction, but centers it on the current turtle position, and puts the turtle back where it started when it's done.
drawCircle, the turtle ends up where it started.
drawCircle, but cannot have separate pen and fill colors.
noTraceto turn off animation completely, and
doTraceto turn it back on. When animation is turned off, use
showPictureto 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)