Graphics with the turtle-class
Here we only bring up some of the most basic methods. See
the documentation for a more complete description.
The Turtle-world is located in the module
turtle, which must be imported.
Create
| Method | Function | Example |
| turtle.Turtle() |
Creates and returns a turtle placed at the origin. If it is the first turtle, the world is also created. |
t = turtle.Turtle() |
Move and Draw
| Method | Function | Example |
| forward(n)
fd(n)
|
Step n pixels forward.
|
|
| backward(n)
bk(n)
|
Step n pixels backwards.
|
|
| left(n)
lt(n)
|
Turn n degrees to the left.
|
|
| right(n)
rt(n)
|
Turn n degrees to the right.
|
|
| goto(x, y) |
Go to position (x,y). |
|
| setx(x) |
Move horizontally. |
|
| sety(y) |
Move vertically. |
|
setheading(n) seth(n) |
Sets the direction. 0 is right along the x-axis, 90 is up along the y-axis. |
t.seth(180) |
| circle(radius) |
Walk in a circle with a given radius. |
|
| dot(radius, col) |
Draw a point with given radius and color. |
t.dot(10, 'blue') |
| speed(n) |
Sets the speed.
- 0 : fastest, no animation
- 1 : slow
- 5
- 9 : fast
|
|
Get information about the turtle
| Method | Function |
| towards(x,y) |
Returns the direction towards the point (x,y).
|
| xcor() |
Returns the x-coordinate.
|
| ycor() |
Returns the y-coordinate.
|
| heading() |
Returns the direction. 0 is to the right along the x-axis, 90 is up along the y-axis.
|
| distance(x, y) |
Returns the distance to the point (x,y).
|
| distance(t) |
Returns the distance to the turtle t.
|
Control of the pen, color, and filling
| Method | Function |
pendown() pd() |
Will draw.
|
penup() pu() |
Will not draw.
|
| pensize() |
Returns the size of the pen.
|
| pensize(n) |
Sets the size of the pen. |
| isdown() |
True if the pen is down, False otherwise.
|
| pencolor(colorstring) |
Sets the color of the pen. Example: 'blue' or '#32c18f'. |
| fillcolor(colorstring) |
Sets the fill color.
|
| begin_fill() |
Starts to fill figures. |
| end_fill() |
Stops filling figures.
|
| clear() |
Erase everything the turtle has drawn. |
| write(text, font=(name,size,type)) |
Writes a text. Example:
t.write('hi', font=('Courier',24,'bold'))
|
| hideturtle()
ht() |
Make the turtle invisible. |
| showturtle()
st() |
Make the turtle visible.
|
There are methods for changing the size, shape, and color of the turtle. There are also methods for changing the window (size, color, coordinate system, ...).
See
the documentation!