簡體   English   中英

pygame 程序運行后窗口凍結

[英]Window freeze after pygame program runs

我正在學習 pygame,並且一直在研究 Eric Matthews ( https://g.co/kgs/WP5fay ) 的“Python Crash Course”一書中給出的示例。 我已經在 macOS High Sierra 上安裝了 pygame 1.9.3 版。

當我運行以下程序時,會打開一個窗口,當我單擊“X”關閉它時,窗口會凍結並且詛咒不斷循環,並且在活動監視器中出現 python 未響應錯誤。 我已經嘗試了許多選項來解決這個問題,但它並沒有消失。

import pygame
import sys
def run_game():
    pygame.init()
    screen = pygame.display.set_mode((400, 300))
    pygame.display.set_caption('Alien Invasion')
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        pygame.display.flip()

run_game() #when I run this function, a window opens and freezes a couple of seconds after

您必須終止應用程序循環並使用pygame.quit()初始化所有 pygame 模塊:

def run_game():
    pygame.init()
    screen = pygame.display.set_mode((400, 300))
    pygame.display.set_caption('Alien Invasion')

    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
        pygame.display.flip()
 
    pygame.quit()
    sys.exit()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM