pbaumgarten.com

Learn Python Turtle

The turtle introduction is intended to suit for younger students who are opting-in to embrace a programming environment more challenging than Scratch but are a little intimidated by the idea of coding in a pure text environment. The turtle allows you to learn the basics through a graphical/visual system where you draw and animate various shapes on screen.

Use either of the following tools:

Import

from turtle import *
from random import randint

Basic movement and drawing

The turtle will begin in the screen center, facing right. Positive angles rotate counter-clockwise

Command Example Description
home() home() return to the starting point and heading
right( angle-in-degrees ) right(45) Rotate clockwise the given number of degrees
left( angle-in-degrees ) left(45) Rotate counter-clockwise the given number of degrees
goto( x-coord, y-coord ) goto(-50, 50) Jump to new x,y coordinates on screen
setx( x-coord ) setx(100) Jump only the x coordinate to new position
sety( x-coord ) sety(100) Jump only the y coordinate to new position
setheading( new-angle-in-degrees ) setheading(90) Point in new direction where 0 == facing right. Positive numbers turn counter-clockwise
forward( distance ) forward(100) Move forward given distance of pixels
backward( distance ) backward(100) Move backward given distance of pixels
circle( radius ) circle(50) Draw a circle with radius 50 pixels
circle( radius, arc-size-in-degrees ) circle(50, 180) Draw part of a circle, determined by number of degrees given
dot( radius ) dot(50) Draw a filled circle(dot) of given size
hideturtle() hideturtle() Will still draw but hide the little animated turtle shape. Will speed up complex drawings
showturtle() showturtle() Show the turtle when drawing

Pen control

Command Example Description
pendown() pendown() Draw including whenever moving, jumping location
penup() penup() Stop drawing when moving
pensize( width ) pensize(1) Thickness to draw lines
isdown() isdown() Returns True or False based on if the pen is down

Get turtle information

Command Example Description
position() x,y = position() Returns an (x,y) tuple of the current location
xcor() x = xcor() Get the current x-coordinate location
ycor() y = ycor() Get the current y-coordinate location
heading() direction = heading() Get the current facing direction in degrees

Colors

Command Example Description
pencolor( color ) pencolor( "yellow") Change the pen color
fillcolor( color ) fillcolor( "lime") Change the fill color - see begin_fill() and end_fill()!
begin_fill() begin_fill() Tells Python you are starting a shape you want to be filled in when complete
end_fill() end_fill() Tells Python you have finished the shape and to fill it in
bgcolor( color ) bgcolor( "sky blue") Change the background color
bgpic( picture_file ) bgpic( "background.gif") Set a background picture. Must be GIF format
bgpic( "nopic" ) bgpic( "nopic") Removes the background picture

Note: colors can be any of the following:

Screen settings

Command Example Description
screensize( width, height ) screensize( 640,480 ) Set width and height of turtle screen
title( name ) title( "My amazing project" ) Set title name of turtle screen
reset() reset() Clear screen, re-center turtle, reset heading to right
clear() clear() Clear screen without recentering turtle or resetting heading
window_width() w = window_width() Get screen width
window_height() h = window_height() Get screen height
isvisible() vis = isvisible() Is the turtle visible?
speed( new-speed ) speed(10) Set drawing speed between 1 and 10. Normally starts at 6.
bye() bye() Close turtle
exitonclick() exitonclick() Tells Turtle to quit if the exit icon of the screen is clicked

Events

Command Example Description
onscreenclick( function ) onscreenclick( click ) execute function when screen clicked. callback must take two parameters for x,y coordinates of the click
onrelease( function ) onrelease( click ) execute function when mouse click let go. callback must take two parameters for x,y coordinates of the click
onkeypress( function, key ) onkeypress( pressed, "Up" ) execute function when nominated key is pressed. callback must take two parameters for x,y coordinates of the click
ontimer( function, time-in-ms ) ontimer( ticktock, 1000 ) execute function once after given number milli-seconds
mainloop() mainloop() Start the main event handling loop to run your game

Write text to screen

Command Example Description
write( text ) write( "Hello there") Write text to screen where ever the turtle is
write( text, font=(fontname,size,weighting) write( "Hello there", font=("Arial",10,"normal")) Write text to screen of specified font

Input popup prompts

Command Example Description
textinput("title", "prompt") name = textinput("Name", "What is your name?" Popup box for text information
numinput("title", "prompt") num = numinput("Enter a number", "Enter a number between 0 and 100") Popup box to enter a number

Lessons

Lesson 1, 2 and 3 will teach you how to use turtle to draw on your screen.

Lesson 4 will teach you how to use mouse clicks to control where your turtle goes.

Lesson 5 will teach you how to have two turtles on screen, one chasing the other.

Lesson 6 will add a timing feature to our lesson 5 game. How long can you keep the turtle from getting caught?

Lesson 7 will finish with a mini project challenge.

Turtle reference