-
Notifications
You must be signed in to change notification settings - Fork 0
/
alien_invasion.py
56 lines (46 loc) · 1.66 KB
/
alien_invasion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import sys, pygame
from settings import Settings
from ship import Ship
from ugly_elf import UglyElf
class AlienInvasion:
"""Overall class to manage game assets and behaviour"""
def __init__(self):
pygame.init()
self.clock = pygame.time.Clock()
self.settings = Settings()
self.screen = pygame.display.set_mode((self.settings.screen_width,
self.settings.screen_height))
pygame.display.set_caption("Incredible Alien Invasion Game")
self.bg_color = (230, 230, 230)
self.ship = Ship(self)
self.elf = UglyElf(self)
def run_game(self):
"""Start the main loop for the game"""
while True:
# watch for keyboard and mouse events
self._check_events()
# custom: 1 : try block to handle quitting game from keyboard
try:
self._update_screen()
except Exception as exp:
print(f"\nException Stopped running")
sys.exit(-1)
else:
self.clock.tick(60)
def _check_events(self):
"""Respond to keypresses and mouse events"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
def _update_screen(self):
"""Update images on the screen"""
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
self.elf.blitme()
# make the most recently drawn screen visible
pygame.display.flip()
if __name__ == '__main__':
# Make game instance and run game
# custom: 1: try block
ai = AlienInvasion()
ai.run_game()