Attempt to modularize python code but has Recursion Error: maximum recursion depth
Attempt to modularize python code but has Recursion Error: maximum recursion depth
您可以在此处找到所有代码
https://github.com/Ninedeadeyes/7-Dungeons-Deep
您将需要 运行 模块化 game.py 以识别
递归错误
下面的函数在 Enemy.py 中(在 Enemy Class 中)
我很久以前写了一个动作角色扮演游戏,它在一个单独的 python 文件(game.py)中都有效,现在我正试图将它模块化。我已经模块化了一大块代码,但我仍然无法区分敌人 class
问题出在敌人 class 和 turtle.ontimer 中的 'move' 函数的最底部。在原始文件 (game.py) 中,它会重复 enemy.move 函数,这样一旦最初触发了移动函数,敌人就会继续移动,但是一旦我对其进行模块化,它就会返回错误 RecursionError:调用 Python 对象时超出了最大递归深度。任何让这个工作的建议。我试图将 'ontimer' 函数输入到游戏循环中,但它变得太卡了,无法玩了。对于为什么在单个文件中不会发生递归错误的任何解释,我们也将不胜感激。
'''
def move(self,block,bob):
if self.direction =="up":
dx= 0
dy= 24
self.shape(".\art\orkup.gif")
elif self.direction =="down":
dx= 0
dy= -24
self.shape(".\art\ork.gif")
elif self.direction =="left":
dx= -24
dy= 0
self.shape(".\art\orkleft.gif")
elif self.direction =="right":
dx= 24
dy= 0
self.shape(".\art\orkright.gif")
else:
dx = 0
dy = 0
if self.is_close(bob):
if bob.xcor()<self.xcor():
self.direction="left"
elif bob.xcor()>self.xcor():
self.direction="right"
elif bob.ycor()<self.ycor():
self.direction="down"
elif bob.ycor()>self.ycor():
self.direction="up"
# Calculate the spot to move to
move_to_x = self.xcor()+ dx
move_to_y = self.ycor()+ dy
if (move_to_x, move_to_y) not in block:
self.goto(move_to_x, move_to_y)
else:
self.direction=random.choice(["up","down","left", "right"])
turtle.ontimer(self.move(block,bob),t=random.randint(100,300))
'''
self.move(block,bob)
不是函数 - 相反,它是对该函数的直接递归调用。
修复:将此调用转换为不带参数的函数
turtle.ontimer(lambda: self.move(block,bob),t=random.randint(100,300))
您可以在此处找到所有代码
https://github.com/Ninedeadeyes/7-Dungeons-Deep
您将需要 运行 模块化 game.py 以识别 递归错误
下面的函数在 Enemy.py 中(在 Enemy Class 中)
我很久以前写了一个动作角色扮演游戏,它在一个单独的 python 文件(game.py)中都有效,现在我正试图将它模块化。我已经模块化了一大块代码,但我仍然无法区分敌人 class
问题出在敌人 class 和 turtle.ontimer 中的 'move' 函数的最底部。在原始文件 (game.py) 中,它会重复 enemy.move 函数,这样一旦最初触发了移动函数,敌人就会继续移动,但是一旦我对其进行模块化,它就会返回错误 RecursionError:调用 Python 对象时超出了最大递归深度。任何让这个工作的建议。我试图将 'ontimer' 函数输入到游戏循环中,但它变得太卡了,无法玩了。对于为什么在单个文件中不会发生递归错误的任何解释,我们也将不胜感激。
'''
def move(self,block,bob):
if self.direction =="up":
dx= 0
dy= 24
self.shape(".\art\orkup.gif")
elif self.direction =="down":
dx= 0
dy= -24
self.shape(".\art\ork.gif")
elif self.direction =="left":
dx= -24
dy= 0
self.shape(".\art\orkleft.gif")
elif self.direction =="right":
dx= 24
dy= 0
self.shape(".\art\orkright.gif")
else:
dx = 0
dy = 0
if self.is_close(bob):
if bob.xcor()<self.xcor():
self.direction="left"
elif bob.xcor()>self.xcor():
self.direction="right"
elif bob.ycor()<self.ycor():
self.direction="down"
elif bob.ycor()>self.ycor():
self.direction="up"
# Calculate the spot to move to
move_to_x = self.xcor()+ dx
move_to_y = self.ycor()+ dy
if (move_to_x, move_to_y) not in block:
self.goto(move_to_x, move_to_y)
else:
self.direction=random.choice(["up","down","left", "right"])
turtle.ontimer(self.move(block,bob),t=random.randint(100,300))
'''
self.move(block,bob)
不是函数 - 相反,它是对该函数的直接递归调用。
修复:将此调用转换为不带参数的函数
turtle.ontimer(lambda: self.move(block,bob),t=random.randint(100,300))