Trees with Turtle in Python

Introduction

Make fractal trees using Python and Turtle.

Source code is available on https://github.com/dojojon/py_turtle/blob/master/tree.py

What you will make

Use loops and recursion to create fractal trees.

Steps

  1. Open the blank Python template Trinket: jumpto.cc/python-new.

  2. Add the following:

    1
    from turtle import *
  3. Add the following code to point our turtle up the canvas.

    1
    setheading(90)
  1. We are going to define a function that gets calls its self to draw the branches. It will set the branch color and draw the first branch. Add the following to define a function we can call.

    1
    2
    3
    def draw_branch():
    color("brown")
    forward(40)
  2. Below our new function add the following to call the function:

    1
    draw_branch()
  3. Our tree is made up of branches that get smaller and smaller, lets add an parameter to this function for the length. Update the function so it looks like this:

    1
    2
    3
    def draw_branch(len):
    color("brown")
    forward(len)
  4. And update where we call the function:

    1
    draw_branch(40)
  5. Next are going to draw two more branches. First we want to rotate the turtles heading, then we will call our draw branch function again. Update the draw branch code as follows:

    1
    2
    3
    4
    5
    def draw_branch(len):
    color("brown")
    forward(len)
    right(25)
    draw_branch(len)
  6. When you run the code you end up with a circle and a turtle that never stops. Lets fix that. Each time we draw a branch, lets make it shorter that the one before. Update the code as follows:

    1
    2
    3
    4
    5
    def draw_branch(len):
    color("brown")
    forward(len)
    right(25)
    draw_branch(len - 5)
  7. Looking a little better, but we need to tell the turtle to stop drawing branches when they get too small. We can do that with an if statement:

    1
    2
    3
    4
    5
    6
    def draw_branch(len):
    if (len > 5):
    color("brown")
    forward(len)
    right(25)
    draw_branch(len - 5)
  8. Lots better now, lets update the code to draw the other branch. So after we have draw a branch we want the turtle to go back to the start of the branch before drawing the next:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    def draw_branch(len):
    if (len > 5):
    color("brown")
    forward(len)
    right(25)
    draw_branch(len - 5)
    left(50)
    draw_branch(len - 5)
    right(25)
    backward(len)
  9. You should see a tree something like this:

  10. You can make thicker branches by using the following:

    1
    pensize(5)
  11. You can speed up the drawing by adding the following to the top of the script:

    1
    speed(0)
  12. Challenge: Try changing the shape of the by altering the length of the branches with a random number:

    Add the following to import the the random function

    1
    from random import randint

    Then update the draw_branch function to change how much we shorten each branch.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    def draw_branch(len):
    if (len > 5):
    color("brown")
    forward(len)
    right(25)
    draw_branch(len - randint(1,10))
    left(50)
    draw_branch(len - randint(1,10))
    right(25)
    backward(len)
  13. Challenge: Try changing the shape of the by altering the angle between the branches. Remember to make sure you rotate back the same amount each time.

  14. Challenge: Try adding leaves to the end of the branch. You can draw a filled circle using the following:

    1
    2
    3
    4
    color("green", "green")
    begin_fill()
    circle(10)
    end_fill()