Notes
Before using turtle, you have to first import turtle module.
import turtle
In standard mode, turtle initialized at the center of the screen by default and it faces right.
Drawing
turtle.forward(distance) | turtle.fd(distance)
Move the turtle forward, in the direction the turtle is headed, by the specified distance.
turtle.backward(distance) | turtle.back(distance) | turtle.bk(distance)
Move the turtle backward, in the opposite direction the turtle is headed, by the specified distance.
Filling
turtle.begin_fill()
To be called just before drawing a shape to be filled.
turtle.end_fill()
Fill the shape drawn after the last call to begin_fill().
Non-Drawing
turtle.home()
Move turtle to the origin position and reset to original direction.
turtle.right(angle) | turtle.rt(angle)
Turn turtle right by certain angle degrees.
turtle.left(angle) | turtle.lt(angle)
Turn turtle left by certain angle degrees.
turtle.setheading(angle) | turtle.seth(angle)
Set the orientation of the turtle to certain angle with respect to initial direction.
turtle.setx(x)
Set the turtle’s x-coordinate to x, leave y-coordinate unchanged.
turtle.sety(y)
Set the turtle’s y-coordinate to y, leave x-coordinate unchanged.
turtle.goto(x, y)
Move turtle to an absolute position.
Circles and Curves
turtle.circle(radius, [extent], [steps])
Draw a circle in counter-clockwise from current position with given radius.
Text
turtle.write(text, [move], [align], [font])
print text on the canvas at turtle's position.
If move (default is False) is True, then turtle position moves to the bottom-right corner of the text.
align (default is "left") indicates how the text is related to the turtle's position.
font is a triple (fontname, fontsize, fonttype). Default is ("Arial", 8, "normal")
Control
turtle.undo()
Undo the last turtle action.
turtle.pendown() | turtle.down() | turtle.pd()
Push the pen down – drawing when moving.
turtle.penup() | turtle.pu() | turtle.up()
Pull the pen up – no drawing when moving.
turtle.pensize([width]) | turtle.width([width])
Set the line thickness to width or return it. If no argument is given, the current pensize is returned.
turtle.pencolor([colorCode])
Set pencolor to the given value. If no argument is given, the current pencolor is returned.
turtle.fillcolor([colorCode])
Set fillcolor to the given value. If no argument is given, the current fillcolor is returned.
turtle.color(colorCode)
Set both pencolor and fillcolor to the given value.
Screen Control
turtle.Screen
Getting turtle Graphics screen object.
turtle.Turtle
Getting turtle Graphics Turtle object.
turtle.bgcolor([colorCode])
Set and return background color of the TurtleScreen.
turtle.clear()
Clear all drawing on TurtleScreen.
turtle.reset()
Clear all drawing on TurtleScreen and reset turtle to its initial state.
turtle.bye()
Close the turtlegraphics window.
turtle.speed(speed)
“fastest”: 0
“fast”: 10
“normal”: 6
“slow”: 3
“slowest”: 1
Add the following to draw without animation
delay(0)
tracer(0)
//put your code here
update()
Getting turtle and Windows States
turtle.heading()
Return the turtle’s current heading.
turtle.filling()
Return fillstate (True for filling, otherwise False)
turtle.isdown()
Return True if pen is down, False if it’s up.
turtle.isvisible()
Return True if the Turtle is shown, False if it’s hidden.
turtle.window_height()
Return the height of the turtle window.
turtle.window_width()
Return the width of the turtle window.
Interactive
turtle.Screen()
Create a Screen object
turtle.Turtle()
Create a Turtle object
turtle.onkey(fun, keyStr)
turtle.onkeyrelease(fun, keyStr)
turtle.onkeypress(fun, keyStr=None)
Call a designated function when the key correlates to keyStr is pressed or released.
turtle.onclick(fun, btn=1, add=None)
Call a designated function when click, release, or drag the mouse . The coordinate of the mouse at the event location will be sent as two parameter to the function.
turtle.listen()
Set focus on Screen.