"> Codesharer · Fractal tree in Python 3 using Turtle Code Snippet

Welcome to Code Sharer, a code gallery for creating, saving and sharing all kinds of code snippets.

Python — Fractal tree in Python 3 using Turtle

Posted On: Monday, August 6, 2018 at 5:48 AM

import turtle

t = turtle.Turtle(shape="square")

t.lt(90)

lv = 14
l = 120
s = 45

t.width(lv)

t.penup()
t.bk(l)
t.pendown()
t.fd(l)

def draw_tree(l, level):
width = t.width() # save the current pen width

t.width(width * 3.0 / 4.0) # narrow the pen width

l = 3.0 / 4.0 * l

t.lt(s)
t.fd(l)

if level < lv:
draw_tree(l, level + 1)
t.bk(l)
t.rt(2 * s)
t.fd(l)

if level < lv:
draw_tree(l, level + 1)
t.bk(l)
t.lt(s)

t.width(width) # restore the previous pen width

t.speed("fastest")

draw_tree(l, 4)

turtle.done()