运行Pygame报错video system not initialized是显卡问题还是代码问题?
问题原因
- 核心错误出在事件判断的代码行,属于代码编写错误,和显卡无法运行程序无关
pygame.quit()是主动关闭pygame所有模块的函数,你在判断条件里调用了它,执行到这行时会直接销毁视频系统,后续执行pygame.display.flip()的时候就会触发「video system not initialized」的报错。
修复方案
把run_game方法里的事件判断行修改为:
if event.type == pygame.QUIT:
pygame.QUIT是pygame定义的窗口关闭事件常量,不需要加括号调用。
修复后完整可运行代码
import pygame import sys class AlienInvasion: def __init__(self): pygame.init() self.screen = pygame.display.set_mode((1200, 800)) pygame.display.set_caption("Alien Invasion") def run_game(self): while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() pygame.display.flip() if __name__ == '__main__': ai = AlienInvasion() ai.run_game()
内容的提问来源于stack exchange,提问作者The noob coder
相关产品推荐
相关产品推荐

