You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

《Python Crash Course》项目中Pygame窗口启动极慢问题求助

Pygame窗口启动缓慢问题求助

我正在跟着《Python Crash Course》里的外星人入侵项目学习,初始阶段写了个创建指定背景色窗口的代码。代码完全照搬书中内容,能正常运行没报错,但窗口启动要等整整一分钟。我怀疑是系统层面的问题,但搞不清具体原因,来求助大家。

代码如下:

import pygame
import sys

from settings import Settings

def run_game():
    # Initialize game and create a screen object.

    pygame.init()
    # Init settings
    my_settings = Settings()

    screen = pygame.display.set_mode((my_settings.screen_widht, my_settings.screen_height))
    pygame.display.set_caption("Alien Invasion")

    # Start the main loop for the game.
    while True:
        # Watch for keyboard and mouse events.
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        # Redraw the screen during each pass through the loop.
        screen.fill(my_settings.bg_color)
        
        # Make the most recently drawn screen visible.
        pygame.display.flip()
        
run_game()

补充:我发现把pygame.init()这句删掉后,窗口就能立刻打开,但不知道为什么会这样。

内容的提问来源于stack exchange,提问作者Elias LP

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 18:55:15