Below is a comprehensive documentation of the turtle
module, starting from the basics and gradually covering more advanced features. This will help you create fun and interactive graphics using the turtle module in Python.
Table of Contents:
- Basic Setup and Commands
- Turtle Movement and Position
- Drawing Shapes
- Changing Turtle Appearance
- Controlling Pen and Filling
- Advanced Turtle Commands
- Keyboard and Mouse Events
- Animation and Control
- Saving and Loading Drawings
- Examples and Projects
1.Basic Setup and Commands:
- Import the
turtle
module:
import turtle
- Create a
turtle
object:
t = turtle.Turtle()
- Start drawing:
t.forward(100) # Move the turtle forward by 100 units
t.right(90) # Turn the turtle right by 90 degrees
t.backward(50) # Move the turtle backward by 50 units
t.left(45) # Turn the turtle left by 45 degrees
2. Turtle Movement and Position:
-
forward(distance)
: Moves the turtle forward by the given distance. -
backward(distance)
: Moves the turtle backward by the given distance. -
right(angle)
: Turns the turtle right by the given angle in degrees.left(angle)
: Turns the turtle left by the given angle in degrees. -
goto(x, y)
: Moves the turtle to the specified coordinates (x, y). -
setx(x)
: Moves the turtle to the specified x-coordinate. -
sety(y)
: Moves the turtle to the specified y-coordinate. -
setheading(angle)
: Sets the turtle's heading (orientation) to the specified angle in degrees. -
home()
: Moves the turtle to the origin (0, 0) and resets the turtle's heading.
3. Drawing Shapes
-
circle(radius)
: Draws a circle with the specified radius. -
dot(size=None, color=None)
: Draws a dot at the turtle's current position. -
stamp()
: Stamps an impression of the turtle shape onto the screen. -
clearstamp(stamp_id)
: Removes the stamp with the specified stamp_id. -
clearstamps(n=None)
: Removes the last n (or all) stamps. -
begin_fill()
: Begins filling a shape. -
end_fill()
: Stops filling and completes the shape. -
filling()
: Returns True if filling is in progress, False otherwise.
4. Changing Turtle Appearance:
-
shape(name)
: Changes the turtle's shape. Available shapes: "arrow", "turtle", "circle", "square", "triangle", "classic". -
color(pen_color, fill_color=None)
: Sets the pen and fill colors. Use color names, RGB values, or color strings like "#RRGGBB". -
pensize(width)
: Sets the width of the turtle's pen. -
speed(speed)
: Sets the turtle's speed (0-10). 0 is the fastest. -
hideturtle()
: Makes the turtle invisible. -
showturtle()
: Makes the turtle visible.
5. Controlling Pen and Filling:
-
penup()
: Lifts the turtle's pen off the screen (stops drawing). -
pendown()
: Puts the turtle's pen on the screen (starts drawing). -
isdown()
: Returns True if the pen is down, False otherwise. -
pensize(width)
: Sets the width of the turtle's pen. -
pencolor(color)
: Sets the color of the turtle's pen. -
fillcolor(color)
: Sets the color for filling shapes.
6. Advanced Turtle Commands:
-
speed(speed)
: Sets the turtle's speed (0-10). 0 is the fastest. -
delay(delay=None)
: Sets or returns the drawing delay in milliseconds. -
begin_poly()
: Starts the definition of a new custom shape. -
end_poly()
: Ends the definition of a custom shape and returns the resulting polygon. -
get_poly() or get_polygon()
: Returns the current custom shape as a polygon.
7. Keyboard and Mouse Events:
-
onkeypress(fun, key=None)
: Calls the specified function when a key is pressed. -
onkeyrelease(fun, key=None)
: Calls the specified function when a key is released. -
onclick(fun, btn=1, add=None)
: Calls the specified function when a mouse button is clicked. -
onscreenclick(fun, btn=1, add=None)
: Calls the specified function when the screen is clicked.
8. Animation and Control:
-
tracer(n=None, delay=None)
: Turns turtle animation on/off and sets the update delay. -
update()
: Redraws the screen if tracer() is turned off.
9. Saving and Loading Drawings:
-
getscreen()
: Returns the turtle's screen object. -
screensize(canvwidth=None, canvheight=None, bg=None)
: Sets the size of the turtle's canvas. -
getcanvas()
: Returns the turtle's canvas object. -
clear()
: Clears the turtle's drawings from the screen. -
reset()
: Clears the screen and resets turtle settings. -
undo()
: Undoes the last turtle action.
10. Examples and Projects:
- Draw a square:
for _ in range(4):
t.forward(100)
t.right(90)
- Draw a colorful spiral:
colors = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]
for i in range(360):
t.pencolor(colors[i % len(colors)])
t.width(i / 100 + 1)
t.forward(i)
t.left(59)
These are just a few examples of what you can do with the turtle
module. The possibilities are endless, and you can explore more advanced features and combine them to create even more interesting and interactive graphics.
Remember to experiment, have fun, and don't hesitate to refer to the official Python documentation for more detailed information on specific turtle commands (https://docs.python.org/3/library/turtle.html)
Top comments (0)