CS 151: Lab #10

### Lab Exercise 10: Using a Turtle

For the last project, we created a ZTurtle object in the lab that implemented turtle graphics using the Zell graphics library. This week we'll extend the class a bit and add an interpreter for L-system strings. Then we'll use the LSystem class you built in the last project to create a simple scene.

1. Open your file zturtle.py from last week.
2. The turtle functions we want to have available are the following. Write any of the functions that don't yet exist. Note that you may need to change some of the internal field names so they don't conflict with method names.
• forward(self, distance) - moves the turtle forward, draws a line if the pen is down
• up(self) - sets the state of the pen to up
• down(self) - sets the state of the pen to down
• right(self, angle) - subtracts angle from the current heading
• clear(self) - undraws all of the shapes, resets the shapes field back to an empty list
• color( self, c ) - sets the color field of the turtle to c, where c is a Zelle color. (Note that fields and functions may not share the same name in a class). If you wish, you can use the original form of the turtle color method which takes in three values (r, g, b) in the range [0, 1.0].
• width( self, w ) - sets the width field of the turtle to w. (Same note as above)
• goto( self, x, y ) - Sends the turtle to the point (x, y). If the pen is down, draws a line from the current location to location (x, y). Note that this is basically identical to the forward function except that the new location of the turtle is given to you by the parameters x and y.
• setheading( self, h ) - Sets the heading field to h.
• heading( self ) - returns the current heading of the turtle in degrees.

3. The following Zelle graphics object methods should also be defined for the turtle.
• setOutline( self, c ) - sets the color of all of the lines in self.shapes to c.
• setWidth( self, c ) - sets the width of all of the lines in self.shapes. to w.
• move( self, tx, ty ) - moves all of the lines in self.shapes to a new location.
• draw( self, win) - draws all of the lines to the given window.
• undraw( self ) - undraws all of the lines.
• clone( self ) - clones the turtle object, its state, and all of its lines. The algorithm for cloning is as follows:
• Create a new ZTurtle object. If the constructor (__init__) take arguments, pass in the values from the fields of self.
• Except for the shapes list, copy the values of any fields not handled by the constructor into the new turtle object.
• Go through the self.shapes list and append a clone of each object into the new turtle's shapes list.
• Return the new ZTurtle object
4. Set up the turtle so that if the draw function has been called that any subsequent forward commands automatically draw the new line. Calling the undraw method should turn off this feature. To implement this, create a field called self.win and initialize it to None in the init method. Then you can test that field in the forward and goto methods to see if the line should be drawn.

In the draw method, set self.win to the window reference win.

In the undraw method, set self.win to be None.

In the forward method, test if self.win is not None. If so, then draw the shape collected by the turtle. Do the same in the goto method.

5. Add a method to your ZTurtle class called lsystem that takes self and lsys, an LSystem object, as input parameters. The method should generate a string using lsys and then parse the string into turtle commands. For something like the 'F' case, make use of your existing turtle methods. Your turtle object is self inside the lsystem method, so to call the forward method you would use self.forward(distance).
6. At the top of your zturtle.py file, import LSystem from lsystem2.
7. In your test section/code for the ZTurtle class, create an Lsystem object. Set it to make a simple tree. For example:
```    ls.set('F', [ ['F', 'fF[-F-F+F]F[+F+F]'] ], 3, 24, 4 )
```
Send the turtle somewhere in the image. Then call the lsystem method, giving the lsystem you just created as an argument. Then call the turtle's draw method. This should draw the tree.
8. Add a few regular graphics objects to the test scene. Now you can mix and match the two kinds of shapes.

Now you have integrated LSystems and ZTurtle in a single application.

Once you've finished the above exercise, keep going on the current project.