"Turtle Graphics in Python
Documentation on Turtle Graphics: This is the ultimate source for all information on this library.
Comprehensive List of Colors
List of Programs in Turtle Graphics
# File: Hello.py
# Description: This program writes out Hello World
import turtle
def main():
# put label on top of page
turtle.title ('Hello World')
# setup screen size
turtle.setup (1000, 1000, 0, 0)
# move turtle to origin
turtle.penup()
turtle.goto (0, 0)
# set the color to navy
turtle.color ('navy')
# write the message
turtle.write ('Hello World!', font = ('Times New Roman', 36, 'bold'))
# hide the turtle
turtle.hideturtle()
# persist the drawing
turtle.done()
main()
# File: Squares.py
# Description: Draws squares of different sizes
import turtle
# draw a square of a given side
# starting at uuper left corner (x, y)
def drawSquare (ttl, x, y, side):
ttl.penup()
ttl.goto(x, y)
ttl.setheading(0) # set the pen in the +ve x direction
ttl.pendown()
for iter in range (4):
ttl.forward(side)
ttl.right(90)
ttl.penup()
def main():
# put label on top of page
turtle.title ('Squares')
# setup screen size
turtle.setup (800, 800, 0, 0)
# create a turtle object
ttl = turtle.Turtle()
# assign a color to the turtle object
ttl.color ('red')
# draw multiple squares
drawSquare (ttl, -50, -50, 50)
drawSquare (ttl, 0, 0, 50)
drawSquare (ttl, 50, 50, 50)
drawSquare (ttl, -50, 50, 150)
# fill a closed region
ttl.fillcolor ('purple')
ttl.begin_fill()
drawSquare (ttl, 0, 0, 50)
ttl.end_fill()
# persist drawing
turtle.done()
main()
# File: Figures.py
# Description: Draws various types of geometric figures
import turtle, math
# draw a line from (x1, y1) to (x2, y2)
def drawLine (ttl, x1, y1, x2, y2):
ttl.penup()
ttl.goto (x1, y1)
ttl.pendown()
ttl.goto (x2, y2)
ttl.penup()
def drawPolygon (ttl, x, y, num_side, radius):
sideLen = 2 * radius * math.sin (math.pi / num_s