This is an excerpt from the Turtle
module documentation from python.org
23.1. turtle —
Turtle graphics
23.1.1.
Introduction
Turtle graphics is a popular way for
introducing programming to kids. It was part of the original Logo programming
language developed by Wally Feurzig and Seymour Papert in 1966.
Imagine a robotic turtle starting at
(0, 0) in the x-y plane. After an import
turtle, give it the command turtle.forward(15), and it moves (on-screen!) 15 pixels in the direction it is
facing, drawing a line as it moves. Give it the command turtle.right(25), and it rotates in-place 25 degrees clockwise.
Turtle can draw intricate shapes
using programs that repeat simple moves.
Turtle motion
Move and draw
forward(distance) | fd(distance)
Parameter: distance – a number (integer or float)
Move the turtle forward by the specified distance, in the direction the turtle is headed.
backward(distance) | bk(distance) | back(distance)
Parameter: distance – a number (integer or float)
Move the turtle backward by distance,
opposite to the direction the turtle is headed.
Do not change the turtle’s heading.
right(angle) | rt(angle)
Parameter: angle – a number (integer or float)
Turn turtle right by angle units. (Units are in degrees, by default.)
left(angle) | lt(angle)
Parameter: angle – a number (integer or float)
Turn turtle left by angle units. (Units are in degrees, by default.)
goto(x, y) | setpos(x, y) | setposition(x, y)
Parameters: x – a number, y – a number
Move turtle to an absolute position. If the pen is down, draw line. Do not change the turtle’s orientation. [Note: this works for two numbers like this: goto(100, 200) and also for one tuple of two numbers like this: goto ((100, 200)).]
setx(x)
Parameter: x – a number (integer or float)
Set the turtle’s first coordinate to x, leave second coordinate unchanged.
sety(y)
Parameter: y – a number (integer or float)
Set the turtle’s second coordinate to y, leave first coordinate unchanged.
setheading(angle) | seth(angle)
Parameter: angle – a number (integer or float)
Set the orientation of the turtle to angle. (The angle is in degrees, by default.)
If you set the heading to 0, the turtle will point to the right (East).
If you set the heading to 90, the turtle will point up (North).
If you set the heading to 180, the turtle will point to the left (West).
If you set the heading to 270, the turtle will point down (South).
home()
There are no parameters.
Move turtle to the origin – coordinates (0,0) – and set its heading to its initial orientation (which is pointing to the right (East)).
circle(radius)
Parameter: radius – a number (integer or float)
Draw a circle with the given
radius. The center is radius units to
the left of the turtle.
(Remember that the turtle is always pointed in some direction (depending on
what has happened up to the point in the program). This direction is called the turtle’s
heading. You can change the turtle’s
heading using the setheading function. The center of the circle (or other polygon)
drawn by the circle function is a distance of radius from the left side of the
turtle – 90 degrees to the left of the turtle’s current heading.)
circle(radius, extent=None, steps=None)
You can also supply a second parameter: extent
If extent is not given, draw the entire circle. If extent is not a full circle, one endpoint of the arc is the current pen position. Draw the arc in counterclockwise direction if radius is positive, otherwise in clockwise direction. Finally the direction of the turtle is changed by the amount of extent.
You can also supply a third parameter: steps
As the circle is approximated by an
inscribed regular polygon, steps determines the number of steps
to use. If not given, it will be calculated automatically. May be used to draw
regular polygons.
dot(size = None, color)
Parameters: size, an integer >= 1 (if given); color – a colorstring or a numeric color tuple.
Draw a circular dot with diameter size, using color. If size is not given, the maximum of pensize+4 and 2*pensize is used.
stamp()
No parameters.
Stamp a copy of the turtle shape onto the canvas at the current turtle position. Return a stamp_id for that stamp, which can be used to delete it by calling clearstamp(stamp_id).
clearstamp(stampid)
Parameter: stampid – an integer, must be the return value of a previous call to stamp()
Delete stamp with given stampid.
clearstamps(n=None)
Parameter: n – an integer (or None)
Delete all or first/last n of turtle’s stamps. If n is None, delete all stamps, if n > 0 delete first n stamps, else if n < 0 delete last n stamps.
undo()
No parameter.
Undo the last turtle action.
speed(speed = None)
Parameter: speed – an integer in the range of 0 .. 10 or a speedstring
Set
the turtle’s speed to an integer value in the range 0..10.
If no argument is given, return current speed.
If
input is a number greater than 10 or smaller than 0.5, speed is set to 0. Speedstrings are mapped to speedvalues
as follows:
· “fastest”: 0
· “fast”: 10
· “normal”: 6
· “slow”: 3
· “slowest”: 1
Speeds from 1 to
10 enforce increasingly faster animation of line drawing and turtle turning.
Attention: speed
= 0 means that no animation takes place. forward/back
makes turtle jump and likewise left/right make the turtle turn instantly.
Tell Turtle’s state
position() | pos()
No parameter.
Return the turtle’s current location (x,y) (as a Vec2D vector).
towards(x, y=None)
Parameters: x – a number (x location of a point); y – a number (y location of a point)
Return the angle between the line
from turtle position to the point specified by (x,y).
This works for two numbers like this: towards(50,-50)
and also for a tuple of two numbers like this: towards((50,-50))
xcor()
No parameter.
Return the turtle’s x coordinate.
ycor()
No parameter.
Return the turtle’s y coordinate.
heading()
No parameter.
Return the turtle’s current heading
distance(x, y)
Parameters: x – a number (x location of a point); y – a number (y location of a point)
Return the distance from the turtle to the point (x,y). This works for two numbers like this: distance(100,100) or for a tuple of two numbers like this: distance((100,100))
Pen control
Drawing state
pendown() | pd() | down()
No parameter.
Put the pen down. There will be drawing when you move the turtle when the pen is down.
penup() | pu() | up()
No parameter.
Put the pen up. There won’t be any drawing when you move the turtle while the pen is up.
pensize(width) | width(width)
Parameter: width – a positive number
If you don’t supply a parameter, this function will return the current width of the pen. If you supply a parameter, this will set the line thickness to the number you supply.
pen()
We’ll look at how this works later in the course.
isdown()
No parameter.
Return True if pen is down, False if it’s up.
Color control
pencolor()
Parameter: either no parameter or a string representing a color or three numbers (inside parentheses) representing a color.
If you don’t supply a parameter, this function will return the current color of the pen. If you supply a parameter (either a string representing a color or three numbers representing a color), it will set the pen color to the color you provided.
fillcolor()
Parameter: either no parameter or a string representing a color or three numbers (inside parentheses) representing a color.
Parameter: either no parameter or a string representing a color or three numbers (inside parethenses) representing a color.
Return or set the fill color.
color()
Return or set both the pencolor and the fillcolor.
Parameters: either no
parameter, one string representing a color or three numbers (inside parentheses)
representing a color, two strings each representing a color, or two sets of
three numbers (each inside parentheses) representing a color.
If you don’t supply a parameter, this function will return the current color of
the pen and the current fill color.
If you supply one parameter (a color – either as a string or as a set of
three numbers in parentheses), it will set the fill color and the pen color to
the color that you supply.
if you supply two parameters (two colors either as
strings or a set of three numbers in parentheses), it will set the pen color to
the first color you supply and set the fill color to the second color that you
supply.
Filling
filling()
No parameters.
This function will return True if you are currently “filling” and it will return False if you are not.
begin_fill()
No parameters.
To be called just before drawing a shape to be filled.
end_fill()
No parameters.
To be called just after you have drawn the shape that you want to fill.
More drawing control
reset()
Delete the turtle’s drawings from the screen, re-center the turtle and set variables to the default values.
clear()
Delete the turtle’s drawings from the screen. Do not move turtle. State and position of the turtle as well as drawings of other turtles are not affected.
write(msg)
Parameter: msg – a text string (the message that you want to display)
Write the text – the string representation of msg – at the current turtle position aligned to the left in Arial font, size 8. The pen will not move.
You can supply more parameters like this:
write(arg,
move=False, align="left",
font=("Arial", 8,
"normal")
Parameters: arg – a text string (the
message that you want to display)
move – either True or False
depending on whether or not you want the turtle to move to the lower right
corner of the text displayed
align – either “left”, “center”,
or “right” depending on how you want the text aligned.
font – you supply the name of the font, the size of the font – a positive integer, and one of the three values “
Turtle state
Visibility
showturtle() | st()
No parameters.
Make the turtle visible.
hideturtle() | ht()
No parameters.
Hide the turtle. (Make it invisible) You will still see the drawing – you just won’t see the turtle.
isvisible()
No parameters.
Returns True if the turtle is currently visible; returns False if the turtle is currently hidden.
Appearance
shape(tshape)
Parameter: tshape – a string indicating the turtle shape. The built-in shapes are “arrow”, “turtle”, “circle”, “square”, “triangle”, and “classic”.
If you don’t provide a parameter, this function will return the current turtle shape. If you supply one of these parameters shown, it will set the turtle shape to the shape you supply.
Window control
bgcolor()
Parameter: a string representing a color or three numbers (in parentheses) representing a color.
Set or return background color of the TurtleScreen. If you don’t provide a parameter, this function will return the background color. If you do provide a parameter, it will set the parameter to the color you provide.
clear() | clearscreen()
No parameters.
Delete all drawing on the screen; delete the turtle. Reset all parameters. Position the screen back at (0,0).
reset() | resetscreen()
No parameters.
Resets the turtle to its initial state.
Settings and special methods
getshapes()
No parameter.
Return a list of names of all currently available turtle shapes.