Question: starting code: # Draws several lines radiating out from the origin. import turtle import random class StampTurtle ( turtle . Turtle ) : # Constructor
starting code:
# Draws several lines radiating out from the origin.
import turtle
import random
class StampTurtleturtleTurtle:
# Constructor just copy and use for now
def initself:
superinit
def main:
# named constants
screensize
screenstartx # x coordinate of the left edge of the graphics window
# Set up the window and its attributes
wn turtle.Screen
wnsetupscreensize, screensize, screenstartx,
turtlelist
numturtles
numstampturtles
# Create a list of different colored turtles, each pointing
# in a different direction
for in rangenumturtles:
t turtle.Turtle
trightrandomrandrange
tcolorrandomchoicered'green','blue','yellow'
#tspeed
turtlelist.appendt
# Create a list of different colored stampturtles, each pointing
# in a different direction
for in rangenumstampturtles:
t StampTurtle
trightrandomrandrange
tcolorblack
#tspeed
turtlelist.appendt
# Move each turtle outward from the origin ten times
for t in turtlelist:
for in range:
tforwardrandomrandrange
wnexitonclick
# Run the main function. This should be the last statement in the file.
main
task:
You will complete the class StampTurtle as part of this assignment.
Uncomment the section of code that starts with
for in rangenumstampturtles:
t StampTurtle
This will create bunch of StampTurtles and add them to your list of Turtles. Run your program again. It should behave exactly as it did before, but with more turtles of different colors. So far, 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 spins around and draws a stamp every time it stops going forward, as a marker of where it has been.
def forwardself dist:
self.right
superforwarddist
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 for the StampTurtles. Congrats, you have created a new type of Turtle that behaves a little differently and you did this with very little extra work. 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 StampTurtle, then you can't easily mix the different objects.
Try writing your own method. First, change the last line of main to call tbackward instead of tforward 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 superbackward as the first line of your new function, passing it a parameter, similar to what the forward method 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 but putting that code in the backward function. 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.
Create your own type of Turtle and add at least two to the list. Your turtles should have a forward and backward function, each of which does something different from the functions already shown in this assignment, such as changing pen size, speed, shape of the stamp, drawing dotted lines, etc. The easiest way is to copypaste the entire StampTurtle class and then edit what you just pasted, specifically changing the name of the class.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
