我正在尝试做Python速成教程中的一个项目:外星人入侵第12章。我刚刚开始,由于某种原因,错误:pygame.error: video system not initialized不断弹出。我很确定我正确地遵循了说明,所以我不知道我可能做错了什么…?
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()发布于 2018-03-09 15:52:53
出现该错误的原因是,在使用pygame.display.set_mode初始化显示之前调用了pygame.event.get。
在Python中,正确缩进代码是非常重要的。你的程序看起来很可能类似于这个版本:
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()https://stackoverflow.com/questions/49175584
复制相似问题