每次我玩我的游戏pygame.error错误:视频系统未初始化

2024-09-27 07:26:21 发布

您现在位置: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 

还没有做任何事情,因为那些程序还没有编写。你知道吗


Tags: inimport程序eventforgetifmain
1条回答
网友
1楼 · 发布于 2024-09-27 07:26:21

我运行了你的代码,把它缩小到基本的,不能复制。你确定你把pygame.退出()在正确的缩进级别?你知道吗

如果这样做,我可以复制相同的异常(注意pygame.退出不缩进)。这将杀死pygame对象,并在第一次进入run循环时引发异常。我将执行顺序标记为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

当我将pygame初始化和screen对象创建移到main中,以及pygame.退出像这样打电话:

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()

相关问题 更多 >

    热门问题