python pygame错误:视频系统未初始化

时间:2014-05-02 15:01:46

标签: python pygame

这是我得到的错误我不确定如何解决这个或正在发生的事情导致这种情况。问题似乎在这个评论之下。如您所见,使用event.get的for循环显然会导致视频系统停止初始化。

Traceback (most recent call last):
  File "E:\ICS\Assignment\Super Break\Super Break.py", line 49, in <module>
    main()
  File "E:\ICS\Assignment\Super Break\Super Break.py", line 26, in main
    for event in pygame.event.get():
error: video system not initialized

import pygame, pySprites
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((640, 480))

def main(): 
    pygame.display.set_caption("Super Break")

    background = pygame.Surface(screen.get_size()) 
    background = background.convert() 
    background.fill((255, 255, 255)) 
    screen.blit(background, (0, 0))

    score_keeper = pySprites.ScoreKeeper() 
    ball = pySprites.Ball(screen) 
    player1 = pySprites.Player(screen, 1)
    allSprites = pygame.sprite.Group(score_keeper,ball, player1)

    clock = pygame.time.Clock() 
    keepGoing = True

    pygame.mouse.set_visible(False)

    while keepGoing:
        clock.tick(30)
        for event in pygame.event.get():
            if event.type == pygame.QUIT: 
                keepGoing = False
            elif event.type == pygame.JOYHATMOTION: 
                player1.change_direction(event.value) 
            elif event.type == pygame.KEYDOWN: 
                if event.key == pygame.K_UP: 
                    player2.change_direction((0, 1)) 
                if event.key == pygame.K_DOWN: 
                    player2.change_direction((0, -1))


            allSprites.clear(screen, background) 
            allSprites.update() 
            allSprites.draw(screen)        
            pygame.display.flip()            

            # def speed_up(self): 
            #    if self.__dx > 0: 
            #       self.__dx += 1

        pygame.mouse.set_visible(True)
        pygame.quit()
main()

1 个答案:

答案 0 :(得分:2)

你的缩进是错误的。您在主循环中调用pygame.quit。这会导致视频屏幕被破坏,并导致上述错误。我还注意到你在事件循环中调用了draw函数。它们应该在主循环中调用。

这是您的固定代码:

import pygame, pySprites
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((640, 480))

def main(): 
    pygame.display.set_caption("Super Break")

    background = pygame.Surface(screen.get_size()) 
    background = background.convert() 
    background.fill((255, 255, 255)) 
    screen.blit(background, (0, 0))

    score_keeper = pySprites.ScoreKeeper() 
    ball = pySprites.Ball(screen) 
    player1 = pySprites.Player(screen, 1)
    allSprites = pygame.sprite.Group(score_keeper,ball, player1)

    clock = pygame.time.Clock() 
    keepGoing = True

    pygame.mouse.set_visible(False)

    while keepGoing:
        clock.tick(30)
        for event in pygame.event.get():
            if event.type == pygame.QUIT: 
                keepGoing = False
            elif event.type == pygame.JOYHATMOTION: 
                player1.change_direction(event.value) 
            elif event.type == pygame.KEYDOWN: 
                if event.key == pygame.K_UP: 
                    player2.change_direction((0, 1)) 
                if event.key == pygame.K_DOWN: 
                    player2.change_direction((0, -1))


        allSprites.clear(screen, background) 
        allSprites.update() 
        allSprites.draw(screen)        
        pygame.display.flip()            

    pygame.mouse.set_visible(True)
    pygame.quit()
main()