CS151 HW#11

Tara Davidson '10

Write up:

  1. A brief description of the task (in your own words): In this project I created a child class, Crayon, that inherited common code from a given parent class, AggregateBase, but modified parts so that I could create a scene that looked like it was drawn in crayon. I created my Crayon class in zurtle2.py and put it at the top of the program. In my Crayon class I made two methods, an init method and a setWidth method. The init method takes two points and draws two lines in between the points (one line from p1 to the midpoint and one line from the to p2). However, I also perturbed p1, the midpoint, and p2 so that the lines created are not perfectly straight. This gives the effect that the lines were more humanly drawn. The second method I created in Crayon was a setWidth method that randomly sets the width between 1 and a constant, 2*w. I did this by creating a for loop with all the objects in self.shapes, creating a random value using random.randint and then setting the width of each object to that width. After that I modified the init and forward methods of my ZTurtle function so that the crayon was used instead of the normal line. I also created a new method in ZTurtle, crayon, that sets the new True/False field icrayon to a constant, c. I then created a scene using my crayon which is posted below.
  2. Explain how you would modify the ZTurtle class to permit a third drawing option like a paintbrush if it were implemented as a class like Crayon. What in the ZTurtle class would need to change Another field would be need to be created in the init function (ex. self.ipaintbrush = False) and in the forward function there would need to be a way to decide when to use the computer's default line, the crayon, or the paintbrush (this would probably involve if, elif, else statements) and then icrayon or ipaintbrush would need to be respectively called. Another method, def paintbrush, would need to be created to set the field ipaintbrush to something other than False.
  3. Explain the benefits of inheritance in creating the Crayon class Inheritance allows code from a parent class to be overwritten by code in the child class. This is helpful because it means that useful common code does not need to be copied over and over again and the child class can be used in specific instances, not necessarily all the time. The Crayon class is a child class with parent class, AggregateBase. Crayon uses AggregateBase code but modifies parts of AggregateBase so that the unique effect of not-perfect lines and random widths can be created.
  4. Why do we need to override the setWidth class for the Crayon class? In the AggregateBase class, setWidth sets all objects in self.shapes to a certain, constant width. When trying to create a crayon effect, I didn't want my lines to always be a certain, constant width so I made a new method in my child class to allow random widths to be set when the crayon is used.
  5. Describe your final image and how it was created. Point out any programming structures like if statements or for loops that helped you create it. My final image contains two of my buildings created in a previous project, four trees slightly modified from previous projects, and a sun in the sky. I created my buildings and trees like I did in my last project, but before drawing the trees and the sun (which use the crayon effect) I called turtle.crayon(True) so that the crayon was used instead of the default computer line. I used the list and for loop from last week's project to create my trees and I also made a new for loop using range to create my sun. Here is my scene:

  6. A description of any extensions you did Extension 4: I made a crayon circle so that I could put a sun in the sky of my scene. I did this by creating a polygon instead of a circle so that my edges wouldn't be perfectly curved. I made the polygon by using a for loop using range(20) and then having my turtle go forward(10) and turn 18 degrees 20 times. I also changed the color of the crayon to yellow.