frankfu88
frankfu88

Reputation: 15

pygame.error: video system not initialized (Alien Invasion)

I'm currently following a python tutorial on a book and have finally coming to creating a pygame. After manually typing each code,

The Game (Alien Invasion)

import sys

import pygame

from settings import Settings
from ship import Ship

def run_game():
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption("Alien Invasion")

    ship = Ship(screen)

bg_color = (230, 230, 230)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    screen.fill(ai_settings.bg_color)
    ship.blitme()

    pygame.display.flip()

run_game()

Settings:

class Settings():

    def __init__(self):
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color = (230, 230, 230)

Ship:

import pygame

class Ship():

    def __init__(self, screen):
        self.screen = screen

        self.image = pygame.image.load('images/ship.bmp')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom

    def blitme(self):
        self.screen.blit(self.image, self.rect)

I keep getting this error

Traceback (most recent call last):
  File "/Users/ff/Desktop/alien invasion/alien_invasion.py", line 21, in <module>
    for event in pygame.event.get():
pygame.error: video system not initialized

I've made sure each line and indentation is properly inserted. Does anyone know how to fix this?

-----edit-----

The pygame initialization error is fixed after I moved pygame.init() outside the while loop, but now it says

Traceback (most recent call last):
  File "/Users/ff/Desktop/alien invasion/alien_invasion.py", line 27, in <module>
    screen.fill(ai_settings.bg_color)
NameError: name 'screen' is not defined

Upvotes: 0

Views: 337

Answers (1)

kusz
kusz

Reputation: 390

Your function run_game() is called after your while loop so your pygame modules have not been initialized before you start looking for events. Try moving it before the while loop.

Edit: If you want to access screen, ai_settings, and ship outside of your function run_game() then you will need to edit your code like this:

def run_game():
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption("Alien Invasion")

    ship = Ship(screen)

    return screen, ai_settings, ship

bg_color = (230, 230, 230)

screen, ai_settings, ship = run_game()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    screen.fill(ai_settings.bg_color)
    ship.blitme()

    pygame.display.flip()

Notable parts are the lines

    return screen, ai_settings, ship

and

screen, ai_settings, ship = run_game()

Upvotes: 1

Related Questions