-
Notifications
You must be signed in to change notification settings - Fork 0
/
alien_invasion.py
170 lines (142 loc) · 5.32 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import sys
import pygame
from time import sleep
from settings import Settings
from button import Button
from scoreboard import Scoreboard
from ship import Ship
from fleet import Fleet
class AlienInvasion:
"""Overall class to manage game assets and behavior."""
def __init__(self):
"""Initialize the game."""
# Initialize game display
pygame.init()
self.settings = Settings()
if self.settings.fullscreen:
self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
self.settings.screen_width = self.screen.get_rect().width
self.settings.screen_height = self.screen.get_rect().height
else:
self.screen = pygame.display.set_mode(
(self.settings.screen_width, self.settings.screen_height))
pygame.display.set_caption("Alien Invasion")
# Game is paused initiallly
self.active = False
# Initialize basic logic variables
self.level = 1
self.ships_remaining = self.settings.ship_limit
# Initialize game elements
self.player_ship = Ship(self)
self.fleet = Fleet(self)
self.play_button = Button(self, "Play")
self.scoreboard = Scoreboard(self)
def run(self):
"""Start the main game loop."""
while True:
# Check for keyboard/mouse events
self._check_events()
# Update player ship and alien fleet, if game is active
if self.active:
self.player_ship.update()
self.fleet.update()
# Draw the game
self._draw()
def _draw(self):
"""Draw the game screen and all elements."""
# Redraw the background
self.screen.fill(self.settings.bg_color)
# Redraw the player ship
self.player_ship.draw()
# Redraw the alien fleet
self.fleet.draw()
# Redraw the scoreboard
self.scoreboard.draw()
# Redraw play button if game is inactive
if not self.active:
self.play_button.draw()
# Make the most recently drawn screen visible
pygame.display.flip()
def _start_game(self):
"""Start a new game."""
# Reset game
self.level = 1
self.ships_remaining = self.settings.ship_limit
self.scoreboard.reset()
self.settings.initialize_dynamic_settings()
self.fleet = Fleet(self)
self.player_ship.reset()
# Hide cursor
pygame.mouse.set_visible(False)
# Start game
self.active = True
def kill_player(self):
"""Handle player death."""
if self.ships_remaining > 0:
# Decrement number of ships remaining
self.ships_remaining -= 1
self.scoreboard.reset()
# Create new fleet and ship
self.fleet = Fleet(self)
self.player_ship.reset()
# Pause
sleep(0.5)
else:
self.active = False
pygame.mouse.set_visible(True)
def kill_alien(self):
"""Handle alien kills."""
self.scoreboard.set_score(self.scoreboard.score + self.settings.alien_points)
# Create a new fleet if all aliens are gone
if not self.fleet.aliens:
self.player_ship.reset()
self.fleet = Fleet(self)
# Increment speed (difficulty)
self.settings.increase_speed()
# Increment level
self.level += 1
self.scoreboard.prep_level()
def _check_events(self):
"""Respond to keyboard and mouse events."""
for event in pygame.event.get():
# Exit when the game window is closed
if event.type == pygame.QUIT:
self.scoreboard.save_high_score()
sys.exit()
# Respond to mouse events
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = pygame.mouse.get_pos()
self._check_play_button(mouse_pos)
# Respond to key events
elif event.type == pygame.KEYDOWN:
self._check_keydown_event(event)
elif event.type == pygame.KEYUP:
self._check_keyup_event(event)
def _check_keydown_event(self, event):
"""Respond to keypresses."""
if event.key == pygame.K_RIGHT:
self.player_ship.moving_right = True
elif event.key == pygame.K_LEFT:
self.player_ship.moving_left = True
elif event.key == pygame.K_SPACE:
self.player_ship.fire_bullet()
elif event.key == pygame.K_q:
self.scoreboard.save_high_score()
sys.exit()
elif event.key == pygame.K_p and not self.active:
self._start_game()
def _check_keyup_event(self, event):
"""Respond to key releases."""
if event.key == pygame.K_RIGHT:
self.player_ship.moving_right = False
elif event.key == pygame.K_LEFT:
self.player_ship.moving_left = False
def _check_play_button(self, mouse_pos):
"""Start a new game when the player clicks Play."""
button_clicked = self.play_button.rect.collidepoint(mouse_pos)
if button_clicked and not self.active:
self._start_game()
if __name__ == '__main__':
# Make a game instance and run it
game = AlienInvasion()
game.run()