-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
229 lines (180 loc) · 7.02 KB
/
player.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
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
import pathlib
import pygame
from toolbox import floor_to_multiple
from settings import (
FPS,
HALF_TILE,
SPRITES_PATH,
TILE_SIZE,
DEFAULT_POWERUP_STATS,
WIDTH,
HEIGHT,
)
from bomb import Bomb
from animation import Animation
class Player(pygame.sprite.Sprite):
def __init__(
self,
pos,
groups,
image_name,
obstacle_sprites,
bomb_sprites,
powerup_sprites,
explosion_sprites,
destructive_wall_sprites,
name,
):
super().__init__(groups)
self.player_group = groups
self.image = pygame.image.load(pathlib.Path(SPRITES_PATH, image_name))
self.image = pygame.transform.scale(self.image, (HALF_TILE, HALF_TILE))
self.rect = self.image.get_rect(topleft=pos)
self.rect.x += HALF_TILE // 2 # Offset
self.rect.y += HALF_TILE // 2
self.direction = pygame.math.Vector2()
self.obstacle_sprites = obstacle_sprites
self.bomb_sprites = bomb_sprites
self.explosion_sprites = explosion_sprites
self.destructive_wall_sprites = destructive_wall_sprites
self.bomb_delay = 0
self.bomb_reload = 5
self.respawn_point = self.rect.topleft
self.stats = DEFAULT_POWERUP_STATS.copy()
self.powerup_sprites = powerup_sprites
self.current_bombs = 0
self.wifi_timer = FPS / 4 # 1/4 of frame (15ms in 60fps)
self.idle_animation = Animation(name, 2, 13, HALF_TILE)
self.walk_animation = Animation(name + "_walk_", 7, 13, HALF_TILE)
self.sounds = {
"kick": pygame.mixer.Sound(pathlib.Path("sounds", "Kick.wav")),
"dies": pygame.mixer.Sound(pathlib.Path("sounds", "Dies.wav")),
"explosion": pygame.mixer.Sound(
pathlib.Path("sounds", "Bomb Explodes.wav")
),
"place_bomb": pygame.mixer.Sound(pathlib.Path("sounds", "Place Bomb.wav")),
}
self.is_dead = False # add
self.name = name # add
def explode_bomb_wifi(self):
"""Explodes the player's bombs."""
if self.stats["wifi_explode"] is False:
return
if self.current_bombs == 0 or self.wifi_timer > 0:
return
self.wifi_timer = FPS / 4 # reset the timer
# get bombs from the player
for bomb in self.bomb_sprites:
if bomb.player == self:
bomb.explode()
break
def update_wifi_timer(self):
"""Updates the wifi timer."""
if self.stats["wifi_explode"] is False:
return
if self.wifi_timer > 0:
self.wifi_timer -= 1
def spawn_bomb(self):
"""Spawns a bomb at the player's position."""
if self.current_bombs >= self.stats["max_bombs"]:
return
if self.current_bombs == 1: # first bomb has no cooldown
self.bomb_delay = 0
if self.bomb_delay > 0:
self.bomb_delay -= 1
return
x_pos = floor_to_multiple(self.rect.x, TILE_SIZE)
y_pos = floor_to_multiple(self.rect.y, TILE_SIZE)
for bomb in self.bomb_sprites: # Check if there's already a bomb there
if (bomb.rect.x == x_pos) and (bomb.rect.y == y_pos):
return
# if already a player in the position, don't spawn a bomb
for player in self.player_group[0]:
if player == self:
continue
if (floor_to_multiple(player.rect.x, TILE_SIZE) == x_pos) and (
floor_to_multiple(player.rect.y, TILE_SIZE) == y_pos
):
return
self.sounds["place_bomb"].play()
Bomb(
(x_pos, y_pos),
[self.bomb_sprites],
self.obstacle_sprites,
self.explosion_sprites,
self.destructive_wall_sprites,
self.player_group,
self,
)
self.bomb_delay = self.bomb_reload
self.current_bombs += 1
def input(self):
"""Handles player input."""
raise NotImplementedError("You need to implement this method.")
def move(self):
"""Moves the player."""
if self.direction.x == 1:
self.walk_animation.mirrored = False
self.walk_animation.play(self)
elif self.direction.x == -1:
self.walk_animation.mirrored = True
self.walk_animation.play(self)
elif self.direction.y == -1 or self.direction.y == 1:
self.walk_animation.play(self)
else:
self.idle_animation.mirrored = self.walk_animation.mirrored
self.idle_animation.play(self)
if self.direction.magnitude() != 0:
self.direction = self.direction.normalize()
self.rect.x += int(self.direction.x * self.stats["speed"])
self.collision("horizontal", self.obstacle_sprites)
self.collision("horizontal", self.destructive_wall_sprites)
self.rect.y += int(self.direction.y * self.stats["speed"])
self.collision("vertical", self.obstacle_sprites)
self.collision("vertical", self.destructive_wall_sprites)
def collision(self, direction, obstacles):
"""Checks for collisions with obstacles."""
if direction == "vertical":
objects_hit = pygame.sprite.spritecollide(self, obstacles, False)
for sprite in objects_hit:
if self.direction.y > 0: # BAIXO
self.rect.bottom = sprite.rect.top
elif self.direction.y < 0: # CIMA
self.rect.top = sprite.rect.bottom
if direction == "horizontal":
objects_hit = pygame.sprite.spritecollide(self, obstacles, False)
for sprite in objects_hit:
if self.direction.x > 0: # DIREITA
self.rect.right = sprite.rect.left
elif self.direction.x < 0: # ESQUERDA
self.rect.left = sprite.rect.right
def powerup(self):
"""Checks for collisions with powerups."""
objects_hit = pygame.sprite.spritecollide(self, self.powerup_sprites, True)
for sprite in objects_hit:
sprite.apply(self.stats)
sprite.kill()
print(self.stats)
def explosions(self):
"""Checks for collisions with explosions and respawns player"""
explosions_hit = pygame.sprite.spritecollide(
self, self.explosion_sprites, False
)
if explosions_hit: # if the player is hit by an explosion
self.is_dead = True
self.rect.topleft = pygame.math.Vector2(WIDTH, HEIGHT)
# play death sound
self.sounds["dies"].play()
def respawn(self):
self.rect.topleft = self.respawn_point
self.stats = DEFAULT_POWERUP_STATS.copy()
self.current_bombs = 0
def update(self):
"""Main player loop that runs every frame."""
if self.is_dead:
return
self.input()
self.move()
self.powerup()
self.explosions()
self.update_wifi_timer()