1

我有这段代码,它读取然后解释一个分形描述语言文件,然后使用分形世界,它是 TurtleWorld 的一个特殊版本,它只是删除了海龟,没有延迟。

import math, random, sys
from FractalWorld import *

world = FractalWorld(width=1000,height=1000,delay=0)
Rasmus = Fractal(draw=False)

class command(object):
    def __init__(self, cmd, l):
            self.cmd = cmd
            self.l = l

    def execute(self, turtle, length):
            if self.cmd=='lt':
                    lt(turtle, int(self.l[0]))
            if self.cmd=='rt':
                    rt(turtle, int(self.l[0]))
            if self.cmd=='fd':
                    fd(turtle, length)
            if self.cmd=='bk':
                    bk(turtle, length)
            if self.cmd=='scale':
                    lenght = length*float(self.l[0])

class rule(object):
    def __init__(self):
            self.rule={} #we make an empty dictionary

    def newrule(self,leftside,rightside):
            self.rule[leftside]=rightside #we fill the dictionary

    def expand (self,oldlist,depth):
            if depth <= 0:
                    return oldlist 
            newlist=[] # we make an empty new list
            for i in oldlist:
                    if i not in self.rule:
                            newlist.append(i)
                    else:
                            newlist.append(self.expand(self.rule[i],depth-1))
            return newlist

class Fractal(object):
    def __init__(self, start, rules, commands, length, depth):
            self.start = start
            self.rules = rules
            self.commands = commands
            self.length = length
            self.depth = depth

    def draw(self, oldlist):
            for i in oldlist:
                    if type(i) == list:
                            self.draw(i)
                    else:
                            cmd = self.commands[i]
                            cmd.execute(Rasmus, self.length)

def read():
    files = open('tree.fdl')
    commands = {}
    r = rule()
    for line in files:
            line = line.strip()
            oldlist = line.split(' ')
            if oldlist[0] == 'start':
                    start = oldlist[1:]
            elif oldlist[0] == 'rule':
                    r.newrule(oldlist[1], oldlist[3:])
            elif oldlist[0] == 'cmd':
                    cmd = command(oldlist[2], oldlist[3:])
                    commands[oldlist[1]] = cmd
            elif oldlist[0] == 'length':
                    length = int(oldlist[1])
            elif oldlist[0] == 'depth':
                    depth = int(oldlist[1])
    return Fractal(start, r, commands, length, depth)

re = read()

print re.commands.keys()

l = re.rules.expand(re.start , re.depth)
re.draw(l)


wait_for_user()

fdl 文件将生成一棵二叉树,如下所示:

start X

rule X -> F L D X R X U L B

length 100

depth 10

cmd X nop

cmd F fd

cmd B bk

cmd R rt 70

cmd L lt 35

cmd D scale 0.7

cmd U scale 1.4285714285714286

我的问题是我的解释器中有一个错误。似乎解释器没有正确读取比例,并且缩小了我的线条以适应树枝的下一个深度。我试图调试它,但一直没能做到。

4

1 回答 1

0
lenght = length*float(self.l[0])

这条线有两个错误。首先,一个错字。但是,即使名称正确,您也会为局部变量分配一个新值,作为您在函数中执行的最后一件事,这是没有意义的。您可能应该将其替换为,xxx.length以便调用该函数可以更改其值。我写的对象xxx可能是调用方可用的 Fractal 实例;那么你会将它作为参数传递,而不是length.

于 2013-11-01T16:48:11.567 回答