CS 151: Project 8

Title image Project 8
Fall 2019

Project 8: Better Trees

As with last week, the assignment is to bring together the lsystem and turtle interpreter classes to make a scene that consists of fractal shapes and trees. Your top-level program will import both the lsystem and interpreter modules. Unlike last week, however, your scene.py may not import the turtle module or make any calls to turtle commands directly. If you want to draw something, you have to pass a string to the interpreter drawString method. There will be no exceptions to that rule. (Note that you may attach arbitrary meaning to any character not used by an L-system grammar.)


  1. Add a color stack to the TurtleInterpreter

    It would be nice to be able to change the color of elements in an Lsystem without affecting the color of other elements. A good design will also avoid setting colors, widths, or other turtle drawing features by adding new parameters to drawString. Instead, modify the drawString method so that it handles five additional characters in the strings.

    • Define < to push a color on the stack

      '<' - the left angle bracket should push the current turtle color onto a color stack. You'll need to create a separate colorstack variable similar to the one used for position and heading. You can use the function turtle.color() to get the turtle's current color. Note that this function returns a tuple of colors. You should append just the first element of the tuple onto the color stack.

      colorstack.append( turtle.color()[0] )

    • Define > to pop a color off the stack

      '>' - the right angle bracket should pop the current turtle color off the color stack and set the turtle's color to that value using the turtle.color function.

    • Define g as green

      'g' - set the turtle's color to green (e.g. (0.15, 0.5, 0.2) ).

    • Define y as yellow

      'y' - set the turtle's color to light yellow ( e.g. (0.8, 0.8, 0.3) ).

    • Define r as red

      'r' - set the turtle's color to red ( e.g. (0.7, 0.2, 0.3) ).

    Be sure to use the turtle.color() and turtle.width() methods properly. They are turtle functions, and the new color or new width needs to be an argument to the function.

    Once you have made the modifications to drawString, try running systemFL using the classtest.py test function from the lab. You can also use the test function single.py to draw just a single tree.

  2. Create a scene that is an arrangement of L-systems

    Create a file arrangement.py. Have the file import lsystem and turtle_interpreter (the new versions). Write a function that makes an arrangement of trees or flowers (note that an arrangement is more than random placement!). The scene should include some multi-rule L-systems with leaves or other ornaments that make use of the color store/restore. You can use the symbol L to indicate a leaf, which could be drawn as a semicircle or a circle or a line. The following are variations with leaves and flowers, all defined by strings (no special characters except L). Try to include L-system trees with different numbers of iterations. (Note that at least two of the L-systems you use must have at least 2 rules.)

    systemCL systemDL systemEL systemFL systemGL

    The arrangement is required image 1.

    Please indicate either in your report or in your code which L-systems you usee. If you happen to use command line arguments to specify filenames and don't tell us which systems you used, then we can't run your code to reproduce your image. So either hard-code which L-systems you use or indicate which ones you used to make the pictures in your report.

  3. Create your own L-system

    Make a new L-system of your own (base string and rule). They can be variations on one of the ones provided, a variation on an L-system from the ABOP book, or one you create on your own. The L-systems of interest are given in the ABOP book chapter 1, pages 10, 11, and 25.

    If you use a variation, include both the original and your variant in our report. (The difference does not have to be large.)

    In the file growth.py, create a scene function that makes an image using your L-systems after 2, 3, and 4 iterations. Note, if using a different pattern of iterations is more interesting (e.g. 4, 5, 6 or 2, 4, 6) you are free to use a different set of three iteration values. Indicate what you used in your writeup.

    The growth image is required image 2.

    Please indicate either in your report or in your code which L-systems you used (and include the txt files for your new L-systems with your code when you turn it in!).

Follow-up Questions

  1. What is a class?
  2. What is an object?
  3. What is the role of self in a class method?
  4. Given an object x how do you call its method y?


Extensions are your opportunity to customize your project, learn something else of interest to you, and improve your grade. The following are some suggested extensions, but you are free to choose your own. Be sure to describe any extensions you complete in your report. Include pictures.

Submit your code

Turn in your code (all files ending with .py) by putting it in a directory in the Courses server. On the Courses server, you should have access to a directory called CS151, and within that, a directory with your user name. Within this directory is a directory named private. Files that you put into that private directory you can edit, read, and write, and the professor can edit, read, and write, but no one else. To hand in your code and other materials, create a new directory, such as project1, and then copy your code into the project directory for that week. Please submit only code that you want to be graded.

When submitting your code, double check the following.

  1. Is your name at the top of each code file?
  2. Does every function have a comment or docstring specifying what it does?
  3. Is your handin project directory inside your Private folder on Courses?

Write Your Project Report

If you haven't already made a new page for this report on the wiki, then make one now (Log into the wiki, goto your Personal space by selecting "Personal Space" on the menu under the Person icon, then make the page using the "Create" button. Put the label cs151f19project8 in the label field on the bottom of the page. But give the page a meaningful title.

Your intended audience for your report is your peers not in the class. From week to week you can assume your audience has read your prior reports. Your goal should be to be able to use it to explain to friends what you accomplished in this project and to give them a sense of how you did it.

Your project report should contain the following elements.