
Python Tutorial for Turtle Graphics
Initially, the turtle was only a physical thing, similar to a robot that could be
placed on a piece of paper and told to move. The turtle then transformed into a visual display on a high-resolution screen, often depicted as any shape.
Even in simply pictorial form, the concept of a turtle can help to represent activities taken and hence what a programme performs.
The turtle module creates a 2-dimensional grid environment in which turtles can move around. Turtles have a position, a heading (the direction the turtle is facing), and a variety of different states (turtles can draw lines of a specific colour when they move or leave no trace) and actions (turtles can turn left or right; go ahead or backward).
Source Code
import turtle as tu
roo = tu.Turtle() # Turtle object
wn = tu.Screen() # Screen Object
wn.bgcolor(\"black\") # Screen Bg color
wn.title(\"Fractal Tree Pattern\")
roo.left(90) # moving the turtle 90 degrees towards left
roo.speed(20) # setting the speed of the turtle
def draw(l): # recursive function taking length \'l\' as argument
if (l < 10):
return
else:
roo.pensize(2) # Setting Pensize
roo.pencolor(\"yellow\") # Setting Pencolor as yellow
roo.forward(l) # moving turtle forward by \'l\'
roo.left(30) # moving the turtle 30 degrees towards left
draw(3 * l / 4) # drawing a fractal on the left of the turtle object \'roo\' with 3/4th of its length
roo.right(60) # moving the turtle 60 degrees towards right
draw(3 * l / 4) # drawing a fractal on the right of the turtle object \'roo\' with 3/4th of its length
roo.left(30) # moving the turtle 30 degrees towards left
roo.pensize(2)
roo.backward(l) # returning the turtle back to its original psition
draw(20)
roo.right(90)
roo.speed(2000)
def draw(l):
if (l < 10):
return
else:
roo.pensize(2)
roo.pencolor(\"magenta\") # magenta
roo.forward(l)
roo.left(30)
draw(3 * l / 4)
roo.right(60)
draw(3 * l / 4)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(20)
roo.left(270)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10):
return
else:
roo.pensize(2)
roo.pencolor(\"red\") # red
roo.forward(l)
roo.left(30)
draw(3 * l / 4)
roo.right(60)
draw(3 * l / 4)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(20)
roo.right(90)
roo.speed(2000)
# recursion
def draw(l):
if (l < 10):
return
else:
roo.pensize(2)
roo.pencolor(\'#FFF8DC\') # white
roo.forward(l)
roo.left(30)
draw(3 * l / 4)
roo.right(60)
draw(3 * l / 4)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(20)
def draw(l):
if (l < 10):
return
else:
roo.pensize(3)
roo.pencolor(\"lightgreen\") # lightgreen
roo.forward(l)
roo.left(30)
draw(4 * l / 5)
roo.right(60)
draw(4 * l / 5)
roo.left(30)
roo.pensize(3)
roo.backward(l)
draw(40)
roo.right(90)
roo.speed(2000)
def draw(l):
if (l < 10):
return
else:
roo.pensize(3)
roo.pencolor(\"red\") # red
roo.forward(l)
roo.left(30)
draw(4 * l / 5)
roo.right(60)
draw(4 * l / 5)
roo.left(30)
roo.pensize(3)
roo.backward(l)
draw(40)
roo.left(270)
roo.speed(2000)
def draw(l):
if (l < 10):
return
else:
roo.pensize(3)
roo.pencolor(\"yellow\") # yellow
roo.forward(l)
roo.left(30)
draw(4 * l / 5)
roo.right(60)
draw(4 * l / 5)
roo.left(30)
roo.pensize(3)
roo.backward(l)
draw(40)
roo.right(90)
roo.speed(2000)
def draw(l):
if (l < 10):
return
else:
roo.pensize(3)
roo.pencolor(\'#FFF8DC\') # white
roo.forward(l)
roo.left(30)
draw(4 * l / 5)
roo.right(60)
draw(4 * l / 5)
roo.left(30)
roo.pensize(3)
roo.backward(l)
draw(40)
def draw(l):
if (l < 10):
return
else:
roo.pensize(2)
roo.pencolor(\"cyan\") # cyan
roo.forward(l)
roo.left(30)
draw(6 * l / 7)
roo.right(60)
draw(6 * l / 7)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(60)
roo.right(90)
roo.speed(2000)
def draw(l):
if (l < 10):
return
else:
roo.pensize(2)
roo.pencolor(\"yellow\") # yellow
roo.forward(l)
roo.left(30)
draw(6 * l / 7)
roo.right(60)
draw(6 * l / 7)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(60)
roo.left(270)
roo.speed(2000)
def draw(l):
if (l < 10):
return
else:
roo.pensize(2)
roo.pencolor(\"magenta\") # magenta
roo.forward(l)
roo.left(30)
draw(6 * l / 7)
roo.right(60)
draw(6 * l / 7)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(60)
roo.right(90)
roo.speed(2000)
def draw(l):
if (l < 10):
return
else:
roo.pensize(2)
roo.pencolor(\'#FFF8DC\') # white
roo.forward(l)
roo.left(30)
draw(6 * l / 7)
roo.right(60)
draw(6 * l / 7)
roo.left(30)
roo.pensize(2)
roo.backward(l)
draw(60)
wn.exitonclick()