簡體   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