CSCI 110, Spring 2011

    Home | | Course Schedule | | Assignments | | Lecture Notes

    Laboratory 6
    Due at the end of today's class

    Problem 1.
    In this problem, you will use the turtle object to draw a self-similar object, the Sierpinski gasket. This is a pattern that looks the same at different levels of magnification. The following picture shows a Sierpinski gasket with three levels:

    Notice that the Sierpinski gasket is composed of 3 smaller versions of the Sierpinski gasket, one in each corner of the original triangle. Each of these smaller versions is half the size of the original, and made of up one less level. Thus, this is a problem that can be solved using recursion. To draw the sierpinski gasket, we will simply call the sierpinski gasket program three times after positioning the turtle correctly to draw the smaller gaskets in the proper places.

    In order to draw the triangle, you will write a recursive function:

      sierpinski(myTurtle, size, level)
    that will call itself to draw the smaller versions of the gasket. Follow these steps to write your function. Note that each step involves exactly one program statement (or function call).

    • Write a conditional to check for the base case, when the level is less than or equal to zero.
    • In the base case, the function should draw a triangle with side length given by size. (You should use the drawTriangle( ) function given in the skeleton code below).
    • The general case, written in the else clause, should do the following:
    • Draw a sierpinski gasket of half the size and with one less level from the current turtle position. (Use recursion to do this with one function call!)
    • Move the turtle forward by half the side length.
    • Draw a sierpinski gasket of half the size and one less level from the current position.
    • Move the turtle backward by half the side length.
    • Turn the turtle left by 60 deg
    • Move the turtle forward by half a side length
    • Turn the turtle right by 60 deg (this places the turtle halfway up the right side of the gasket and facing right.
    • Draw a sierpinski gasket of half the size and one less level from the current position.
    • Turn the turtle left by 60 deg
    • Move the turtle backward by half a side length
    • Turn the turtle right by 60 deg.
    You may use the following skeleton code for your program:
      #Program: sierpinski.py
      #Your name and program prologue go here
      
      from turtle import Pen as Turtle
      
      #Function to draw a triangle, used in the sierpinski function
      def drawTriangle(myTurtle, size):
          myTurtle.forward(size)
          myTurtle.left(120)
          myTurtle.forward(size)
          myTurtle.left(120)
          myTurtle.forward(size)
          myTurtle.left(120)
          
      def sierpinski(myTurtle, size, level):
      	#Your definition of the sierpinski() function goes here
      
      #Begin main program
      yertle = Turtle( )
      sierpinski(yertle, 100, 3)
      


    Problem 2.
    In this problem you will create the Hero class. A hero is an object that has a name and some number of health points. This class would be useful if you were developing an adventure game in which a hero journeys through a world collecting objects and fighting monsters.

    Creating the class.
    Create the Hero class with the following properties and methods:

    Properties:

    • An integer called health. It should be initially set to 10.
    • A string called name. This will be a parameter of the constructor function, set when the hero object is created.

    Methods (functions):

    • def __init__(self, heroName)
      This method should set a property, called name, to the value of the parameter name.
      self.name = heroName
    • def __str__(self)
      This should return a string, "I am the hero, " plus the hero's name, e.g. "I am the hero, Lancelot"
    • def attack_troll(self)
      This should print that the hero has attacked a troll, e.g. "Lancelot has attacked a troll."
      It should subtract 2 from the hero's health.
      It should print the new health value, e.g. "Lancelot now has 8 health points."

    Part 1:
    Start by defining the Hero class with the health property set to 10.
    Add the __init__(self, name) method and the __str__(self) method.

    Use the following template to test your class so far:

    #Program: heroClass.py
    #Your program prologue goes here
    
    class Hero:
    	#Add your properties and methods here (don't forget to indent)
    
    
    
    #The main program starts here (to test the class)
    heroName = raw_input("Please enter the hero's name: ")
    myHero = Hero(heroName)
    print myHero
    

    Sample output: The output should look as follows (user input is underlined):

    Please enter the hero's name: Fred
    I am the hero, Fred
    

    Part 2:
    Add the definition of attack_troll(self). Add this after the definition of __str__(self) but before the main program.

    Change your main program to test these new methods as follows:

    theName = raw_input("Please enter the hero's name: ")
    myHero = Hero(theName)
    print myHero
    myHero.attack_troll( )
    

    Sample output: The output should look as follows (user input is underlined):

    Please enter the hero's name: Mario
    I am the hero, Mario
    Mario has attacked a troll.
    Mario now has 8 health points.
    

    What To Turn In.
    A printed listing of the sierpinski.py and the heroClass.py programs. Be sure that your name is in the program prologue's comment section of the program. Don't forget to save a copy of each of your programs on your P:\ drive.