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()