PLEASE HELP ASAP!! PYTHON please. Thank you!! The Problem This part of project w
ID: 3689398 • Letter: P
Question
PLEASE HELP ASAP!! PYTHON please. Thank you!!
The Problem
This part of project will give you more experience with defining classes and using inheritance. In this part, you are going to use turtle graphics to define some elementary shapes that know how to draw themselves. You are going to create at least 3 classes. Using instances of these classes, you are going to draw a simple picture.
Your Task
Copy the file Project2_2.py (included with this document) to your directory. To get you started, it contains a definition for a Line class and a Polygon class. It also contains a main function, which creates a couple of lines, a simple polygon, and a pen (a turtle.Turtle object). It then uses the pen to draw the shapes.
You will define 3 additional classes: A Triangle class, a Rectangle class, and a Circle class.
Instances of each class respond to at least the following methods:
__init__ : create an instance of the class from the following:
i.For a Triangle: three pairs of coordinates, which indicate the three vertices (not a list, but three arguments, which you may give some default values for easy testing)
ii.For a Rectangle: two pairs of coordinates—one giving the coordinates of the lower left corner and the other giving the coordinates of the top right corner (not a list, but two arguments).
iii.For a Circle: a coordinate pair giving the center of the circle and a float giving the radius
iv.For all, a string indicating a fill color (default value “”, the empty string)
v.For all, a string indicating the line color (default value “black”)
__str__ : conversion to a string; returns the string to be used for printing in the Python shell.
draw: will take a turtle.Turtle object and will use it for drawing the shape;
All classes, methods and functions require a docstring for a general description of the object/method/function. (see docstring note below)
Triangle and Rectangle should both inherit from Polygon and reuse what they can from Polygon.
Once you have tested your class definitions, draw a simple picture using them. For instance, you could draw a house using rectangles, triangles, circles and lines. The only requirement is that it should be made up of multiple shapes.
Deliverables:
Your class definitions (Project2_2.py file)
In a word document, explain your class hierarchy and what benefits you have gotten from reuse.
Include the picture generated by your code in the same word document.
Using turtle graphics: In order to use turtle graphics in Python you must first import the turtle module. You can then use the help function in idle to find out what methods this module includes and what they do. Just type import turtle in the idle command window, hit enter, and then type help(turtle) and scroll up through the list and information. Assuming the assignment pen = turtle.Turtle(), some useful features include,:
pen.up(),pen.down(): Set the pen state to be up (not drawing) or down (drawing)
pen.right(degrees), pen.left(degrees): Turn the direction that the pen is facing. The amount of turn is indicated in degrees.
pen.forward(distance), pen.backward(distance): Move the pen forward or backward the amount of distance indicated. Depends on the direction the pen is facing. Draws a line if the pen is down, not if the pen is up.
pen.goto(x,y): Move the pen to the specified point, drawing a line along the way if the pen is down, and not drawing if the pen is up.
pen.pencolor(r,g,b), pen.pencolor(s): Set the color that the pen will hold for all drawing until the pen color is changed. In the first version, each argument is a floating point number between 0.0 - 1.0; the first is the amount of red, the second, the amount of green and the third the amount of blue. In the second version, the argument is a string indicating a color by name or by its hex code, e.g., “green”, “red”, “#66FFFF”. Hex color codes are at:
www.web-source.net/216_color_chart.htm
pen.fillcolor(r, g, b), pen.fillcolor(s): Set the color for filling figures. Arguments are the same as for the pen color.
pen.circle(radius): draw a circle of the indicated radius. The circle is drawn tangent to the direction of the pen in a clockwise direction (if radius is positive).
pen.write(string): Write a string starting at the present pen point.
To fill a figure, use the command pen.begin_fill() before you start drawing it. Draw the figure, then execute the command pen.end_fill(). The figure drawn between the two fill commands will be filled with the present color setting.
pen.clear(): Clear (erase) everything written by the pen.
The picture below illustrates a house scene that could be drawn using your classes.
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() ##class Rectangle(Polygon): ## def __init__(self, vertices = [], fillcolor = '', 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.vertices = vertices ## self.pencolor, self.fillcolor = pencolor, fillcolor ## Polygon.__init__(self, self.vertices, self.pencolor, self.fillcolor) ## self.tag = "Rectangle" ## 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 = [] ## vertice = self.vertices ## print(vertice) ## newlist=[] ## for i in vertice: ## for number in i: ## n=int(number) ## newlist.append(n) ## print(newlist) ## ## vertices=[(int(newlist[0]),int(newlist[1])),(int(newlist[0]),int(newlist[3])),(int(newlist[1]),int(newlist[3])),(int(newlist[1]),int(newlist[2]))] ## 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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.