CSCI 110, Spring 2011
Home | | Course Schedule | | Assignments | | Lecture NotesLaboratory 6
Problem 1.
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:
Due at the end of today's class
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:
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).
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:
Methods (functions):
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.