srinivas
srinivas

Reputation: 31

pygame.error: video system not initialized python code error

After running this code, I got an error:

pygame.error: video system not initialized

My code:

import sys
import pygame
def run_game():
    # Initialize game and create a screen object.
    pygame.init()
    screen = pygame.display.set_mode((1200, 800))
    pygame.display.set_caption("Alien Invasion")
# Start the main loop for the game.
while True:
    # Watch for keyboard and mouse events.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    # Make the most recently drawn screen visible.
    pygame.display.flip()
run_game()

Can anyone help me and explain what this error means and how to rectify it?

Upvotes: 1

Views: 263

Answers (2)

skrx
skrx

Reputation: 20438

The error is raised because pygame.event.get is called without an initialized display (pygame.display.set_mode). The problem is that your while loop is not indented correctly, so it is executed before the run_game function is called. The loop should be inside of the run_game function.

import sys
import pygame


def run_game():
    # Initialize game and create a screen object.
    pygame.init()
    screen = pygame.display.set_mode((1200, 800))
    pygame.display.set_caption("Alien Invasion")

    # Start the main loop for the game.
    while True:
        # Watch for keyboard and mouse events.
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        # Make the most recently drawn screen visible.
        pygame.display.flip()

run_game()

Upvotes: 1

Druta Ruslan
Druta Ruslan

Reputation: 7402

put your code:

run_game()

before while statment`

Upvotes: 0

Related Questions