退出时挂起的 Pygame 程序 [英] Pygame programs hanging on exit

查看:95
本文介绍了退出时挂起的 Pygame 程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在修改 pygame,当我尝试关闭它们时,我用它制作的所有小程序似乎都挂了.

I'm tinkering around with pygame right now, and it seems like all the little programs that I make with it hang when I try to close them.

以如下代码为例:

from pygame.locals import *
pygame.init()
# YEEAAH!
tile_file = "blue_tile.bmp"
SCREEN_SIZE = (640, 480)
SCREEN_DEPTH = 32

if __name__ == "__main__":
    screen = pygame.display.set_mode(SCREEN_SIZE, 0, SCREEN_DEPTH)
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                break

    tile = pygame.image.load(tile_file).convert()
    colorkey = tile.get_at((0,0))
    tile.set_colorkey(colorkey, RLEACCEL)

    y = SCREEN_SIZE[1] / 2
    x = SCREEN_SIZE[0] / 2

    for _ in xrange(50):
        screen.blit(tile, (x,y))
        x -= 7
        y -= 14

我没有发现代码有什么问题,它可以工作(忽略 tile 没有在正确的位置 blit 的事实),但是没有回溯,关闭它的唯一方法是终止 python 进程在任务管理器中.谁能发现我的代码有问题?

I don't see anything wrong with the code, it works (ignore the fact that the tile isn't blit in the right spots), but there's no traceback and the only way to close it is to kill the python process in Task Manager. Can anyone spot a problem with my code?

推荐答案

如果您从 IDLE 运行它,那么您缺少 pygame.quit().

If you are running it from IDLE, then you are missing pygame.quit().

这是由 IDLE python 解释器引起的,它似乎以某种方式保留了引用.确保在退出应用程序或游戏时调用 pygame.quit().

This is caused by the IDLE python interpreter, which seems to keep the references around somehow. Make sure, you invoke pygame.quit() on exiting your application or game.

参见:在 IDLE 中为什么 Pygame 窗口没有正确关闭?

还有:Pygame 文档 - pygame.quit()

这篇关于退出时挂起的 Pygame 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆