Derek Benz
Derek Benz

Reputation: 19

pygame error: video system not initalized

Writing the code for class but keep getting an error. here is what i have:

import sys
import pygame

from settings import Settings

from ship import Ship

class AlienInvasion:

    def _init_(self):

        pygame.init()
        self.settings = Settings()
        self.screen = pygame.display.set_mode(
            (self.settings.screen_width, self.settings.screen_height))
        pygame.display.set_caption('Alien Invasion')
        self.ship = Ship(self)
        self.bg_color = (230, 230, 230)

    def run_game(self):

        while True:
            self._check_events()
            self._update_screen()
    def _check_events(self):

            #watch for keyboard and mouse events
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
    def _update_screen(self):

            self.screen.fill(self.settings.bg_color)
            self.ship.blitme()  

            pygame.display.flip()
if _name_ == '_main_':

    ai = AlienInvasion()
    ai.run_game()

Upvotes: 1

Views: 71

Answers (1)

Kingsley
Kingsley

Reputation: 14906

Maybe it's a SO-paste-bug, but the code has a single underscores, whereas a double-underscore is required.

I think the specific error your question asks is caused by:

class AlienInvasion:

    def _init_(self):
        pygame.init()

That init(), must have double underscores, e.g.:

class AlienInvasion:

    def __init__(self):     # <-- HERE
        pygame.init()

So with only single underscores, python did not recognise this _init_() function as the class initialiser - __init__(), and thus the pygame.init() display initialise was never called. This leads to the pygame.error: video system not initialized error.

Also:

if __name__ == "__main__":   # (note, double underscores on both)

Upvotes: 2

Related Questions