Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Alien Invasion
#1
I'm trying to teach myself Python. I have a little knowledge in C++, so python isn't completely foreign to me, but it's all still really new to me.

I bought a book: Matthes, Eric. Python Crash Course, 2nd Edition (p. 229). No Starch Press. Kindle Edition.

In the book I started the first Project. It's making an Alien Invasion game. I wrote the code he wrote to get a window to open, but I can't seem to get it to work. I looked at other parts of the forum and found a section that shows how to make a window. This code worked for me, and I got the window to pop up. However, that code is completely different than the code from the book. For now, I would just like to figure out why this code, copied directly from the book, is not working for me and why. Any help would be much appreciated.
I keep getting an error code AttributeError: 'AlienInvasion' object has no attribute 'run_game'

import sys

import pygame

class AlienInvasion:

	"""Overall classs to manage game assets and behavior."""

def __init__(self):
	"""Initialize the game, and greate 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()
Reply
#2
Indentation is a big deal in Python. You need to indent lines 9 through 25. Since they are not indented, Python does not see them as part of your class AlienInvasion.
Reply
#3
Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  "Mother" from Alien terminal game. michael1789 1 2,423 Jun-09-2019, 02:09 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020