您可以在此處找到所有代碼https://github.com/Ninedeadeyes/7-Dungeons-Deep您需要運行 modularize game.py 來識別遞歸錯誤以下函數(shù)位于 Enemy.py 中(在 Enemy 類中)我很久以前寫了一個動作角色扮演游戲,它全部工作但在一個 python 文件(game.py)中,現(xiàn)在我試圖將它模塊化。我已經(jīng)模塊化了很大一部分代碼,但我仍然無法分離敵人的類問題出在敵人類中帶有turtle.ontimer的“移動”函數(shù)的最底層。在原始文件(game.py)中,它將重復(fù)敵人.move函數(shù),以便一旦最初觸發(fā)移動函數(shù),敵人就會繼續(xù)移動,但一旦我將其模塊化,它就會返回錯誤RecursionError:超出最大遞歸深度調(diào)用 Python 對象時。任何讓它發(fā)揮作用的建議。我嘗試將“ontimer”函數(shù)輸入到游戲循環(huán)中,但隨后它變得太卡頓而無法玩。任何解釋為什么遞歸錯誤在單個文件中時不會發(fā)生的原因也將不勝感激。'''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)) '''
1 回答

斯蒂芬大帝
TA貢獻1827條經(jīng)驗 獲得超8個贊
self.move(block,bob)
不是一個函數(shù) - 相反,它是對該函數(shù)的立即遞歸調(diào)用。
修復(fù):將此調(diào)用轉(zhuǎn)換為不帶參數(shù)的函數(shù)
turtle.ontimer(lambda: self.move(block,bob),t=random.randint(100,300))
添加回答
舉報
0/150
提交
取消