import turtle #General Shape class - base class for other shapes class Shape(object): #Constructor for Shape class #Parameters: pencolor - color of the pen to use for outlining # pensize - size (width) of the pen to use #Returns: None, post-condition: instance variables for class will be set with initial values def __init__(self, pencolor = "black", pensize = 1): self.pencolor = pencolor self.pensize = pensize #Line class - class used to create and draw a line object class Line(Shape): # Constructor for Line class #Parameters: beg - coordinates of the beginning point # end - coordinates of the ending point # pencolor - color of the line # pensize - size (width) of the line #Returns: None, post-condition: instance variables for class will be set with initial values def __init__(self, beg = (0.0, 0.0), end = (50.0, 0.0), pencolor = "black", pensize = 1): Shape.__init__(self,pencolor,pensize) self.beg = beg self.end = end #Overloading the str method #Parameters: None #Returns: a nicely formatted string with start and end points def __str__(self): return "Line(beg:{},end:{})".format(self.beg,self.end) #Method used for drawing a Line using Turtle #Parameter: pen - an object of type Turtle #Returns: None def draw(self, pen): pen.pensize(self.pensize) pen.pencolor(self.pencolor) pen.up() pen.goto(self.beg) pen.down() pen.goto(self.end) #3-D box class class Box(object): #Constructor for a 3-D box #Parameters: pos - reference position (lower left corner of the box) # size - length of a side of the box #Returns: None def __init__(self, pos = (0.0,0.0), size = 50): self.pos = pos x,y = pos self.size = size offset = size/3.0 # off set to use for back face # an instance contains 9 lines, comments indicate: face, edge self.side_lines = [Line((x,y),(x,y+size)), # front, left Line((x,y),(x+size,y)), # front, bottom Line((x+size,y),(x+size,y+size)), # front, right Line((x,y+size),(x+size,y+size)), # front, top Line((x,y+size),(x+offset,y+size+offset)), #top, left Line((x+offset,y+size+offset),(x+size+offset,y+size+offset)), #top, back Line((x+size,y+size),(x+size+offset,y+size+offset)), #top, right Line((x+size,y),(x+size+offset,y+offset)), # right, bottom Line((x+size+offset,y+offset),(x+size+offset,y+size+offset)) # right, back ] #Overloading the str method #Parameters: None #Returns: a nicely formatted string with position and size def __str__(self): return "Box(pos:{},size:{})".format(self.pos,self.size) #Method used for drawing a Line using Turtle #Parameter: pen - an object of type Turtle #Returns: None def draw(self,pen): for l in self.side_lines: l.draw(pen) def main(): #create an object of type Turtle pen = turtle.Turtle() #create a new box, print out information about the box and then draw it using the turtle box1 = Box() print(box1) box1.draw(pen) #create a second box, print out information about the box and then draw it using the turtle box2 = Box((80.0,0.0), 60) print(box2) box2.draw(pen) #create a line, print out information about the line and then draw it using the turtle line = Line((-100,-100),(-100,100),"purple", 3) print(line) line.draw(pen) #command makes the turtle icon disappear from the screen pen.hideturtle() #The turtle screen will close when you click on it. turtle.exitonclick() main()