Question: write the code based on the starting code using the instructions starting code: # Draws several lines radiating out from the origin. import turtle import

write the code based on the starting code using the instructions
starting code:
# Draws several lines radiating out from the origin.
import turtle
import random
class StampTurtle(turtle.Turtle):
# Constructor - just copy and use for now
def __init__(self):
super().__init__()
def main():
# named constants
screen_size =500
screen_startx =60 # x coordinate of the left edge of the graphics window
# Set up the window and its attributes
wn = turtle.Screen()
wn.setup(screen_size, screen_size, screen_startx, 0)
turtle_list =[]
num_turtles =2
num_stamp_turtles =3
# Create a list of different colored turtles, each pointing
# in a different direction
for _ in range(num_turtles):
t = turtle.Turtle()
t.right(random.randrange(350))
t.color(random.choice(['red','green','blue','yellow']))
#t.speed(7)
turtle_list.append(t)
# Create a list of different colored stamp_turtles, each pointing
# in a different direction
'''
for _ in range(num_stamp_turtles):
t = StampTurtle()
t.right(random.randrange(350))
t.color('black')
#t.speed(7)
turtle_list.append(t)
'''
# Move each turtle outward from the origin ten times
for t in turtle_list:
for _ in range(10):
t.forward(random.randrange(10,30))
wn.exitonclick()
# 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 range(num_stamp_turtles):
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 forward(self, dist):
self.right(360)
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 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 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, 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 copy/paste 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!