“视频系统未初始化”关闭pygame窗口

时间:2014-03-20 18:20:00

标签: python-3.x pygame

我有两个类似的代码片段。两者都运行正常但是当我关闭pygame窗口时,One会显示错误

Traceback (most recent call last):
  File "/home/nabeel/Devalopment/Python/aaa.py", line 92, in <module>
    pygame.display.flip()
pygame.error: video system not initialized

以下是抛出错误的代码

import pygame

pygame.init()

done = False

screen = pygame.display.set_mode([640,480])
pygame.display.set_caption("my_game")

while not done:
    screen.fill((0,0,0))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
            pygame.quit()

    pygame.display.flip()
pygame.quit()

这是一个没有

的人
import pygame

done = False
pygame.init()

screen_size = [320,240]

white = [255,255,255]
black = [0,0,0]

paddle_x = 0
paddle_y = 0
vel_x = 0
vel_y = 0

gutter = 10

screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("mygame")



while not done:

    screen.fill(black)


    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        if event.type == pygame.KEYDOWN:
            if pygame.key.get_pressed()[pygame.K_DOWN]:
                vel_y += 2
            if pygame.key.get_pressed()[pygame.K_UP]:    
                vel_y -= 2
            if pygame.key.get_pressed()[pygame.K_LEFT]:
                vel_x -= 2
            if pygame.key.get_pressed()[pygame.K_RIGHT]:    
                vel_x += 2

    paddle_x += vel_x
    paddle_y += vel_y    


    pygame.draw.line(screen,white,(gutter,0),(gutter,screen_size[1]))
    pygame.draw.line(screen,white,(screen_size[0]-gutter,0),(screen_size[0]-gutter,screen_size[1]))
    pygame.draw.rect(screen,white,[screen_size[0]/2,screen_size[1]/2,10,10],0)
    pygame.draw.line(screen,white,[paddle_x+5,paddle_y],[paddle_x+5,paddle_y+30],gutter)
    pygame.display.flip()




pygame.quit()

虽然这两个人没有做任何有害的事情,但我只是对导致这一切的原因感到好奇

1 个答案:

答案 0 :(得分:3)

尝试删除此块中的pygame.quit():

 if event.type == pygame.QUIT:
    done = True
    pygame.quit()

这个想法是,当调用pygame.quit()时,整个模块是未初始化的,因此如果找到模块的更多方法调用(如pygame.display.flip()),它们将抛出错误:)

如果你希望在上面的条件if语句中保持pygame.quit()方法调用,你可以做的是导入sys模块,在pygame.quit()下面添加sys.exit()

两种方式都应该很好用:) 希望有所帮助,

干杯!亚历