Turtle World
Turtle World
Turtle Graphics
Like all Python classes, the turtle class defines data and methods.
The data (state) of the turtle consists of:
Position: denoted by its current x and y coordinates; the units
are pixels.
Heading: denoted by an angle in degrees. East is 0 degrees.
north is 90 degrees; west is 180 degrees; south is 270
degrees.
Color: the color can be set to 224 (∼ 16.8 million) colors.
Width: the width of the line drawn as the turtle moves
(initially 2 pixels).
Down: a Boolean attribute indicating whether the turtle’s
tail is down.
Many turtle methods are listing in your textbook (pages 81, 83)
and online; Google “python turtle graphics.”
t = Turtle() create a new Turtle object and open its window
t.home() move the turtle to (0, 0), pointing east
t.pendown() lower the tail (t.down() also works)
t.penup() raise the tail (t.up() also works)
t.pensize(k) set linewidth to k pixels
t.setheading(d) change heading to direction d
t.left(d) turn left d degrees
t.right(d) turn right d degrees
t.speed(n) how fast the turtle moves (0 .. 10)
t.setx(n) set the turtle’s x coordinate, leave y unchanged
t.sety(n) set the turtle’s y coordinate, leave x unchanged
import turtle
def drawSquare ( ttl , x , y , length ) :
""" Draws a square using turtle ttl , with upper left
corner at (x , y ) , and side of length """
ttl . penup () # raise the pen
ttl . goto (x , y ) # move to starting position
ttl . setheading (0) # point turtle east
ttl . pendown () # lower the pen
for count in range (4) : # draw 4 sides :
ttl . forward ( length ) # move forward length ;
ttl . right (90) # turn right 90 degrees
ttl . penup () # raise the pen
If you’re in colormode 255, you can also specify the color with a
hex string value.
For example, the color Burnt Orange can be specified with the
RGB decimal triple: (255, 125, 25).
You can also specify it with the hex string value: ’#FF7D19’
because 255 is hex (base 16) value FF, 125 is hex value 7D, and 25
is hex value 19.
t.circle(r, ext, step) draw a circle with radius r, ext (arc of circle
drawn; 360 is entire circle), step (number of
segments).
t.dot(d, color) draw a filled circle with diameter r and color
Note: the circle is not centered at the starting point. If you want
that you could write:
def centered Circle ( ttl , r , x , y ) :
""" Draw a circle with radius r centered at (x , y ) . """
ttl . up () # raise the pen
angle = ttl . heading () # save the current heading
ttl . setheading (0) # set heading east
ttl . goto (x , y - r ) # move to bottom of circle
ttl . down () # pen down
ttl . circle ( r ) # draw the circle
ttl . up () # pen up
ttl . setheading ( angle ) # restore the heading
def d ra wS om e Ci rc le s ( ttl ) :
ttl . speed (10)
ttl . pensize (3) # line is 3 pixels
ttl . up ()
ttl . home () # go to (0 , 0)
ttl . down ()
ttl . pencolor ( ’ Green ’)
ttl . circle (25) # rad . 25 pixels
ttl . up ()
ttl . goto (0 , 0)
ttl . pencolor ( ’ Red ’)
ttl . down ()
ttl . circle (50 ,180) # arc 180 deg .
ttl . up ()
ttl . goto (0 , 0)
ttl . pencolor ( ’ Blue ’)
ttl . down ()
ttl . circle (75 ,360 ,8) # octogon
ttl . up ()
def c o n c e n t r i c C i r c l e s ( ttl ) :
""" Print 10 concentric circles . """
r = 10 # initial radius
for i in range (10) :
ttl . circle ( r * i )
ttl . up ()
ttl . sety (( r * i ) *( -1) )
ttl . down ()
If you draw a closed region, you can fill it with a specified color:
t.fillcolor() sets the pen fill color
t.begin fill() call this before filling a shape
t.end fill() call to no longer keep filling
t.filling() return True if filling, False otherwise
I don’t know why those weird lines are in there. They don’t show
up on the screen.
ts = turtle . getscreen ()
tc = ts . getcanvas ()
# creates a postscript image file
# substitute your own filename
tc . postscript ( file = " filename . eps " )
# converts to JPEG
save_as_jpg ( tc , " filename " )
turtle . done ()
import turtle
from PIL import Image
Joe . hideturtle ()
ts = turtle . getscreen ()
tc = ts . getcanvas ()
# creates a postscript image file
# substitute your own filename
tc . postscript ( file = " S eychelle sFlag . eps " )
# converts to JPEG
save_as_jpg ( tc , " Seyc hellesFl ag " )
turtle . done ()