turtle
turtle
: Fun with Graphics
Section titled “turtle: Fun with Graphics”The turtle
module provides a simple and intuitive way to draw graphics. Imagine a small robot turtle that you can command to move around a canvas, leaving a trail behind it. You can change its direction, color, and whether it draws or not. It’s a fantastic way to learn programming concepts by creating visual art.
To get started, you just need to import the module:
import turtle
Note: You need to download the turtle.py file to the same location that you run your program…
Contents
Section titled “Contents”Turtle Motion
Section titled “Turtle Motion”These commands control how the turtle moves around the screen.
forward(distance)
or fd(distance)
Section titled “forward(distance) or fd(distance)”Moves the turtle forward by the specified distance
in its current direction.
# Move forward 100 stepsturtle.forward(100)
backward(distance)
or back(distance)
or bk(distance)
Section titled “backward(distance) or back(distance) or bk(distance)”Moves the turtle backward by the specified distance
, opposite to its current direction.
# Move back 50 stepsturtle.backward(50)
right(angle)
or rt(angle)
Section titled “right(angle) or rt(angle)”Turns the turtle to its right by the specified angle
in degrees.
# Turn right by 90 degreesturtle.right(90)
left(angle)
or lt(angle)
Section titled “left(angle) or lt(angle)”Turns the turtle to its left by the specified angle
in degrees.
# Turn left by 45 degreesturtle.left(45)
goto(x, y)
or setpos(x, y)
or setposition(x, y)
Section titled “goto(x, y) or setpos(x, y) or setposition(x, y)”Moves the turtle to an absolute position on the screen. The center of the screen is (0, 0)
.
# Go directly to the top-right areaturtle.goto(100, 50)
setx(x)
Section titled “setx(x)”Moves the turtle to the specified x
coordinate, keeping its current y
coordinate.
sety(y)
Section titled “sety(y)”Moves the turtle to the specified y
coordinate, keeping its current x
coordinate.
setheading(angle)
or seth(angle)
Section titled “setheading(angle) or seth(angle)”Sets the turtle’s orientation to an absolute angle
.
0
degrees points to the East (right).90
degrees points to the North (up).180
degrees points to the West (left).270
degrees points to the South (down).
# Make the turtle face upwardsturtle.setheading(90)
circle(radius, extent=360)
Section titled “circle(radius, extent=360)”Draws a circle or an arc. radius
determines the circle’s size. extent
(in degrees) determines how much of the circle to draw.
# Draw a full circle with a radius of 50turtle.circle(50)
# Draw a semicircleturtle.circle(50, 180)
home()
Section titled “home()”Moves the turtle to the center of the screen (0, 0)
and sets its heading to 0
degrees (East).
Pen Control
Section titled “Pen Control”These commands control the turtle’s pen.
penup()
or pu()
or up()
Section titled “penup() or pu() or up()”Lifts the pen up. The turtle will move without drawing.
pendown()
or pd()
or down()
Section titled “pendown() or pd() or down()”Puts the pen down. The turtle will draw as it moves.
pensize(width)
or width(width)
Section titled “pensize(width) or width(width)”Sets the thickness of the line drawn by the turtle. A width
of 1 is the default. The maximum is 5.
# Make the line 3 pixels thickturtle.pensize(3)
isdown()
Section titled “isdown()”Returns True
if the pen is down (drawing), and False
if it’s up.
Turtle State
Section titled “Turtle State”These commands manage the turtle’s appearance and position.
hideturtle()
or ht()
Section titled “hideturtle() or ht()”Makes the turtle invisible. This is useful when you want to see only the drawing.
showturtle()
or st()
Section titled “showturtle() or st()”Makes the turtle visible again.
isvisible()
Section titled “isvisible()”Returns True
if the turtle is currently visible.
shape(name)
Section titled “shape(name)”Changes the appearance of the turtle. Available shapes are "classic"
(the default) and "turtle"
.
# Change the turtle's lookturtle.shape("turtle")
speed(speed)
Section titled “speed(speed)”Sets the turtle’s animation speed. speed
can be:
- A number from 1 (slowest) to 10 (fast).
0
(fastest) means no animation; the drawing appears instantly.- A string:
'slowest'
,'slow'
,'normal'
,'fast'
,'fastest'
.
# Set a medium speedturtle.speed(6)
# Make the drawing happen instantlyturtle.speed(0)
position()
or pos()
Section titled “position() or pos()”Returns the turtle’s current (x, y)
coordinates.
xcor()
Section titled “xcor()”Returns the turtle’s current x
coordinate.
ycor()
Section titled “ycor()”Returns the turtle’s current y
coordinate.
heading()
Section titled “heading()”Returns the turtle’s current heading in degrees.
distance(x, y)
Section titled “distance(x, y)”Returns the distance from the turtle to the point (x, y)
.
towards(x, y)
Section titled “towards(x, y)”Returns the angle (in degrees) from the turtle’s current position to the point (x, y)
. Useful with setheading()
.
# Make the turtle face the top-left cornerangle_to_corner = turtle.towards(-150, 80)turtle.setheading(angle_to_corner)
Screen Control
Section titled “Screen Control”clear()
Section titled “clear()”Erases all drawings from the screen but leaves the turtle at its current position and state.
reset()
Section titled “reset()”This is a complete reset. It clears the screen, moves the turtle back to the center (0, 0)
, and resets all its settings (color, pensize, etc.) to their defaults.
Color Control
Section titled “Color Control”pencolor(*color)
Section titled “pencolor(*color)”Sets the color of the line the turtle draws. You can specify a color in several ways:
- As a color name string:
"red"
,"blue"
,"green"
, etc. - As an RGB tuple of floats from 0.0 to 1.0:
(1.0, 0.5, 0.0)
.
# Set pen color by nameturtle.pencolor("blue")
# Set pen color with RGB values (for an orange color)turtle.pencolor(1.0, 0.65, 0.0)
Other Commands
Section titled “Other Commands”write(text)
Section titled “write(text)”Writes text
on the screen at the turtle’s current position.
turtle.penup()turtle.goto(0, 50)turtle.write("Hello, Turtle!")