python - Pygame 应用程序不会在第一次关闭

标签 python pygame

我正在用 pygame 构建一个小游戏,我想要一个退出函数。但是需要多次点击才能退出,而且也不一致。 Windows 退出功能也正在重新启动程序这是处理退出的代码部分

    if isKill:
        pygame.mixer.music.stop()
        gameover = myfont.render("Press R to Respawn", False, (255, 255, 255))
        rect = gameover.get_rect()
        rect.center = screen.get_rect().center
        screen.blit(gameover, rect)
        if event.type == KEYDOWN:
            if event.key == K_r:
                gameloop()

    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False

*gameloop() 是整个脚本

最佳答案

您可以使用 pygame 内置事件 QUIT 而不是 event.type

引用以下代码

running = True
while running:
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        running = False
        #Can use pygame.quit() in place of running=False if the above line doesn't work.

这个while 循环是gameloop 的开始。游戏循环的内容应该在里面。
不要在 while 循环中调用 gameloop() 函数

关于python - Pygame 应用程序不会在第一次关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63585295/

相关文章:

python - 基于颜色的图像分割

python - Pygame gfxdraw模块改变线宽

python - 我的 A* 寻路算法并不总是能得到最短路径

python - 使用 .pyc 文件是否合适?

python - 如何在pygame中隐藏标题栏?

python - gobject.type_register() 是做什么的?

python - Redshift 创建表无法通过 Python 工作

python - django.db.utils.operationalError : (2059 ,"Authentication Plugin ' caching_sha2_password'")

Pygame 鼠标事件中的 Python 时间计数器

python - 手动生成的 JSON 可以工作,但通过 json.dumps 创建的 JSON 不起作用,即使输出看起来完全相同