繁体   English   中英

Pygame window 没有正确关闭 clock.tick 在循环中

[英]Pygame window does not close properly when clock.tick in the loop

我正在做一个项目,当我将 clock.tick 添加到我的主游戏循环时,我的 pygame window 没有关闭。


def game_loop():
    """The main game loop that runs as the game runs. Returns when the pygame window is closed."""
    global running
    global timer
    while running:
        while timer > screen.fixed_fps:
            fixed_update()
            timer -= screen.fixed_fps
        update()
        for event in pygame.event.get():  
            if event.type == pygame.QUIT:
                running = False
                return
        screen.clock.tick(screen.fps)
        timer += delta_time()
    pygame.quit()
    return

当我点击 X 时,屏幕冻结,直到我让 go,但除非我在非常特定的时间范围内点击 X(我通常需要点击它 20 次才能关闭)它不起作用。

这可能是因为您在事件循环中处理 QUIT 事件的方式。 pygame.event.get() function 返回自上次调用以来发生的所有事件,因此如果循环中存在延迟,则可能不会处理 QUIT 事件,直到累积了多个事件。 要解决此问题,您应该将 QUIT 事件处理代码移动到事件循环的顶部,以便在事件发生时立即处理它。 您也可以尝试在事件处理后添加pygame.display.update()以确保它已更新。

    while running:
        for event in pygame.event.get():  
            if event.type == pygame.QUIT:
                running = False
                pygame.quit()
                return
        while timer > screen.fixed_fps:
            fixed_update()
            timer -= screen.fixed_fps
        update()
        screen.clock.tick(screen.fps)
        timer += delta_time()

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM