""" Using turtles to draw recursive patterns.
"""
import turtle
# constant for choosing color by number
COLOR = ("red", "green", "blue")


def tree_burst(turtle_, length, level):
    """
    Draw a tree with 'level' levels, using turtles.
    At each level, we are sprouting 3 "branches" using the starting
    position of the Turtle turtle_.
    Each branch will be angled at 0, 120, and 240 degrees and
    draw a little branch of length pixels, then use that new
    starting point to spawn 3 more branches recursively..

    @param int level: how many levels of recursion to use
    @param int length: number of pixels to draw current shape
    @param Turtle turtle_: drawing turtle
    @rtype: None
    """
    if level == 0:
        pass
    else:
        turtle_list = []  # place to keep 3 turtles
        for h in range(3):
            # store new turtle
            turtle_list.append(turtle_.clone())
            # set colour, using weird spelling
            turtle_list[h].color(COLOR[h])
            # set direction (angled at 0, 120, 240 degrees)
            turtle_list[h].setheading(120 * h)
            # draw a little
            turtle_list[h].forward(length)
            # 1/2 size version
            tree_burst(turtle_list[h], length / 2, level - 1)


if __name__ == "__main__":
    import time
    T = turtle.Turtle()
    T.color("red")
    T.speed("slow")
    # hide the tail...
    T.hideturtle()
    tree_burst(T, 128, 4)
    time.sleep(10)
