NEED ASAP please! PYTHON please. Thank you! In this part, you are going to use t
ID: 3689402 • Letter: N
Question
NEED ASAP please! PYTHON please. Thank you!
In this part, you are going to use turtle graphics to draw two “snow persons.” You will define and use at least 7 classes in doing so. Using instances of these classes, you will write a main() function that creates and draws two snow people.
Project Description / Specification
1.Define a minimum of 7 classes. Most can be classes for geometric shapes (e.g., Line, Rectangle, Circle, Triangle), but 3 must be “drawing-specific” classes (see #5).
2.Each class will have at least the following methods:
a.: the constructor will take arguments indicating the positions (coordinate pairs, e.g., (0.0, 0.0)) of one or more reference points (e.g., start and end points for a line, vertices for a triangle, etc); and other arguments appropriate for an instance (e.g., pen color, fill color, etc.). To simplify creating instances, it will define appropriate default values for most of the arguments. (If the argument for fill color is the empty string (), the shape is not filled.)
b. :a conversion method, it returns the string to be used for printing an instance in Python.
c. :the draw method will take a object to use for drawing the shape.
d.Other arguments may be required for your methods; all arguments will be described in your docstrings.
3.All classes, methods and functions require a docstring for a general description of the object/method/function.(see DocStrings note below)
4.Define a main function that is called without any arguments. Your main function should create and draw two snow people at different positions on the canvas.
5.Define a drawing-specific class hierarchy containing (at least) 3 classes:
a. : an instance contains (at least) the 3 snowballs
b. : specializes a Snow_person with a handful of additional components (e.g., arms, buttons, head wear, two eyes and a mouth.)
c. : specializes a Snow_person with an alternate set of additional components.
6.At a minimum, a snow person should include three snowballs, drawn one on top of the other. Feel free to personalize your classes to include other components.
Deliverables
Turn in ExtraCredit2.py containing all of your class and function definitions.
Project Notes:
The idea is to make classes for geometric objects that you will need in drawing snow people. These objects store information about how they are to be drawn (position, pen and fill color, and such). Invoking the draw function on an object draws it on the canvas at the indicated position and using the indicated colors.
You can use the class definitions you defined in part 2 of the project (not a requirement). If you do, you will need to write at least another 3 classes.
The screen shot below was created by a program that meets the specifications for this project. It shows an instance of a Snow_man class (left) and an instance of a Snow_lady class (right).
Explanation / Answer
import math
import turtle
import time
# This function draws my 3 snow balls one on top of each other.
class Snow_person():
"""Line class"""
# I set the cirlce/ position of the cricle to draw in the center at 0.
def __init__(self, pos = (0, 0.0)):
"""
create a line starting from the coordinates given by beg to
the coordinates given by end
"""
self.pos = pos
def __str__(self):
""" a conversion method, it returns the string to be used for
printing an instance in python
"""
return "%s(%s,%s)" % (self.tag,self.beg,self.end)
def lines(self,pen):
lines = [Line((100,0),(40,20)), Line((0,20), (40,0), "red")]
print(lines)
for line in lines:
print (line, end=" ")
line.draw(pen)
print()
def draw(self,pen):
x,y = self.pos
"""draw the line using the provided pen"""
# c is my first circle.
c = Circle((self.pos[0],100),40,fillcolor = 'white', pencolor = 'black')
c.draw(pen)
print(c)
# c2 is my second circle bellow.
c2 = Circle((self.pos[0],-20),60,fillcolor = 'white', pencolor = 'black')
c2.draw(pen)
print(c2)
# c3 is my thrid circle at the bottom
c3= Circle((self.pos[0],-180),80,fillcolor = 'white', pencolor = 'black')
c3.draw(pen)
print(c3)
pen.up()
pen.goto((x-20),(y+155))
c = Circle(((x-20),(y+130)),7,fillcolor = 'red', pencolor = 'red')
c.draw(pen)
pen.up()
pen.goto((x+20),(y+155))
c = Circle(((x+20),(y+130)),7,fillcolor = 'red', pencolor = 'red')
c.draw(pen)
pen.up()
r = Rectangle(((x-10),(y+115)),((x+10),(y+115)), ((x+10),(y+120)),((x-10),(y+120)))
r.draw(pen)
# This function draws the snow lady with a ribbon on top of her head.
class Snow_lady(Snow_person):
def __init__(self, pos = (0, 0.0)):
"""
create a line starting from the coordinates given by beg to
the coordinates given by end
"""
self.pos = pos
def __str__(self):
""" a conversion method, it returns the string to be used for
printing an instance in python
"""
return "%s(%s,%s)" % (self.tag,self.beg,self.end)
def draw(self,pen):
Snow_person.draw(self,pen)
rect = Rectangle((-37.5,217.5), (-37.5,167.5), (32.5,217.5), (32.5,167.5))
rect.draw(pen)
# This function draws the snow man with a hat on top of his head.
class Snow_man(Snow_person):
def __init__(self, pos = (200,0)):
self.pos = pos
def __str__(self):
""" a conversion method, it returns the string to be used for
printing an instance in python
"""
return "%s(%s,%s)" % (self.tag,self.beg,self.end)
def draw(self,pen):
Snow_person.draw(self,pen)
rect = Rectangle((172.5,170),(222.5,170),(222.5,220),(172.5,220))
rect.draw(pen)
# This function is used to draw my lines for both the snow man and snow woman.
class Line(object):
"""Line class"""
def __init__(self, beg = (0.0, 0.0), end = (50.0, 0.0),
pencolor = "black"):
"""
create a line starting from the coordinates given by beg to
the coordinates given by end
"""
self.pencolor = pencolor
self.beg = beg
self.end = end
self.tag = "Line"
def __str__(self):
""" a conversion method, it returns the string to be used for
printing an instance in python
"""
return "%s(%s,%s)" % (self.tag,self.beg,self.end)
def draw(self,pen):
"""draw the line using the provided pen"""
pen.pencolor(self.pencolor)
if pen.pos() != self.beg:
pen.up()
pen.goto(self.beg)
pen.down()
pen.goto(self.end)
# This function is used to better align the angles and dimenssions for my shapes being drawn.
class Polygon(object):
"""Polygon class"""
def __init__(self, vertices = [],
fillcolor = "", pencolor = "black"):
"""
create a polygon from a list of its vertices
"""
self.vertices = vertices
self.pencolor, self.fillcolor = pencolor, fillcolor
self.tag = "Poly"
def __str__(self):
''' a conversion method, it returns the string to be used for printing an instance in python '''
vertexStr = ",".join([str(v) for v in self.vertices])
return "%s(%s)" % (self.tag,vertexStr)
def draw(self, pen):
"""draw the line using the provided pen"""
lines = []
vertices = self.vertices
print(vertices)
if vertices:
for i in range(len(vertices)-1):
lines.append(Line(vertices[i],vertices[i+1], self.pencolor))
lines.append(Line(vertices[-1],vertices[0]))
pen.color(self.pencolor, self.fillcolor)
if self.fillcolor: pen.begin_fill()
for l in lines:
l.draw(pen)
pen.end_fill()
# This function is used to draw my triangle for the snow woman.
class Triangle(Polygon):
def __init__(self, coord1 = (0.0,0.0), coord2 = (100.0,0.0),
coord3 = (50.0,50.0), fillcolor = '', pencolor = 'black'):
Polygon.__init__(self, [coord1,coord2,coord3], fillcolor, pencolor)
self.tag = "Triangle"
def __str__(self):
vertexStr = ",".join([str(v) for v in self.vertices])
return "%s(%s)" % (self.tag,vertexStr)
def draw(self,pen):
lines = []
vertices = self.vertices
print(vertices)
if vertices:
for i in range(len(vertices)-1):
lines.append(Line(vertices[i],vertices[i+1],self.pencolor))
lines.append(Line(vertices[-1],vertices[0]))
pen.color(self.pencolor, self.fillcolor)
if self.fillcolor: pen.begin_fill()
for l in lines:
l.draw(pen)
pen.end_fill()
# This function is used to draw my rectangle for the snow woman.
class Rectangle(Polygon):
def __init__(self, cd1 = (0,0), cd2 = (20,0), cd3 = (20,30), cd4 = (0,30), fillcolor = 'green', pencolor = 'black'):
Polygon.__init__(self,[cd1,cd2,cd3,cd4],fillcolor,pencolor)
self.tag = 'Rect'
def __str__(self):
''' a conversion method, it returns the string to be used for printing an instance in python '''
vertexStr = ",".join([str(v) for v in self.vertices])
return "%s(%s)" % (self.tag,vertexStr)
def draw(self, pen):
"""draw the line using the provided pen"""
lines = []
vertices = self.vertices
print(vertices)
if vertices:
for i in range(len(vertices)-1):
lines.append(Line(vertices[i],vertices[i+1], self.pencolor))
lines.append(Line(vertices[-1],vertices[0]))
pen.color(self.pencolor, self.fillcolor)
if self.fillcolor: pen.begin_fill()
for l in lines:
l.draw(pen)
pen.end_fill()
# The circle function is used to draw my circles.
class Circle(object):
def __init__(self,center = (0,0),radius = 0, fillcolor = 'white', pencolor = 'black'):
'''This is the constructor that takes arguments indicating the positions of one or more reference points
and other arguments appropriate that will define appropriate default values for most of the arguments.'''
self.center = center
self.radius = radius
self.pencolor, self.fillcolor = pencolor, fillcolor
self.tag = "Circle"
def __str__(self):
return "%s(%s,%s)" % (self.tag,self.center,self.radius)
def draw(self,pen):
pen.color(self.pencolor, self.fillcolor)
if self.fillcolor:
pen.begin_fill()
print(self.center)
pen.up()
pen.goto(self.center)
pen.down()
pen.circle(self.radius)
pen.end_fill()
# The main function is used to run my whole program which then prints out the
# coordinates where my snow person and man are drawn.
def main():
pen = turtle.Turtle()
pen.speed(60)
snowperson = Snow_man()
snowperson.draw(pen)
snowperson2 = Snow_lady()
snowperson2.draw(pen)
main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.