问题
When ever I try to run my code I constantly get this error:
RuntimeError: maximum recursion depth exceeded while calling a Python object
I am quite confused to why this happens, I am trying to make a picture blit and constantly move down the screen, as an object that the player has to dodge and if hits gets "killed" (Still to add collisions in). When I start to get the error is spams the shell with this:
File "C:\Users\John\Desktop\Michael\V'Room External\GAME_MAIN_.py", line 195, in movement
fallingObject()
File "C:\Users\John\Desktop\Michael\V'Room External\GAME_MAIN_.py", line 206, in fallingObject
movement()
File "C:\Users\John\Desktop\Michael\V'Room External\GAME_MAIN_.py", line 160, in movement
print(x)
File "C:\Python34\lib\idlelib\PyShell.py", line 1352, in write
return self.shell.write(s, self.tags)
RuntimeError: maximum recursion depth exceeded while calling a Python object
The relevant code is:
def movement():
crashed = False
while not crashed:
print(x)
...
if x < -10:
x = -10
else:
if x > 490:
x = 490
else:
fallingObject()
def fallingObject():
global w
w = 20
global o_x
o_x = random.randrange(0,width)
objectSpawn = True
while objectSpawn:
movement()
...
回答1:
The problem is that under certain conditions your movement() method calls fallingObject() then it calls movement() and it calls fallingObject() which then calls movement() which then calls fallingObject()... They would continue calling each other infinitely if there wasn't a maximum recursion depth. Python detects this pattern and shuts down your program. Infinite recursion is always bad!
If you look at these oversimplified versions of your methods, you can see they call each other:
def fallingObject():
...
movement()
...
and
def movement():
...
fallingObject()
...
Because of the conditions in your code this behaviour doesn't always occur, only when -10 <= x <= 490.
Solution
You need to re-think your logic. What is the reason why you call one method from the other?
I actually managed to make your program work by removing the movement() call from fallingObject() and making a few other changes. This is the modification which prevents the infinite recursion:
def fallingObject():
...
while objectSpawn:
movement() #<-delete this line
...
objectSpawn = False
You have removed those parts of your code which are irrelevant from the viewpoint of the infinite recursion, but I still write down here the most important changes you have to make in order to make your program work:
- define these variables in the beginning of the program:
o_x = 0,o_y = 0, instead of using global from inside the function - write
if o_y >= heightinstead ofif o_y > heightinsidefallingObject() - do the
screen.blit(a, (o_x,o_y))when you draw yourcarandroadbecause otherwise the blue screen hides your falling object
回答2:
fallingObject and movement call each other as long as object_Spawn and not crashed are True. Since movement is called before objectSpawn is modified, and crashed is only changed when the user quits, they will call each other until you hit the recursion limit (1000 by default), which, as I don't see any kind of delay or tick rate in your code, will happen very quickly.
Note that you also have each of these calls happening inside a while loop. Restructure your code to run the game with these loops (you might not even need both) instead of making recursive calls like this.
回答3:
Your movement function calls fallingObject, which calls movement, creating a recursive loop. Python places a limit on how deep you can recurse (by default, 1000 times), so you need to reorganize your logic to avoid that recursion.
BTW, you should also try to avoid using modifiable global variable because they make code less modular, which can make it harder to debug and to expand the program. But if you must use them you should put the global statements at the start of each function that needs them. Don't scatter them through the function bodies.
I also see that fallingObject has a while loop that always runs exactly once. Why?
来源:https://stackoverflow.com/questions/34476939/pygame-runtimeerror-maximum-recursion-depth-exceeded-while-calling-a-python-o