每次我运行游戏时都会遇到pygame.error:视频系统未初始化

时间:2015-12-13 03:31:37

标签: python

import pygame
import Selection
import Round
import Winner
import Fighting
pygame.init()


screen = pygame.display.set_mode((640, 500))


def main():
    process = 0

    clock = pygame.time.Clock()
    keepGoing = True
    while keepGoing:
        clock.tick(30)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False

        if process == 0:
            Selection.main()
            process += 1
        elif process == 1:
            Fighting.main()
            process += 1
    pygame.quit()

if __name__ == "__main__":
    main()

出现的错误是

for event in pygame.event.get():
pygame.error: video system not initialized

它贯穿整个程序并进入

Selection.main()

并调用该程序运行正常,但何时关闭此程序开始备份

clock.tick(30)

然后停在

for event in pygame.event.get()

并抛出该错误。在

import Round
import Winner 

还没有做任何事情,因为那些程序尚未编写。

1 个答案:

答案 0 :(得分:1)

我运行了你的代码并将其缩小到必需品并且无法重现。你确定你把pygame.quit()放在正确的缩进级别吗?

如果我这样做,我可以复制相同的异常(注意pygame.quit没有缩进)。这会杀死pygame对象,并且会在首次进入运行循环时引发异常。我将执行顺序标记为1-10,因此很清楚它会抛出异常。

import pygame  #1

pygame.init()  #2
screen = pygame.display.set_mode((640, 500)) #3

def main():  #7

    running = True  #8
    while(running == True):  #9
        for event in pygame.event.get(): #10 pygame doesn't exist and was killed in 4.
            if event.type == pygame.QUIT:
                running = False

pygame.quit() #4

if __name__ == "__main__":  #5
    main()  #6

当我在main中移动pygame初始化和屏幕对象时,异常消失,以及pygame.quit调用如下:

import pygame

def main():
    pygame.init()
    screen = pygame.display.set_mode((640, 500))

    running = True
    while(running == True):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

    pygame.quit()

if __name__ == "__main__":
    main()

这也有效:

import pygame

pygame.init()
screen = pygame.display.set_mode((640, 500))

def main():

    keepGoing = True
    while keepGoing:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False

pygame.quit()

if __name__ == "__main__":
    main()