1
\$\begingroup\$

I'm making a game and I have almost finished it, but I'm usually finding the error RuntimeError: maximum recursion depth exceeded when I iterate a sprite group.

This is an example and the group is self.nextStageCollision():

def nextStageCollision(self):
    for tile in self.nextStageCollision():
        if tile.rect.colliderect(self.rect):
            self.rect.x = 18*20
            self.rect.y = 20*20
            return True

    return False

The group self.nextStageCollision() has 4 sprites (tiles) and I'm checking collision between my player and those 4 tiles to go to the next stage but I have to be doing something wrong iterating the sprite group because (I don't know why) it becomes recursive and raises the maximum recursion depth runtime error. I've debugged it and when it reaches the for each line, it loops a lot of times there (althought there are only 4 sprites in that group).

I know I can increase the recursion depth, but probably it's better to not play with that and try to improve the code. So, what am I doing wrong? Is there another better way to interate a sprite group to check collisions?

\$\endgroup\$
0
\$\begingroup\$

You've made an infinite loop:

def nextStageCollision(self):
    for tile in self.nextStageCollision():

You probably meant something like:

def nextStageCollision(self):
    for tile in self.tileList:

Note what you are iterating over. You are calling the nextStageCollision to find what to iterate over, which calls nextStageCollision to find what to iterate over, which calls nextStageCollision and so on until you've hit the "maximum recursion depth".

You shouldn't change it, it's in a good position and it wouldn't matter, it would just take longer for your code to crash.

\$\endgroup\$
  • \$\begingroup\$ omg what a stupid error.. xD \$\endgroup\$ – Drumnbass Jun 15 '15 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.