Introduction
This assignment lets you practice with creating and using a new Turtle object. This lets you practice creating objects that inherit methods and attributes from other objects.
Preparation
Download the program StampTurtle.pyDownload StampTurtle.py. Be sure to run the program before you make any changes. If you run into any errors, make sure you are using the latest version of your python compiler/IDE. Or ask the instructor to enable this Assignment in the textbook. Look at what the program does and look at the code to get an idea of the program structure.
Directions
You will complete the class StampTurtle as part of this assignment.
The program currently creates several Turtles as you have done all semester. Now change the line that creates new Turtles to
t = StampTurtle()
which will create a bunch of StampTurtles instead. Run your program again. It should behave exactly as it did before, because your StampTurtles are exactly like regular turtles. We haven’t changed a thing yet, only created a new type of Turtle.
Add a new method to your StampTurtle class. Use the code exactly as shown below. This makes your StampTurtle do something different from a regular Turtle. It draws a stamp every time it stops going forward, as a marker of where it has been.
def forward(self, dist):
super().forward(dist)
self.stamp()
Run your program again. Don’t change anything in the main function. If there are errors, they must be in your class, not the previously working code. Your program should display lines with stamps along them. Congrats, you have created your own type of Turtle, with very little extra work.
Because your StampTurtle is a Turtle, you can have StampTurtles and Turtles both in the same program. Create one or more additional Turtles, change their direction, and add them to the list turtle_list. Run your program again. Some of the lines are stamped, some aren’t. This works as long as you only call Turtle methods, like forward or backward. If you add a method with a different name to your class, then you can’t easily mix the different objects.
Try writing your own method. First, change the last line of main() to call t.backward instead of t.forward . Run your program. Since your StampTurtle doesn’t have a backward method (yet!), it just uses the one from Turtle, and all Turtles and StampTurtles draw the same type of line.
Now, write a new method backward in your StampTurtle class. Make sure you call super().backward as the first line of your new function, similar to what forward does. But in addition to drawing a stamp, have the StampTurtle change colors after every move. You want to pick a random color, similar to what is done in main(). Now the StampTurtles again behave differently from the regular Turtles.
When you create a StampTurtle you are using inheritance, so you get all the methods and attributes of a Turtle without having to write any Turtle code yourself. When you create a StampTurtle method forward or backward, you are using polymorphism, where your StampTurtle behaves differently than a regular Turtle. These are important concepts that you will explore more of in later programming courses.
Submission
Submit the file with .py extension.
Write a reflection in the comment box answering the following questions:
In a sentence or two, what did you learn?
In a sentence or two, what did you like about this project?
In a sentence or two, what did you find confusing or would like to see done differently regarding this project?
In a sentence or two, if you had another hour or two, what would you like to add to the project or how would you do things differently?
What do you call a bunch of StampTurtles moving around the screen?