Python3 Please read the following pages. Please take a screen shot of the code a
ID: 3706904 • Letter: P
Question
Python3
Please read the following pages. Please take a screen shot of the code and copy and paste the code also. Thank you.
Problem C: Caterpillars, Classes, and Objects oh my! (10 points): In this problem you will be creating a Caterpillar class that will draw a caterpillar using turtle graphics. Your caterpillar object will contain the following information: 1. Body color (default-"green") 2. Legs color (default-"purple") 3. Body size (the radius of the 5 circles that make the caterpillars body) (default 50) You are going to want to create a turtle object that you will use to draw the caterpillar in the constructor, but it is not a parameter required to create a caterpillar. Your Caterpillar class must include a display function that will be where your caterpillar is called (display can call helper functions if you choose to split up the different parts you need to draw). Here is a sample caterpillar with all of the defaults (this picture is smaller than what it would look like in the turtle window): You must create a Caterpillar class ( named Caterpillar) and include a display function (named display) to draw the caterpillar . Your display function should call 3 helper function (these will be extremely helpful in problem D) o draw_body0 this function should draw the body of the Caterpillar (the 5 green overlapping circles) » draw antennae() this function should draw the antennae on the Calkrpillars hcadExplanation / Answer
import turtle
class Caterpillar:
def __init__(self, body_color='green', legs_color='purple', body_size=50):
self.body_color = body_color
self.legs_color = legs_color
self.body_size = body_size
self.caterpillar = turtle.Turtle()
self.caterpillar.speed(0)
def display(self):
self.draw_body()
self.draw_antennae()
self.draw_legs()
self.caterpillar.hideturtle()
turtle.done()
def draw_body(self):
x, y = 0, -80
for i in range(5):
self.caterpillar.penup()
self.caterpillar.setposition(x, y)
self.caterpillar.pendown()
self.caterpillar.color(self.body_color)
self.caterpillar.begin_fill()
self.caterpillar.circle(self.body_size)
self.caterpillar.end_fill()
y += self.body_size
def draw_antennae(self):
self.caterpillar.color(self.legs_color)
x, y = self.caterpillar.position()
self.caterpillar.penup()
self.caterpillar.left(90)
self.caterpillar.forward(self.body_size)
self.caterpillar.left(45)
self.caterpillar.forward(self.body_size)
self.caterpillar.pendown()
self.caterpillar.forward(self.body_size)
self.caterpillar.penup()
self.caterpillar.backward(2 * self.body_size)
self.caterpillar.right(90)
self.caterpillar.forward(self.body_size)
self.caterpillar.pendown()
self.caterpillar.forward(self.body_size)
def draw_legs(self):
self.caterpillar.penup()
x, y = 0, 4 * self.body_size - 80
for i in range(4):
self.caterpillar.setposition(x, y)
self.caterpillar.left(135)
self.caterpillar.forward(self.body_size)
self.caterpillar.pendown()
self.caterpillar.forward(self.body_size)
self.caterpillar.left(45)
self.caterpillar.forward(self.body_size)
self.caterpillar.penup()
self.caterpillar.backward(self.body_size)
self.caterpillar.left(135)
self.caterpillar.forward(3 * self.body_size)
self.caterpillar.pendown()
self.caterpillar.forward(self.body_size)
self.caterpillar.right(45)
self.caterpillar.forward(self.body_size)
self.caterpillar.penup()
self.caterpillar.backward(self.body_size)
self.caterpillar.left(90)
y -= self.body_size
c = Caterpillar()
# c = Caterpillar("red", "blue", 32)
c.display()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.