-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
353 lines (308 loc) · 12.2 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
from typing import Union
from collections import OrderedDict
from settings import *
import pygame
from pygame.image import load
from pygame import Surface, Rect
from pygame.sprite import Group, GroupSingle, Sprite
from pygame.math import Vector2
from character import Character
from frameloader import load_frames
class Player(Character):
STATS = OrderedDict(
{
"health": {
"amount": HEALTH_BAR_WIDTH,
"cost": 100,
"max_value": 300,
"increment": 150,
},
"energy": {
"amount": ENERGY_BAR_WIDTH,
"cost": 100,
"max_value": 300,
"increment": 50,
},
"flame": {
"amount": magic_data["flame"]["strength"],
"cost": 200,
"max_value": 50,
"increment": 10,
},
"heal": {
"amount": magic_data["heal"]["strength"],
"cost": 200,
"max_value": 50,
"increment": 10,
},
"attack": {"amount": 20, "cost": 150, "max_value": 40, "increment": 10},
"speed": {"amount": SPEED, "cost": 200, "max_value": 8, "increment": 1},
}
)
def __init__(
self,
pos: tuple[int, int],
group: Union[Group, GroupSingle],
obstacle: Group,
weapon_creator,
weapon_hider,
magic_creator,
magic_destroyer,
magic_obstacle_handler,
):
super().__init__(group)
self.image: Surface = pygame.transform.scale(
load(
os.path.join(PROJECT_DIR, "graphics/player/down_idle/idle_down.png")
).convert_alpha(),
(TILESIZE, TILESIZE),
)
self.rect: Rect = self.image.get_rect(topleft=pos)
self.hitbox = self.rect.inflate(0, -20)
# Initializing player attributes
self.speed = Player.STATS["speed"]["amount"]
self.health = Player.STATS["health"]["amount"]
self.energy = Player.STATS["energy"]["amount"]
self.exp = EXP
self.obstacle_sprites = obstacle
self.invincible_duration = 1000
self.weapon_handler = WeaponHandler(weapon_creator, weapon_hider)
self.magic_handler = MagicHandler(
magic_creator, magic_destroyer, magic_obstacle_handler, self
)
self.animation_handler = AnimationHandler(self)
def update(self):
self.get_input()
self.move()
self.weapon_handler.cooldown()
self.magic_handler.cooldown()
self.animation_handler.update_status()
self.animation_handler.animate()
self.magic_handler.handle_obstacle_collision(self.obstacle_sprites)
self.handle_vulnerability()
def update_energy(self, amount: int):
self.energy += amount
if self.energy > self.STATS["energy"]["amount"]:
self.energy = self.STATS["energy"]["amount"]
elif self.energy < 0:
self.energy = 0
def update_health(self, amount: int):
self.health += amount
if self.health > self.STATS["health"]["amount"]:
self.health = self.STATS["health"]["amount"]
elif self.health < 0:
self.health = 0
def update_attribute(self, stat_name: str, update_value: int):
if stat_name in self.STATS:
stat = self.STATS[stat_name]
new_amount = stat["amount"] + update_value
if new_amount > stat["max_value"]:
new_amount = stat["max_value"]
stat["amount"] = new_amount
if stat_name == "speed":
self.speed = new_amount
if stat_name == "health":
self.update_health(new_amount - self.health)
elif stat_name == "energy":
self.update_energy(new_amount - self.energy)
stat["cost"] += stat["cost"] * 2
def get_stat_by_index(self, index: int):
key = list(self.STATS.keys())[index]
return {"attr": key, "value": self.STATS[key]}
def get_current_health(self):
return (self.health / self.STATS["health"]["amount"]) * self.STATS["health"][
"amount"
]
def get_current_energy(self):
return (self.energy / self.STATS["energy"]["amount"]) * self.STATS["energy"][
"amount"
]
def update_exp(self, exp: int):
self.exp += exp
def get_current_exp(self):
return self.exp
def get_current_status(self):
return self.animation_handler.status
def get_input(self):
keys = pygame.key.get_pressed()
if not self.weapon_handler.attacking:
self.handle_movement_input(keys)
self.handle_attack_input(keys)
self.handle_magic_input(keys)
self.handle_magic_switching(keys)
def handle_movement_input(self, keys):
if keys[pygame.K_RIGHT]:
self.direction.x = 1
self.animation_handler.status = "right"
elif keys[pygame.K_LEFT]:
self.direction.x = -1
self.animation_handler.status = "left"
else:
self.direction.x = 0
if keys[pygame.K_UP]:
self.direction.y = -1
self.animation_handler.status = "up"
elif keys[pygame.K_DOWN]:
self.direction.y = 1
self.animation_handler.status = "down"
else:
self.direction.y = 0
def handle_attack_input(self, keys):
if keys[pygame.K_SPACE]:
self.weapon_handler.attack()
def handle_magic_input(self, keys):
if keys[pygame.K_RETURN] and not self.magic_handler.magic_attacking:
self.magic_handler.attack(magic_data[self.magic_handler.magic])
def handle_magic_switching(self, keys):
if keys[pygame.K_LCTRL]:
if self.magic_handler.can_switch_magic:
self.magic_handler.switch_magic()
self.magic_handler.can_switch_magic = False
else:
self.magic_handler.can_switch_magic = True
def handle_vulnerability(self):
current_time = pygame.time.get_ticks()
if not self.vulnerable:
if current_time - self.invincibility_timer >= self.invincible_duration:
self.vulnerable = True
self.image.set_alpha(self.normalized_sine())
elif self.image.get_alpha() != 255:
self.image.set_alpha(255)
class WeaponHandler:
def __init__(self, weapon_creator, weapon_hider):
self.attacking = False
self.attack_time = 0
self.attack_delay = ATTACK_DELAY
self.create_attack = weapon_creator
self.hide_weapon = weapon_hider
self.weapon = pygame.image.load(
os.path.join(PROJECT_DIR, "graphics/weapons/sword/up.png")
)
self.weapon_attack_sound = pygame.mixer.Sound(
os.path.join(PROJECT_DIR, "audio/sword.wav")
)
self.weapon_attack_sound.set_volume(1)
def attack(self):
self.attack_time = pygame.time.get_ticks()
self.attacking = True
self.create_attack()
self.weapon_attack_sound.play()
def get_current_weapon(self):
return self.weapon
def cooldown(self):
current_time = pygame.time.get_ticks()
if (current_time - self.attack_time) >= self.attack_delay:
self.attacking = False
self.hide_weapon()
class MagicHandler:
def __init__(
self,
magic_creator,
magic_destroyer,
magic_obstacle_handler,
player,
):
self.player = player
self.magic_attacking = False
self.magic_attack_time = 0
self.magic_exist_delay = 800
self.create_magic = magic_creator
self.destroy_magic = magic_destroyer
self.handle_magic_obstacle_collision = magic_obstacle_handler
self.magic_index = 0
self.magic_list = list(magic_data.keys())
self.magic = self.magic_list[self.magic_index]
self.magic_image = pygame.image.load(magic_data[self.magic]["image"])
self.can_switch_magic = True
self.change_to_idle = False
def attack(self, magic_entity):
magic_cost = magic_data[self.magic]["cost"]
self.magic_attack_time = pygame.time.get_ticks()
self.magic_attacking = True
self.create_magic(magic_entity)
self.player.update_energy(-magic_cost)
def switch_magic(self):
self.magic_index += 1
self.magic = self.magic_list[self.magic_index % len(self.magic_list)]
self.magic_image = pygame.image.load(magic_data[self.magic]["image"])
self.can_switch_magic = False
def cooldown(self):
current_time = pygame.time.get_ticks()
magic_time_diff = current_time - self.magic_attack_time
if magic_time_diff >= self.magic_exist_delay:
self.magic_attacking = False
self.change_to_idle = False
self.destroy_magic()
elif magic_time_diff >= self.magic_exist_delay // 4:
self.change_to_idle = True
def handle_obstacle_collision(self, obstacle_sprites):
self.handle_magic_obstacle_collision()
def get_current_magic(self):
return self.magic_image
class AnimationHandler:
def __init__(self, player):
self.player = player
self.status = "down"
self.frame_index = 0
self.animation_speed = 0.2
self.animations = {
"right_idle": load_frames(
os.path.join(PROJECT_DIR, "graphics/player/right_idle/")
),
"left_idle": load_frames(
os.path.join(PROJECT_DIR, "graphics/player/left_idle/")
),
"up_idle": load_frames(
os.path.join(PROJECT_DIR, "graphics/player/up_idle/")
),
"down_idle": load_frames(
os.path.join(PROJECT_DIR, "graphics/player/down_idle/")
),
"right_attack": load_frames(
os.path.join(PROJECT_DIR, "graphics/player/right_attack/")
),
"left_attack": load_frames(
os.path.join(PROJECT_DIR, "graphics/player/left_attack/")
),
"up_attack": load_frames(
os.path.join(PROJECT_DIR, "graphics/player/up_attack/")
),
"down_attack": load_frames(
os.path.join(PROJECT_DIR, "graphics/player/down_attack/")
),
"down": load_frames(os.path.join(PROJECT_DIR, "graphics/player/down/")),
"up": load_frames(os.path.join(PROJECT_DIR, "graphics/player/up/")),
"left": load_frames(os.path.join(PROJECT_DIR, "graphics/player/left/")),
"right": load_frames(os.path.join(PROJECT_DIR, "graphics/player/right/")),
}
def update_status(self):
if self.player.direction.x == 0 and self.player.direction.y == 0:
if "idle" not in self.status and "attack" not in self.status:
self.status = self.status + "_idle"
if self.player.weapon_handler.attacking:
self.player.direction = self.player.direction * 0
if "attack" not in self.status:
if "_idle" in self.status:
self.status = self.status.replace("_idle", "_attack")
else:
self.status = self.status + "_attack"
elif "attack" in self.status:
self.status = self.status.replace("_attack", "_idle")
if (
self.player.magic_handler.magic_attacking
and not self.player.magic_handler.change_to_idle
):
if "attack" not in self.status:
if "_idle" in self.status:
self.status = self.status.replace("_idle", "_attack")
else:
self.status += "_attack"
elif self.player.magic_handler.change_to_idle:
self.status = self.status.replace("_attack", "_idle")
def animate(self):
animation = self.animations[self.status]
self.frame_index += self.animation_speed
if self.frame_index >= len(animation):
self.frame_index = 0
self.player.image = animation[int(self.frame_index)]
self.player.rect = self.player.image.get_rect(center=self.player.hitbox.center)