2017-04-21 60 views
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() 

回答

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上重新調整你的線條。像它座標的起始位置等。