Python: Turtle reference

Reference

An abbreviated instruction set for the turtle, taken from the Python reference docs.

Import

from turtle import *

Moving and Drawing

forward( distance )
backward( distance )
right( angle-in-degrees )
left( angle-in-degrees )
goto( x-coord, y-coord )
setx( x-coord )
sety( x-coord )
setheading( new-angle-in-degrees ) ... 0 = right, increasing counter-clockwise
home() ... return to starting point and heading

Shapes

circle( radius )
circle( radius, arc-size-in-degrees )
dot( size )

Pen control

pendown()
penup()
pensize( width )
isdown() ... returns True or False

Get turtle information

position() ... returns an (x,y) tuple
xcor() ... returns the x-coordinate
ycor() ... returns the y-coordinate
heading() ... returns the heading

Colors

pencolor( "#abcdef" )
pencolor( "yellow" )
pencolor( r, g, b )
pencolor(( r, g, b ))
pencolor() ... will return the colour

fillcolor( "#abcdef" )
fillcolor( "yellow" )
fillcolor( r, g, b )
fillcolor(( r, g, b ))
fillcolor() ... will return the colour
begin_fill() ... start a new shape to fill when completed
end_fill() ... fill the shape created after begin_fill()

bgcolor( "#abcdef" )
bgcolor( "yellow" )
bgcolor( r, g, b )
bgcolor(( r, g, b ))
bgcolor() ... will return the colour

bgpic( "background.gif" )
bgpic( "nopic" ) ... removes the background pic

Screen settings

reset() ... clear screen, re-center turtle, reset heading
clear() ... clear screen only
screensize( width, height ) ... set width & height
title( screen_title )
window_width() ... return the width
window_height() ... return the height

hideturtle()
showturtle()
isvisible() ... returns True or False
speed( speed ) ... between 1 to 10. normal == 6

bye() ... close turtle graphics window
exitonclick() ... close the turtle graphics window when the quit icon is clicked

Events

onscreenclick( function ) ... function must take two parameters for x,y coordinates of the click
onrelease( function ) ... function must take two parameters for x,y coordinates of the click
ondrag( function ) ... function must take two parameters for x,y coordinates of the click
onkey( function, key ) ... function to execute if key is pressed
onkeyrelease( function, key ) ... function to execute if key is pressed
ontimer( function, time-in-milliseconds )

listen() ... activate listening to events
done() ... starts the event loop.

Write text

write( text )
write( text, align="left|center|right" )
write( text, font=("Arial", 10, "normal") )

Input

string = textinput( "title", "prompt" )
num = numinput( "title", "prompt" )
num = numinput( "title", "prompt", default, minimum-value, maximum-value )