Turtle Docs

Python Sandbox is powered by Skulpt, which includes a Turtle Graphics module. This page gives an overview of the available methods.

Before You Begin

Before you can begin utilizing turtle graphics, you must be sure to import the turtle module and create a Turtle.

import turtle
t = turtle.Turtle()

Save Your Creations

In addition to saving your code, you can always download an image file of your creation. All images download in the PNG format and are transparent black (though the canvas appears white).

Simple Movement

Forward

Using this command the turtle can move forward a given number of spaces (pixels).

Note: If a negative value is provided, the turtle will move backwards

forward(value) fd(value)
# turtle moves forward 100 spaces
t.forward(100)

Backward

Using this command the turtle can move backward a given number of spaces (pixels).

Note: If a negative value is provided, the turtle will move forward.

backward(value) back(value) bk(value)
# turtle moves backward 100 spaces
t.backward(100)

Right

Using this command the turtle can turn right a given number of degrees.

Note: If a negative degree is given, the turtle will turn left.

right(degrees) rt(degrees)
# Turns the turtle right 90 degrees.
t.right(90)

Left

Using this command the turtle can turn left a given number of degrees.

Note: If a negative degree is given, the turtle will turn right.

left(degrees) lt(degrees)
# Turns the turtle left 90 degrees.
t.left(90)

Circle

Using this command the turtle moves in a circlular patter of a given radius.

Note: The turtle's starting point is point of the circle (not the center). The turtle rotates to the it's left as it makes the circle.

circle(radius)
# Moves around circle of radius 50
t.circle(50)

This command can also be used to move over only a portion of a circle (arc) if a second degree measure (measure of the arc) is given.

circle(radius, degrees)
# Moves 180 degrees around a circle of radius 75
t.circle(75,180)

Speed

This command can be used to alter the speed of the turtle. The lower the value, the quicker the turtle animates. A value of 0 is fastest

speed(5)
# Sets the speed of the turtle to 5 (a normal speed)
t.speed(5)

Advanced Movement

SetX

This command moves the turtle to the x-coordinate value specified. The canvas mimics a Cartesian coordinate system whose center is (0,0).

Note: This moves the turtle to the specified position, so if the turtle's pen is down, the turtle will draw.

setx(value)
# Sets the x-coordinate of the turtle to -100
t.setx(-100)

SetY

This command moves the turtle to the x-coordinate value specified. The canvas mimics a Cartesian coordinate system whose center is (0,0).

Note: This moves the turtle to the specified position, so if the turtle's pen is down, the turtle will draw.

sety(value)
# Sets the y-coordinate of the turtle to 100
t.sety(100)

SetPos

This command sets both the x and y-coordinate at the same time.

Note: This moves the turtle to the specified position, so if the turtle's pen is down, the turtle will draw.

setpos(x,y)
# Sets the position of the turtle to (-100,100)
t.setpos(-100,100)

SetHeading

This command sets the turtle's heading to the specified degree measure. Headings are given in degrees where 0 points to right, 90 points up, 180 points left, and 270 points down.

setheading(degree) seth(degree)
# Causes the turtle to face up and to the right (northeast).
t.setheading(45)

Home

This command sends the turtle back to its home position, the origin (0,0). If the turtle's pen is down, it will draw as it is sent back home.

home()
# Sends the turtle home
t.home()

Drawing Tools

PenDown

Puts the turtle's pen down so that it draws as it moves (default setting).

pendown() pd() down()
# Put's the turtle's pen down
t.pendown()

PenUp

Puts the turtle's pen in the up position so that it does not draw as it moves.

penup() pu() up()
# Put's the turtle's pen up
t.penup()

PenSize

Sets the width of the turtle's pen.

pensize(width) width(width)
# Sets the pen size to 3
t.pensize(5)

Fill

Turns the turtle's fill mode on and off. When on, the turtle fills the shapes as it draws them. The command requires a boolean value (True/False) indicates whether fill mode should be on.

fill(boolean)
# Turns fill mode on
t.fill(True)
# Turns fill mode off
t.fill(False)

PenColor

Set's the turtle's pen color. The pen color can be set to any HTML/CSS recognized color string. More information on HTML colors can be found here.

pencolor("{color}")
# Sets the pen color to red
t.pencolor("red")

This method can also used by providing an amount of red, green, blue, and opactity (alpha). Each color amount is an integer value between 0 and 255 and the opacity is a floating-point value between 0 and 1 (0 = transparent).

pencolor(red, green, blue, alpha)
# Sets the pen color to half transparent blue.
t.pencolor(0,0,255,0.5)

FillColor

Set's the turtle's fill color. The fill color can be set to any HTML/CSS recognized color string. More information on HTML colors can be found here.

pencolor("{color}")
# Sets the fill color to light grey
t.fillcolor("#CCCCCC")

This method can also used by providing an amount of red, green, blue, and opactity (alpha). Each color amount is an integer value between 0 and 255 and the opacity is a floating-point value between 0 and 1 (0 = transparent).

fillcolor(red, green, blue, alpha)
# Sets the fill color to a semi-transparent green.
t.fillcolor(0,255,0,0.85)

Clearing & Reseting

Clear

Clears the turtle's canvas, causing all drawings to be erased.

clear()
# Clears the canvas
t.clear()

Reset

Clears the turtle's canvas and resets all settings and positioning of the turtle.

reset()
# Resets the canvas and turtle
t.reset()

Utility

HideTurtle

Hides the turtle from view

hideturtle() ht()
# Hides the turtle
t.hideturtle()

ShowTurtle

Shows the turtle on the canvas.

showturtle() st()
# Shows the turtle
t.showturtle()

IsVisible

Returns a boolean indicating whether the turtle is visible.

isvisible()
# If the turtle is not visible, show it.
if not(t.isvisible()):
  t.showturtle()

IsDown

Returns a boolean indicating whether the turtle's pen is down.

isdown()
# If the pen is down, it picks it up
if t.isdown():
  t.penup()