2
Picture of what the code does with the Top Hat我正在使用面向對象編程的項目,我們的任務是繪製一個雪人,主要是形狀的類。我把整個雪人抽出來,但是當試圖填充正方形時,它會填滿一個三角形,而不是出於某種原因。填寫由列表繪製的形狀
import turtle
class Shape(object):
def __init__(self, pensize = 1, pencolor = "black"):
self.pensize = pensize
self.pencolor = pencolor
class Line(Shape):
def __init__(self, start = (0.0, 0.0), end = (50.0, 0.0),
pencolor = "black", pensize = 1):
Shape.__init__(self,pensize,pencolor)
self.start = start
self.end = end
def __str__(self):
return "Line(beg:{},end:{})".format(self.start ,self.end)
def draw(self, pen):
"""draw the line using the provided pen"""
pen.pensize(self.pensize)
pen.pencolor(self.pencolor)
pen.up()
pen.goto(self.start)
pen.down()
pen.goto(self.end)
class Square(object):
def __init__(self, pos = (0.0,0.0), size = 50):
self.pos = pos
x ,y = pos
self.size = size
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
def __str__(self):
return "Square(pos:{},size:{})".format(self.pos, self.size)
def draw(self, pen):
for l in self.side_lines:
l.draw(pen)
class Snow_man(Shape):
def __init__(self, pos = (0.0, 0.0)):
self.pos= pos
def __str__(self):
return "Drawing a Snowman"
def draw(self, pen):
brim = Line((-400, 300), (-200, 300), "black", 5)
brim.draw(pen)
pen.color("black")
pen.begin_fill()
topHat = Square((-360, 300), 120)
topHat.draw(pen)
pen.end_fill()
pen= turtle.Turtle()
snowman= Snow_man()
snowman.draw(pen)
turtle.exitonclick()