CS 151: Project #9

Project 9: Unique Trees and Shapes

Main course page

The project this week is to continue to make shape classes, making use of inheritance, starting with a tree class.


  1. Create a file called tree.py. Import your lsystem and shape modules. Create a Tree class that makes use of an Lsystem to generate a string to draw. The Tree class should inherit the Shape class, but you'll need to override some of the methods. The methods you'll need to override or create for the Tree class include:
    • def __init__(self, distance=5, angle=22.5, color=(0.5, 0.4, 0.3), iterations=3, filename=''): - The init function should call the parent init with distance, angle, and color, store the iterations number in an iterations field, then create an Lsystem object and store it in an lsystem field. If the filename is not empty, the Lsystem should read the file.
    • Create a setIterations accessor for the iterations field of the Tree object. Then create a read(self, filename) method that calls the lsystem's read method with the specified filename.
    • Override the draw method--but keep the same parameter list--so it uses the Lsystem to build the string, assigns the string to the string field of self, and then calls the parent draw function. You may want to change the default orientation to 90.

    Once you've written the tree class, make a test method for the class and try it out. The test method should take in an Lsystem filename, create a Tree object, and then use the Tree object's draw method to draw at least 3 trees.

    The output of your tree.py test method is required image 1.

  2. Create at least three other classes that inherit the Shape class and define different shapes using strings, just like the Square and Triangle classes. One of them should make a filled shape using curly brackets { and } to turn on and off the fill. Make a test function for your shape.py file that generates an image that incorporates all of the shapes you created. The function should test all of the capabilities of the different shape classes.

    The output of your shape.py test method is required image 2.

  3. Create a new indoor scene where part of your scene is a set of shapes and at least one tree in something that looks like a painting. You may use only the Tree and various shape classes from this assignment to create the scene. As with last week, only the Interpreter class can execute turtle commands.

    The indoor scene with a painting is required image 3.

  4. For scene 4, create a function tile(x, y, scale) that draws a set of shapes inside a square that is scale by scale in size with the lower left corner of the tile at location (x, y). If scale is 10, then the tile would be 10x10.

    Then make a function mosaic(x, y, scale, Nx, Ny) that draws a 2D array of tiles Nx by Ny, where each tile is of size scale by scale, and the lower left corner of the mosaic is at (x, y). So if scale is 10, Nx is 3 and Ny is 4, the function should draw twelve 10x10 tiles three across and four down.

    An image of at least 20 tiles (5 x 4) is required image 4.



Make a new wiki page for your assignment. Give the page a useful title using English words. Put the label cs151s11proj9 in the label field at the bottom of the page. Each of you needs to make your own writeup.

In addition to making the wiki page writeup, put the python files you wrote on the Academics server in your handin directory.

Colby Wiki

In general, your writeup should follow the outline below.