0
\$\begingroup\$

I'm going through a tutorial and trying to build a game using Python and struggling to resolve below error. Not sure what I'm missing here can someone help with it

Alien_invasion.py

import sys
import pygame
from settings import Settings
from ship import Ship

class AlienInvasion:
    """Overall Class to manage game assets and  behavior.."""

    def __int__(self):
        """Initialize the game and create game resources.."""
        pygame.init()

        self.settings = Settings()   #pygame.display.set_mode((1200,800))
        self.screen = pygame.display.set_mode((self.settings.screen_width,self.settings.screen_height))
        self.ship = Ship(self)
        #pygame.display.set_caption("Alien Invasion")

        #Set the background color
        #self.bg_color = (230,230,230)

    def run_game(self):
        """ Start the main loop for the game..."""
        running = True
        while running:
            #Watch for Keyboard and mouse events.
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    #pygame.QUIT()
                    #running = False
                    sys.exit()

            #Redraw the screen during each pass through the loop
            self.screen.fill(self.settings.bg_color)
            self.ship.blitme()


            # Make the most recently drwan screen visible.
            pygame.display.flip()


if __name__ == '__main__':

    ## Make a game instance and run the game.
    ai= AlienInvasion()
    ai.run_game()

settings.py

class Settings:
    """A class to store all the settings related for Alien Invasion...! """
    def __int__(self):
        """Initialize game settings..! """
        #screen settings
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color = (230,230,230)

ship.py

import pygame

class Ship:
    """ A class to Manage the ship...! """
    def __int__(self,ai_game):
        """ Initialize the ship and set its starting position...! """
        self.screen = ai_game.screen
        self.screen_rect = ai_game.screen.get_rect()

        # Load the ship image and get its rect.
        self.image = pygame.image.load('images/ship.bmp')
        self.rect = self.image.get_rect()

        # Start each new ship at the bottom center of the screen.
        self.rect.midbottom = self.screen_rect.midbottom

    def blitme(self):
        """ Draw the ship at its current location...! """
        self.screen.blit(self.image,self.rect)

Error

C:\Users\user\PycharmProjects\pythonLearningProject\venv-dev\Scripts\python.exe C:\Users\user\PycharmProjects\pythonLearningProject\alien_invasion.py 
pygame 2.1.2 (SDL 2.0.18, Python 3.9.0)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "C:\Users\user\PycharmProjects\pythonLearningProject\alien_invasion.py", line 45, in <module>
    ai.run_game()
  File "C:\Users\user\PycharmProjects\pythonLearningProject\alien_invasion.py", line 26, in run_game
    for event in pygame.event.get():
pygame.error: video system not initialized

Process finished with exit code 1

enter image description here

\$\endgroup\$
2

1 Answer 1

1
\$\begingroup\$

You have a typo, __int__ instead of __init__ (you forgot the second i), which is the problem.

It doesn't run run pygame.init() so it can't initiate video and it can't display window (and later it would give a problem with events because events can't work when window doesn't exist..)

You could use print() to see which part of code is executed and possibly catch these kinds of issues.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .