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.
- Open your file zturtle.py from last week.
- 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
- left(self, angle) - adds angle to the current heading
- 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.
- 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
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.
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.
Now you have integrated LSystems and ZTurtle in a single application.
Once you've finished the above exercise, keep going on the current project.