首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python速成课程:"pygame.error:视频系统未初始化“

Python速成课程:"pygame.error:视频系统未初始化“
EN

Stack Overflow用户
提问于 2018-03-08 22:30:55
回答 1查看 560关注 0票数 3

我正在尝试做Python速成教程中的一个项目:外星人入侵第12章。我刚刚开始,由于某种原因,错误:pygame.error: video system not initialized不断弹出。我很确定我正确地遵循了说明,所以我不知道我可能做错了什么…?

代码语言:javascript
复制
import sys

import pygame

from settings import Settings

def run_game():
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption('Alien Invasion')

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            screen.fill(ai_settings.bg_color)
            pygame.quit()
            sys.exit()

    pygame.display.flip()
run_game()
EN

回答 1

Stack Overflow用户

发布于 2018-03-09 15:52:53

出现该错误的原因是,在使用pygame.display.set_mode初始化显示之前调用了pygame.event.get

在Python中,正确缩进代码是非常重要的。你的程序看起来很可能类似于这个版本:

代码语言:javascript
复制
import sys

import pygame

from settings import Settings

def run_game():
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption('Alien Invasion')
    # This block should be in the `run_game` function.
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        # Fill the screen in the while loop not in the event
        # loop. It makes no sense to fill the screen only when
        # the user quits.
        screen.fill(ai_settings.bg_color)
        pygame.display.flip()

run_game()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49175584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档