#!/usr/bin/env python3 """ This template describes a tree that is drawn recursively using the turtle module. """ __author__ = "" __version__ = "" from turtle import * def branch(t, length): """We'll need to have as parameters: * the turtle we're using to draw the tree * the length of this branch that we're about to draw Additional parameters that can be added in subsequent versions of the progrm include line thickness, color, angle... """ # if the length has gotten too small (base case): return # otherwise: # draw a line forward of the given length # turn the turtle to the right some number of degrees # call the branch function recursively with a smaller branch length # (At some point, upon returning from THIS branch, we'll be back # (at the end point of THIS branch, at which time...) # turn the turtle to the left twice the degrees as before # call the branch function recursively as before # turn the turtle back to the right (so that it is in it's original # "forward" position # move the turtle back of the given length return def main(): win = Screen() t = Turtle() t.penup() # don't leave a line while we're moving the turtle t.goto(0,-200) # move to this location on the screen: # horizontally centered, and 200 pixels below the center t.left(90) # aim turtle "up" to create our first branch t.pendown() # pendown--we're ready to draw lines! t.speed(0) # let's draw them as fast as possible branch(t, 100) # It's a branch, but really it's our "trunk" win.exitonclick() # Leave the window open so we can admire our work if __name__ == "__main__": main()