user3596669 user3596669 - 7 months ago 24
Python Question

python pygame error: video system not initialized

This is the error I got I'm not sure how to fix this or what is happening to cause this. The problem seems to be under this comment. As you can see, the for loop that is using event.get apparently causes the video system to stop initializing.

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

Answer

Your indentation is wrong. You call pygame.quit in your main loop. This causes the video screen to be destroyed, and causes the above error. I also noticed that you call your draw functions in your event loop. They should be called in the main loop.

Here is your fixed code:

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