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()
答案 0 :(得分:0)
首先,当您想要在广场的角落调用begin_fill()时,从乌龟的当前位置调用begin_fill(),这是帽檐的末端。
因此在使用begin_fill()之前使用pen.goto(-360,300)之类的东西会将乌龟带到起始位置。然后你调用begin_fill()。
接下来是你如何绘制线条,如果线条是从一个角落到另一个角落而不是仅使用fd()和right()命令作为一个简单的例子来回传递会更好。
being_fill()
fd(50)
right(90)
fd(50)
right(90)
fd(50)
right(90)
fd(50)
end_fill()
将填补一个正方形。所以我认为你必须重新调整你在side_lines中绘制的线条。喜欢从哪个坐标开始等等。