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 / forwardfd(100)bk / back / backwardbk(100)lt / left and rt / rightlt(90)circlecircle(100) circle(-50, 90)speedspeed(3)seth / setheadingsetheading(180)setpos / setposition / goto, setx, setysetx 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 / penuppenup()pd / pendownpendown()pencolorpencolor("Blue") pencolor(0,0.3,0.7)pensizepensize(3)begin_fill, end_fillbegin_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()
fillcolorfillcolor("Forest Green") fillcolor(0,0.4,0)colorcolor("Peach Puff") color("Misty Rose", "Lavender")writedrawText 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, ycorpos 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()headingheading returns the current direction that the turtle is facing, in degrees. 0 is due east, and 90 is north.h = heading()towardstowards 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)distancedistance 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)titletitle("Drawing")bgcolorbgcolor("Light Sky Blue")resetreset()turtleBeads commands
teleportsetpos 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)leapleap(30) leap(-25)hopleap, 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)drawCirclecircle 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)drawEllipsedrawCircle, the turtle ends up where it started.drawEllipse(100, 0.5) drawEllipse(50, 2)drawDotdrawCircle, but cannot have separate pen and fill colors.drawDot(15)drawSquaredrawSquare(50)drawRectangledrawRectangle(50, 100)drawPolygondrawPolygon(60, 3) drawPolygon(30, 12)fontsizefontsize(32)alignalign("left")drawTextdrawText("Hello")setupTurtlesetupTurtle()noTrace, doTrace, showPicturenoTrace 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)