简体   繁体   中英

Attempt to modularize python code but has Recursion Error: maximum recursion depth

You can find all code here at

https://github.com/Ninedeadeyes/7-Dungeons-Deep

You will need to run modularize game.py to identify the Recursion Error

The below function is within the Enemy.py ( within Enemy Class)

I wrote an action rpg quite a while back ago which all worked but in one single python file(game.py) and now I am attempting to modularize it. I modularize a good chunk of the code already but I am stuck separating the enemy class

The issue is with the very bottom of 'move' function within the enemy class with the turtle.ontimer. In the original file (game.py), it will repeat the enemy.move function so that enemies will continue moving once the move function has been initially triggered but once I modularize it, it comes back with the error RecursionError: maximum recursion depth exceeded while calling a Python object. Any suggestions to get this working. I have attempted to input the 'ontimer' function into the game loop but then it becomes much too janky to play. Any explanation why the Recursion error doesn't happen when it is in a single file would also be appreciated.

'''

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) is not a function - instead, it is an immediate recursive call to the function.

Fix: transform this call to a function with no args

turtle.ontimer(lambda: self.move(block,bob),t=random.randint(100,300)) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM