2017-04-21 6 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のあなたのラインの描画を再調整しなければならないと思う。どのように開始座標を調整するなど

関連する問題