Repository navigation
Expand file tree
/
Copy pathgame_classes.py
More file actions
300 lines (250 loc) · 11.3 KB
/
Copy pathgame_classes.py
File metadata and controls
300 lines (250 loc) · 11.3 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import pygame
import random
from os.path import join
from game_functions import cycle_player_imgs, load_and_scale_imgs
class Player(pygame.sprite.Sprite):
"""
The player (spaceship) in the game. Handles its movement, animation, health, shields, and explosions.
"""
def __init__(self, groups, screen_width, screen_height, image_dict, game):
super().__init__(groups)
self.game = game
self.images = image_dict['spaceship']
self.idle_image_index = 0
self.image = self.images[self.idle_image_index]
self.rect = self.image.get_frect(
center=(screen_width / 2, screen_height / 2))
self.mask = pygame.mask.from_surface(self.image)
# Movement and animation attributes
self.is_moving = False
self.animation_speed = 0.2
self.animation_timer = 0
self.movement_threshold = 3
self.screen_width = screen_width
self.screen_height = screen_height
self.x, self.y = self.rect.center
# Health and shield attributes
self.max_health = 100
self.health = self.max_health
self.max_shield = 100
self.shield = 0
self.temp_shield_timer = 0
self.temp_shield_duration = 5
self.has_permanent_shield = False
# Explosion attributes
self.explosion_images = image_dict['explosions']
self.explosion_index = 0
self.is_exploding = False
self.explosion_timer = 0
self.explosion_speed = 0.1
def update(self):
"""Update the player's state each frame."""
if self.is_exploding:
self.explode()
else:
self.move_player(self.x, self.y)
self.update_shield()
def move_player(self, x, y):
"""
Move the player to the specified position, respecting screen boundaries.
Also handles player animation based on movement.
"""
# Clamp the position within screen boundaries
new_x = max(self.rect.width // 2,
min(x, self.screen_width - self.rect.width // 2))
new_y = max(self.rect.height // 2,
min(y, self.screen_height - self.rect.height // 2))
# Check if the player is moving significantly
dx = new_x - self.rect.centerx
dy = new_y - self.rect.centery
self.is_moving = abs(dx) > self.movement_threshold or abs(
dy) > self.movement_threshold
if self.is_moving:
self.rect.center = (new_x, new_y)
self.animation_timer += self.game.dt
if self.animation_timer >= self.animation_speed:
self.animation_timer = 0
self.idle_image_index, self.image = cycle_player_imgs(
self.idle_image_index, self.images[1:])
self.mask = pygame.mask.from_surface(self.image)
else:
self.idle_image_index = 0
self.image = self.images[0]
self.mask = pygame.mask.from_surface(self.image)
def take_damage(self, amount):
"""Apply damage to the player, considers shields first."""
if self.shield > 0:
self.shield = max(0, self.shield - amount)
if self.shield == 0 and self.has_permanent_shield:
self.has_permanent_shield = False
else:
self.health = max(0, self.health - amount)
def update_shield(self):
"""Update the shield status, handling temporary shield decay."""
if not self.has_permanent_shield and self.temp_shield_timer > 0:
self.temp_shield_timer -= self.game.dt
self.shield = max(
0, self.shield - (self.max_shield / self.temp_shield_duration * self.game.dt))
if self.temp_shield_timer <= 0:
self.shield = 0
def add_shield(self, shield_type):
"""Add a shield to the player based on the shield type."""
if shield_type == 1:
if self.has_permanent_shield:
self.shield = min(self.max_shield, self.shield + 10)
else:
self.shield = self.max_shield
self.temp_shield_timer = self.temp_shield_duration
self.has_permanent_shield = False
elif shield_type == 2:
self.shield = self.max_shield
self.temp_shield_timer = 0
self.has_permanent_shield = True
def explode(self):
"""Handle the player's explosion animation."""
self.explosion_timer += self.game.dt
if self.explosion_timer >= self.explosion_speed:
self.explosion_timer = 0
if self.explosion_index < len(self.explosion_images):
self.image = self.explosion_images[self.explosion_index]
self.explosion_index += 1
else:
self.kill()
def start_explosion(self):
"""Initiate the explosion sequence."""
self.is_exploding = True
self.explosion_index = 0
self.game.sounds['explosion'].play()
self.game.explosions.add(self)
def is_explosion_complete(self):
"""Check if the explosion animation is complete."""
return self.is_exploding and self.explosion_index >= len(self.explosion_images)
class Asteroid(pygame.sprite.Sprite):
"""
Asteroid in the game. Handles its movement, rotation, and collision detection.
"""
def __init__(self, groups, pos, image_dict, game, speed_range=(100, 500)):
super().__init__(groups)
self.game = game
self.type = random.choice(['L', 'M', 'S'])
self.original_image = image_dict['asteroids'][[
'L', 'M', 'S'].index(self.type)]
self.image = self.original_image
self.rect = self.image.get_frect(center=pos)
self.mask = pygame.mask.from_surface(self.image)
self.pos = pygame.math.Vector2(self.rect.topleft)
self.direction = pygame.math.Vector2(
random.uniform(-0.5, 0.5), 1).normalize()
self.speed = random.uniform(*speed_range)
self.rotation = 0
self.rotation_speed = random.randint(20, 50)
def update(self):
"""Update the asteroid's position and rotation."""
self.pos += self.direction * self.speed * self.game.dt
self.rect.topleft = (round(self.pos.x), round(self.pos.y))
self.rotate()
if self.rect.top > self.game.screen.get_height():
self.kill()
def rotate(self):
"""Rotate the asteroid image."""
self.rotation = (
self.rotation + self.rotation_speed * self.game.dt) % 360
self.image = pygame.transform.rotate(
self.original_image, self.rotation)
self.rect = self.image.get_frect(center=self.rect.center)
self.mask = pygame.mask.from_surface(self.image)
def check_collision(self, player):
"""Check for collision with the player using mask collision."""
return pygame.sprite.collide_mask(self, player)
class Shield(pygame.sprite.Sprite):
"""
Shield powerup(s) in the game. Handles its movement and collision detection.
"""
def __init__(self, groups, pos, image_dict, game, shield_type):
super().__init__(groups)
self.game = game
self.shield_type = shield_type
self.image = image_dict['shields'][shield_type - 1]
self.rect = self.image.get_frect(center=pos)
self.mask = pygame.mask.from_surface(self.image)
self.pos = pygame.math.Vector2(self.rect.topleft)
self.direction = pygame.math.Vector2(
random.uniform(-0.5, 0.5), 1).normalize()
self.speed = random.uniform(50, 150)
def update(self):
"""Update the shield's position."""
self.pos += self.direction * self.speed * self.game.dt
self.rect.topleft = (round(self.pos.x), round(self.pos.y))
if self.rect.top > self.game.SCREEN_HEIGHT:
self.kill()
def check_collision(self, player):
"""Check for collision with the player using mask collision."""
return pygame.sprite.collide_mask(self, player)
class UI:
"""
Handles the game's user interface elements by managing the health bar, shield bar, and score display.
"""
def __init__(self, screen_width, screen_height):
self.screen_width = screen_width
self.screen_height = screen_height
self.game_font = pygame.font.Font(join('font', 'PressStart2P.ttf'))
scale_factor = 1.3
# Load and position UI elements
self.ui_slot = load_and_scale_imgs(
join('images', 'ui_Slots.png'), scale_factor)
self.ui_slot_pos = (15, 15)
self.ui_slot_rect = self.ui_slot.get_frect(topleft=self.ui_slot_pos)
self.full_health_bar = load_and_scale_imgs(
join('images', 'ui_Health.png'), scale_factor)
self.ui_health = self.full_health_bar.copy()
ui_health_offset_x, ui_health_offset_y = 47, 10
self.health_rect = self.ui_health.get_frect(
topleft=(self.ui_slot_rect.left + ui_health_offset_x,
self.ui_slot_rect.top + ui_health_offset_y))
self.health_bar_original_width = self.health_rect.width
self.full_shield_bar = load_and_scale_imgs(
join('images', 'ui_Shield.png'), scale_factor)
self.ui_shield = self.full_shield_bar.copy()
ui_shield_offset_x, ui_shield_offset_y = 47, 42
self.shield_rect = self.ui_health.get_frect(
topleft=(self.ui_slot_rect.left + ui_shield_offset_x,
self.ui_slot_rect.top + ui_shield_offset_y))
self.shield_bar_original_width = self.shield_rect.width
self.show_ui = False
self.score = 0
self.score_text = self.game_font.render(
'SCORE:0', True, (255, 255, 255))
def update_score(self, score):
"""Update the score display."""
if score != self.score:
self.score = score
self.score_text = self.game_font.render(
f'SCORE:{score}', True, (255, 255, 255))
def update_health_bar(self, player_health):
"""Update the health bar display based on player's current health."""
health_percentage = player_health / 100
new_width = int(self.health_bar_original_width * health_percentage)
self.ui_health = pygame.Surface(self.full_health_bar.get_size())
self.ui_health.blit(self.full_health_bar, (0, 0),
(0, 0, new_width, self.full_health_bar.get_height()))
def update_shield_bar(self, player_shield):
"""Update the shield bar display based on player's current shield."""
shield_percentage = player_shield / 100
new_width = int(self.shield_bar_original_width * shield_percentage)
self.ui_shield = pygame.Surface(self.full_shield_bar.get_size())
self.ui_shield.blit(self.full_shield_bar, (0, 0),
(0, 0, new_width, self.full_shield_bar.get_height()))
def draw(self, screen):
"""Draw the UI elements on the screen."""
if self.show_ui:
screen.blit(self.ui_slot, self.ui_slot_rect)
screen.blit(self.ui_health, self.health_rect)
screen.blit(self.ui_shield, self.shield_rect)
self.score_rect = self.score_text.get_frect()
self.score_rect.topright = (self.screen_width - 20, 20)
screen.blit(self.score_text, self.score_rect)
class GameState:
"""Different game states."""
LOADING = 0
PLAYING = 1
GAME_OVER = 2