I'm trying to create a pygame window and responding to user input

52 views Asked by At

Here is my source code:

import sys  
import pygame  
from settings import Settings  

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

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

        #self.screen = pygame.display.set_mode((1200, 800))
        #pygame.display.set_caption('Alien Invasion')
        self.bg_color = (230, 230, 230)
        self.settings = Settings()

        self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
        pygame.display.set_caption('Alien Invasion')

    def run_game(self):
        """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:
                    sys.exit()

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


            # make the most recently drawn screen visible.
            pygame.display.flip()

if __name__ == '__main__':  
    # make a game instance, and run the game.  
    a1 = AlienInvasion  
    a1.run_game()

When I run the program, I got a TypeError:

Traceback (most recent call last):
  File "c:\Users\franc\Desktop\pyprojects\game_project\alien_invasion.py", line 38, in <module>
    a1.run_game()
TypeError: AlienInvasion.run_game() missing 1 required positional argument: 'self'

I imported the sys and pygame modules, and created a class with an __init__() method followed by rest of the code. At the end of the file, I created an instance of the class, and then called run_game(). I was expecting it to show a blank window screen.

1

There are 1 answers

1
Akrix On

That error typically occurs when you call a method of a class on the class rather than an instance of that class. Instead of doing that, try this:

obj = AlienInvasion()
obj.run_game()

Also, could you try adding all your code?