我的乌龟蟒有什么问题

时间:2018-02-26 20:14:04

标签: python python-3.x turtle-graphics

我在python中编写了这段代码来学习多态性。我创建了一个文件paint2.txt,里面有一些海龟命令,如下所示(数字表示笔的宽度,圆半径和x,y坐标为goto):

pendown
beginfill 
black
circle
20 
3 
black 
endfill
goto
120 
0 
1 
red
beginfill
green
circle
20
3
yellow
endfill
penup

并且此代码从文件中读取这些命令并尝试解析并运行它们:

import turtle
class GoToCommand:
    def __init__(self, x, y, width=1, color='balck'):
        self.x = x
        self.y = y
        self.width = width
        self.color = color

    def draw(self,turtle1):
        turtle1.width(self.width)
        turtle1.pencolor(self.color)
        turtle1.goto(self.x,self.y)

class CircleCommand:
    def __init__(self, radius, width=1, color='black'):
        self.radius = radius
        self.width = width
        self.color = color

    def draw(self, turtle1):
        turtle1.pencolor = self.color
        turtle1.width = self.width
        turtle1.circle(self.radius)

class BeginFillCommand:

    def __init__(self,color='black'):
        self.color = color
    def draw(self,turtle1):
        turtle1.fillcolor = self.color
        turtle1.begin_fill()

class EndFillCommand:
    def __init__(self):
        pass
    def draw(self,turtle1):
        turtle1.end_fill()

class PenUpCommand:
    def __init__(self):
        pass
    def draw(self,turtle1):
        turtle1.penup()

class PenDownCommand:
    def __init__(self):
        pass
    def draw(self,turtle1):
        turtle1.pendown()


class PyList:
    def __init__(self):
        self.items=[]
    def append(self,item):
        self.items = self.items + [item]
    def __iter__(self):
        for c in self.items:
            yield c
def main():

    t = turtle.Turtle()
    screen = t.getscreen()

    GraphicsCommands = PyList()

    # paint2.txt is my made up file with turtle commands 
    file = open ('paint2.txt','r')
    line = file.readline()

    while line != '':
        line = line.strip()

        if line == 'goto':
            x = int(file.readline().strip())
            y = int(file.readline().strip())
            width = int(file.readline().strip())
            color = file.readline().strip()
            cmd = GoToCommand(x,y,width,color)

        elif line == 'beginfill':
            color = file.readline().strip()
            cmd = BeginFillCommand(color)
        elif line == 'pendown':
            cmd = PenDownCommand()

        elif line == 'penup':
            cmd = PenUpCommand()

        elif line == 'circle':
            radius = int(file.readline().strip())
            width = int(file.readline().strip())
            color = file.readline().strip()
            cmd = CircleCommand(radius,width,color)

        elif line == 'endfill':
            cmd = EndFillCommand()

        else:
            raise RuntimeError("Unkonwn command :", line)
        GraphicsCommands.append(cmd)
        line = file.readline()

    file.close()
    for cmd in GraphicsCommands:
        cmd.draw(t)
    t.ht()
    turtle.done()


if __name__ == '__main__':
    main()

这是错误:

Traceback (most recent call last):
  File "/Users/amin/PycharmProjects/comeon/com.py", line 110, in <module>
    main()

  File "/Users/amin/PycharmProjects/comeon/com.py", line 104, in main
    cmd.draw(t)

  File "/Users/amin/PycharmProjects/comeon/com.py", line 10, in draw
    turtle1.width(self.width)

TypeError: 'int' object is not callable

0 个答案:

没有答案