3

I'm trying to do one of the projects in the Python Crash Course book: Alien Invasion Chapter 12. I just started and for some reason the error: pygame.error: video system not initialized keeps popping up. I'm pretty sure I followed the directions appropriately so I don't know what I have possibly done wrong...?

import sys

import pygame

from settings import Settings

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

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

    pygame.display.flip()
run_game()
Kaitlyn
  • 31
  • 3
  • 4
    Try adding one level of indentation to your `while` block. – fenceop Mar 08 '18 at 14:51
  • 1
    `run_game` will never be called since the only way out of the infinite loop in your case is `sys.exit` which kills the script. – ForceBru Mar 08 '18 at 14:51
  • That's what I thought too but that's what it says in the book. I tried moving or getting rid of '`sys.exit` but I still receive an error. I also tried removing `run_game` but obviously the game didn't run – Kaitlyn Mar 08 '18 at 15:32

1 Answers1

0

The error occurs because pygame.event.get is called before you initialize the display with pygame.display.set_mode.

It's very important in Python to indent your code correctly. Your program should most likely look similar to this version:

import sys

import pygame

from settings import Settings

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')
    # This block should be in the `run_game` function.
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        # Fill the screen in the while loop not in the event
        # loop. It makes no sense to fill the screen only when
        # the user quits.
        screen.fill(ai_settings.bg_color)
        pygame.display.flip()

run_game()
skrx
  • 19,980
  • 5
  • 34
  • 48
  • I tried your code but now I'm getting: Traceback (most recent call last): `AttributeError: 'Settings' object has no attribute 'screen_width'` Does that mean my `settings.py` file isn't connected to the original file? – Kaitlyn Mar 09 '18 at 15:12
  • That means your `Settings` class doesn't have a `screen_width` attribute. Have you defined it elsewhere in the settings.py file? – skrx Mar 09 '18 at 16:18
  • 'class Settings(): def _intit_(self): self.screen_width = 800 self.screen_height = 1200 self.bg_color = 230,230,230' – Kaitlyn Mar 10 '18 at 16:51
  • There are some typos: `def _intit_(self):` should be `def __init__(self):`. – skrx Mar 10 '18 at 16:55
  • 'AttributeError: 'Settings' object has no attribute 'screen_width'' – Kaitlyn Mar 10 '18 at 21:45