Python Help - Alien Invasions

Hi,

Can you please advise me on where my mistakes are in this code?
I’ve only been coding for about a month.
It’s telling me that I made two mistakes.
Please let me know specifically how to fix them:

import sys

import pygame

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

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

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

if name == “main”:
#Make a game instance, and run the game.
ai = AlienInvasion()
ai.run_game()

Traceback (most recent call last):
File “C:/Users/qzhon/AppData/Local/Programs/Python/Python37/Python_Josh/Alien_game.py”, line 31, in
ai.run_game()
File “C:/Users/qzhon/AppData/Local/Programs/Python/Python37/Python_Josh/Alien_game.py”, line 20, in run_game
for event in pygame.event.get():
pygame.error: video system not initialized

Thank you very much,
Josh

Try this. As a side note I recommend going through some of the pygame documentation and just copying out the examples they give you and see how things line up. It’ll help you troubleshoot faster when you’re creating a game proper.

import sys
import pygame

pygame.init()

screen = pygame.display.set_mode((1200, 800))
screen.fill((0,0,0))
pygame.display.set_caption("Alien Invasion")

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

	def __init_(self):
		#Initialize the game, and create game resources.
		pass


	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:
					print('quitting')
					sys.exit()

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


ai = AlienInvasion()
ai.run_game()
1 Like

Thank you very much, Toastedpitabread!

That works.

I’m struggling with indentation errors, but I’m getting better at most of the other aspects of coding.

Thank you again, :slight_smile:
Josh

Just keep at it. It may feel like small gains, but before you know it your knowledge will be like a mountain.

2 Likes