/
sh_game.py
57 lines (44 loc) · 1.74 KB
/
sh_game.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
57
import sys
import pygame
from ship import Ship
from alien_ship import AlienShip
class SidewaysHero:
"""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("Sideways Hero")
self.ship = Ship(self)
# Make a group of three aliens.
self.aliens = pygame.sprite.Group()
for alien_num in range(30):
new_alien = AlienShip(self)
self.aliens.add(new_alien)
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()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
self.ship.moving_right = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
self.ship.moving_right = False
# Update positions of any characters that might be moving.
self.ship.update()
self.aliens.update()
if pygame.sprite.spritecollideany(self.ship, self.aliens):
self.ship.rect.x = 10000
self.screen.fill((255, 255, 255))
self.ship.blitme()
self.aliens.draw(self.screen)
# Make the most recently drawn screen visible.
pygame.display.flip()
if __name__ == '__main__':
# Make a game instance, and run the game.
sh_game = SidewaysHero()
sh_game.run_game()