2

我在 Debian 上使用 Pygame 1.9.1 和 Python 2.7.3,

在我变得更高级之前,我正在尝试让一个小型测试程序正常工作;
在我告诉 Pygame 退出之前,一切似乎都很好。

我读过你需要pygame.quit(),至少在 IDLE 中(以确保它干净地退出,)
所以我明白了 - 但它仍然冻结了

相关代码如下所示:

def mainLoop():                                                                                                                 
    running = True                                                                                                              
    clock = pygame.time.Clock()                                                                                                 
    while running:                                                                                                              
        gameMode()                                                                                                              
        render()                                                                                                                
        key = pygame.key.get_pressed()                                                                                          
        for event in pygame.event.get():                                                                                        
            if event.type == QUIT or key[K_ESCAPE]:                                                                             
                running = False                                                                                                 
                print 'finished'                                                                                                
                return                                                                                                          
        clock.tick(30)

if __name__=='__main__':                                                                                                        
    mainLoop()                                                                                                                  
    print 'exiting after main loop'                                                                                             
    pygame.quit()

我运行它,它打印出“完成”和“主循环后退出”,
但即便如此,窗口仍然存在。

更奇怪的是,
我把它缩减为一个仍然挂起的最小程序(就是这样,全部):

import pygame
pygame.init()
pygame.quit()

有人可以解释一下这里发生了什么吗?

4

4 回答 4

4

pygame.init will try to initialise all of PyGame's modules, whether needed or not. pygame.quit will quit all active modules, and is called automatically when the interpreter quits (which is why the issue still remains when the explicit call to pygame.quit is removed).

The pygame.mixer.quit method currently hangs on Debian Wheezy (as of 2013-06-12). If you are not using the mixer, you can just replace calls to pygame.init with calls to the init methods of the modules which you are using (a list of modules which require init calls can be found under 'I' in the PyGame documentation index). In my case, initialising the display module was sufficient. So, your example becomes:

import pygame
pygame.display.init()
pygame.quit()
于 2013-06-12T15:14:29.017 回答
0

您是否尝试在 IDLE 中运行该程序?我听说你不应该这样做,因为 IDLE 不喜欢 pygame

于 2013-02-04T12:00:54.537 回答
0

该命令pygame.quit()基本上只是关闭您打开的窗口。然后代码将继续执行,直到出现错误,即屏幕不存在。如果您想真正停止程序,请使用sys.exit(),程序将停止。

于 2013-02-04T00:30:06.363 回答
0

即使使用 IDLE,我也做了两个:

# other code ^
pygame.quit()
sys.exit()

这似乎奏效了

于 2018-04-05T23:02:29.667 回答