/
main.py
executable file
·161 lines (126 loc) · 5.77 KB
/
main.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
#!/usr/bin/env python3
"""
This file is part of pyinvaiders.
Copyright (C) 2019, James Lee <jamesl33info@gmail.com>.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
# pylint: disable-msg=E1121
import pygame
from constants import FPS, DISPLAY, BACKGROUND
from factory import Factory
class SpaceInvaiders():
"""A clone of the classic Space Invaiders game.
Attributes:
_display (pygame.display): The main display surface.
_background (pygame.Surface): The games background surface.
_clock (pygame.time.Clock): The games main clock.
_tank (Tank): The tank the user controls.
_shields (list [Shield]): The shields which defend the user.
_alien_horde (AlienHorde): The alien horde which the user fights.
_entities (dict {pygame.sprite.Group}): The groups of entities.
"""
def __init__(self):
self._display = pygame.display.set_mode(DISPLAY.size)
self._background = pygame.Surface(BACKGROUND.size)
self._clock = pygame.time.Clock()
self._tank = None
self._shields = None
self._alien_horde = None
self._entities = {
'all': pygame.sprite.LayeredDirty(),
'bullets': pygame.sprite.LayeredDirty(),
'explosions': pygame.sprite.LayeredDirty(),
'mystery': pygame.sprite.LayeredDirty(),
'shields': pygame.sprite.LayeredDirty(),
'ship_bullets': pygame.sprite.LayeredDirty(),
'ships': pygame.sprite.LayeredDirty(),
'tank_bullets': pygame.sprite.LayeredDirty(),
'tanks': pygame.sprite.LayeredDirty()
}
def start(self):
"""Start playing the game."""
self.restart()
while True:
self._update()
def restart(self):
"""Reset all the games variables causing a restart."""
for _, sprite_group in self._entities.items():
sprite_group.empty()
self._tank = Factory.create_tank(self._entities['all'],
self._entities['tanks'])
self._shields = Factory.create_shields(self._entities['all'],
self._entities['shields'])
self._alien_horde = Factory.create_horde(self._entities['all'],
self._entities['ships'])
def _update(self):
"""Update the game by one frame."""
self._clear_entities()
self._update_entities(self._clock.tick(FPS) / 1000)
self._handle_input(pygame.key.get_pressed())
self._draw_entities(self._entities['all'].draw(self._display))
def _clear_entities(self):
"""Clear all of the sprites on the display"""
self._entities['all'].clear(self._display, self._background)
def _update_entities(self, seconds_elapsed):
"""Do any operations which will update the games state.
Arguments:
seconds_elapsed (float): The time in seconds since the last frame.
"""
self._entities['all'].update(seconds_elapsed)
self._alien_horde.update(seconds_elapsed)
self._tank.take_damage(self._entities['ship_bullets'],
self._entities['all'],
self._entities['explosions'])
self._alien_horde.move()
self._alien_horde.shoot(self._tank,
self._entities['all'],
self._entities['bullets'],
self._entities['ship_bullets'])
for sheild in self._entities['shields']:
sheild.take_damage(self._entities['bullets'])
for ship in self._entities['ships']:
ship.take_damage(self._entities['tank_bullets'],
self._entities['all'],
self._entities['explosions'])
for mystery in self._entities['mystery']:
mystery.take_damage(self._entities['tank_bullets'],
self._entities['all'],
self._entities['explosions'])
for bullet in self._entities['bullets']:
bullet.take_damage(self._entities['bullets'],
self._entities['all'],
self._entities['explosions'])
def _handle_input(self, keys):
"""Handle any of the keys pressed by the user.
Arguments:
keys (dict {int: bool}): The keys which are currently pressed.
"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if keys[pygame.K_LCTRL] and keys[pygame.K_r]:
self.restart()
self._tank.move(keys[pygame.K_RIGHT] - keys[pygame.K_LEFT])
if keys[pygame.K_UP]:
self._tank.shoot(self._entities['all'],
self._entities['bullets'],
self._entities['tank_bullets'])
@classmethod
def _draw_entities(cls, dirty_rects):
"""Redraw any of the entities which were cleared.
Arguments:
dirty_rects (list [pygame.Rect]): The rects to redraw.
"""
pygame.display.update(dirty_rects)
if __name__ == '__main__':
GAME = SpaceInvaiders()
GAME.start()