简体   繁体   English

Pygame-RuntimeError:调用Python对象时超出了最大递归深度

[英]Pygame - RuntimeError: maximum recursion depth exceeded while calling a Python object

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()
    ...

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. 问题是,在一定条件下的movement()方法调用fallingObject()然后调用movement()它调用fallingObject()然后调用movement()然后调用fallingObject() ......他们将继续称呼对方无限如果没有最大递归深度。 Python detects this pattern and shuts down your program. Python检测到此模式并关闭程序。 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. 由于您的代码中的条件,此行为并不总是发生,仅当-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. 其实,我设法去除,使您的工作方案movement()从调用fallingObject()和做一些其他的变化。 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: 从无限递归的角度来看,您已经删除了与代码无关的那些部分,但是我仍然在此处写下了您必须进行的最重要的更改,才能使程序正常工作:

  1. define these variables in the beginning of the program: o_x = 0 , o_y = 0 , instead of using global from inside the function 在程序的开头定义以下变量: o_x = 0o_y = 0 ,而不是从函数内部使用global
  2. write if o_y >= height instead of if o_y > height inside fallingObject() if o_y >= height而不是在fallingObject() if o_y > height fallingObject()
  3. do the screen.blit(a, (o_x,o_y)) when you draw your car and road because otherwise the blue screen hides your falling object 在绘制carroad时执行screen.blit(a, (o_x,o_y)) ,因为否则蓝屏会隐藏掉落的物体

fallingObject and movement call each other as long as object_Spawn and not crashed are True . fallingObjectmovement相互只要调用object_Spawnnot crashedTrue 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. 由于movement是在修改objectSpawn之前objectSpawn的,并且crashed仅在用户退出时才更改,因此它们将相互调用,直到您达到递归限制(默认为1000)为止,因为我看不到任何延迟或刻度率很高,将很快发生。

Note that you also have each of these calls happening inside a while loop. 请注意,您还会在while循环内发生所有这些调用。 Restructure your code to run the game with these loops (you might not even need both) instead of making recursive calls like this. 重组代码以使用这些循环运行游戏(您可能甚至不需要两者),而不是像这样进行递归调用。

Your movement function calls fallingObject , which calls movement , creating a recursive loop. 你的movement函数调用fallingObject ,这就要求movement ,创建一个递归循环。 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. Python限制了递归的深度(默认情况下为1000次),因此您需要重新组织逻辑以避免这种递归。

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. 但是,如果必须使用它们,则应将global语句放在需要它们的每个函数的开头。 Don't scatter them through the function bodies. 不要将它们分散在功能主体中。

I also see that fallingObject has a while loop that always runs exactly once. 我还看到fallingObject具有一个while循环,该循环始终仅运行一次。 Why? 为什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RuntimeError:调用Python对象时超出了最大递归深度 - RuntimeError: maximum recursion depth exceeded while calling a Python object RuntimeError:使用ast调用Python对象时,超出了最大递归深度 - RuntimeError: maximum recursion depth exceeded while calling a Python object with ast Python可以循环运行直到RuntimeError:调用Python对象时超出了最大递归深度 - Python possible to run while loop until RuntimeError: maximum recursion depth exceeded while calling a Python object RuntimeError:在从DB获取多边形点时调用Python对象时超出了最大递归深度 - RuntimeError: maximum recursion depth exceeded while calling a Python object while getting polygon points fron DB RuntimeError:在运行openerp时调用Python对象时超出了最大递归深度 - RuntimeError: maximum recursion depth exceeded while calling a Python object while running openerp RuntimeError:在odoo中使用split()函数时调用Python对象时超出了最大递归深度 - RuntimeError: maximum recursion depth exceeded while calling a Python object while using split() function in odoo RuntimeError:在运行sqlite3时调用Python对象时超出了最大递归深度 - RuntimeError: maximum recursion depth exceeded while calling a Python object while running sqlite3 Python suds“ RuntimeError:调用Python对象时超出了最大递归深度” - Python suds “RuntimeError: maximum recursion depth exceeded while calling a Python object” 偶尔的Flask服务器错误:&#39;RuntimeError:调用Python对象时超出了最大递归深度&#39; - Occasional Flask server error: 'RuntimeError: maximum recursion depth exceeded while calling a Python object' pyinstaller 创建 EXE 运行时错误:调用 Python 对象时超出了最大递归深度 - pyinstaller creating EXE RuntimeError: maximum recursion depth exceeded while calling a Python object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM