-
Notifications
You must be signed in to change notification settings - Fork 0
/
items.py
504 lines (393 loc) · 17.4 KB
/
items.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
import random
import pygame
from collections import defaultdict
from typing import Any
from support import BASE_IMG_PATH, load_image
from data import EQUIPMENT, EQUIPMENTS_CATEGORIES, BOOKS
from projectile import Necromancy
class GameLoot:
"""
The class represents the gaming items like a potions, scrolls, coins, gems.
"""
def __init__(self, game, pos, size, i_type, name=None):
"""
Initializes the GameLoot object.
:param game: game class instance
:param pos: spawn position on map
:param size: the size of the object's rectangle for intercepting collisions with it
:param i_type: type of GameLoot object item
"""
self.game = game
self.i_type = i_type
self.name = name
self.animation = self.game.assets['loot/' + self.i_type].copy() if self.game else None
self.pos = list(pos) if pos else None
self.size = size
self.flip = False
self.rect = pygame.Rect(self.pos[0], self.pos[1], self.size[0], self.size[1]) if self.pos else None
def __eq__(self, other):
return self.name == other.name
def __hash__(self):
return hash(self.name)
def update(self):
self.animation.update()
player_stuff = {
'coin': 'money',
'gem': 'artifacts_remaining',
'glass_red': 'heal_potions',
'glass_blue': 'magic_potions',
'glass_green': 'stamina_potions',
'glass_yellow': 'power_potions',
}
scrolls = {
'holly_scroll': 'holly_spell',
'bloodlust_scroll': 'bloodlust_spell',
'speed_scroll': 'speed_spell',
'invulnerability_scroll': 'invulnerability_spell',
}
keys = ['steel_key', 'red_key', 'bronze_key', 'purple_key', 'gold_key']
for loot_item in self.game.loot.copy():
if loot_item.rect.colliderect(self.game.player.rect()):
self.game.sfx[loot_item.i_type].play()
if loot_item.i_type in player_stuff:
if loot_item.i_type == 'gem':
self.game.artifacts_remaining -= 1
elif loot_item.i_type == 'coin':
self.game.player.money += 1
else:
player_thing = player_stuff[loot_item.i_type]
setattr(self.game.player, player_thing, getattr(self.game.player, player_thing) + 1)
elif loot_item.i_type in scrolls:
scroll_name = scrolls[loot_item.i_type]
if scroll_name in self.game.player.scrolls:
self.game.player.scrolls[scroll_name] += 1
else:
self.game.player.scrolls[scroll_name] = 1
elif loot_item.i_type in keys:
self.game.player.keys[loot_item.i_type] += 1
self.game.loot.remove(loot_item)
def render(self, surf, offset=(0, 0)):
surf.blit(pygame.transform.flip(self.animation.current_sprite(), self.flip, False),
(self.pos[0] - offset[0], self.pos[1] - 2 - offset[1]))
class Gem(GameLoot):
def __init__(self, game, pos, size):
super().__init__(game, pos, size, 'gem')
self.name = 'Gem'
class Coin(GameLoot):
def __init__(self, game, pos, size):
super().__init__(game, pos, size, 'coin')
self.name = 'Coin'
# Potions
class HealthPoison(GameLoot):
def __init__(self, game=None, pos=None, size=None):
super().__init__(game, pos, size, 'glass_red')
self.name = 'Health Potion'
self.price = 3
self.pic = BASE_IMG_PATH + 'ui/merchant/health_potion.png'
self.description = ''
class MagicPoison(GameLoot):
def __init__(self, game=None, pos=None, size=None):
super().__init__(game, pos, size, 'glass_blue')
self.name = 'Magic Potion'
self.price = 3
self.pic = BASE_IMG_PATH + 'ui/merchant/mana_potion.png'
self.description = ''
class StaminaPoison(GameLoot):
def __init__(self, game=None, pos=None, size=None):
super().__init__(game, pos, size, 'glass_green')
self.name = 'Stamina Poison'
self.price = 2
self.pic = BASE_IMG_PATH + 'ui/merchant/stamina_potion.png'
self.description = ''
class PowerPoison(GameLoot):
def __init__(self, game=None, pos=None, size=None):
super().__init__(game, pos, size, 'glass_yellow')
self.name = 'PowerPoison'
self.price = 6
self.pic = BASE_IMG_PATH + 'ui/merchant/power_potion.png'
self.description = ''
# scrolls
class HollyScroll(GameLoot):
def __init__(self, game=None, pos=None, size=None):
super().__init__(game, pos, size, 'holly_scroll')
self.name = 'Holly Scroll'
self.price = 10
self.pic = BASE_IMG_PATH + 'ui/scrolls/holly_scroll.png'
self.description = ''
class BloodlustScroll(GameLoot):
def __init__(self, game=None, pos=None, size=None):
super().__init__(game, pos, size, 'bloodlust_scroll')
self.name = 'Bloodlust Scroll'
self.price = 5
self.pic = BASE_IMG_PATH + 'ui/scrolls/bloodlust_scroll.png'
self.description = ''
class SpeedScroll(GameLoot):
def __init__(self, game=None, pos=None, size=None):
super().__init__(game, pos, size, 'speed_scroll')
self.name = 'Speed Scroll'
self.price = 5
self.pic = BASE_IMG_PATH + 'ui/scrolls/speed_scroll.png'
self.description = ''
class InvulnerabilityScroll(GameLoot):
def __init__(self, game=None, pos=None, size=None):
super().__init__(game, pos, size, 'invulnerability_scroll')
self.name = 'Invulnerability Scroll'
self.price = 10
self.pic = BASE_IMG_PATH + 'ui/scrolls/invulnerability_scroll.png'
self.description = ''
def create_scroll():
available_scrolls = [BloodlustScroll, SpeedScroll, InvulnerabilityScroll, HollyScroll, SpeedScroll, HollyScroll]
random.shuffle(available_scrolls)
scrolls = []
for scroll in available_scrolls[:6]:
scrolls.append(scroll())
return scrolls
def create_poison():
available_poisons = [HealthPoison, MagicPoison, StaminaPoison, PowerPoison, HealthPoison, StaminaPoison]
random.shuffle(available_poisons)
poisons = []
for poison in available_poisons[:6]:
poisons.append(poison())
return poisons
class Book:
def __init__(self, title):
self.book = BOOKS.get(title, 'default_book')
self.book_name = self.book.get('book_name')
self.branch = self.book.get('branch')
self.lvl = self.book.get('level')
self.price = self.book.get('price')
self.prologue = self.book.get('prologue')
self.features = self.book.get('features')
self.image = self.book.get('image')
self.pic = self.book.get('pic')
class DungeonShadows(Book, GameLoot):
def __init__(self, game, pos=(0, 0), size=(0, 0)):
Book.__init__(self, title='dungeon_shadows')
GameLoot.__init__(self, game, pos, size, i_type='dungeon_shadows')
def read(self):
self.game.player.necromancy = True
self.game.effects.append(Necromancy(self.game, self.game.player.hitbox.midtop, 0))
class ForgottenSouls(Book, GameLoot):
def __init__(self, game, pos=(0, 0), size=(0, 0)):
Book.__init__(self, title='forgotten_souls')
GameLoot.__init__(self, game, pos, size, i_type='forgotten_souls')
def read(self):
print('The House of Forgotten Souls Book read')
class BridgeEternity(Book, GameLoot):
def __init__(self, game, pos=(0, 0), size=(0, 0)):
Book.__init__(self, title='bridge_to_eternity')
GameLoot.__init__(self, game, pos, size, i_type='bridge_to_eternity')
def read(self):
print('Bridge to Eternity')
class WhispersAfterlife(Book, GameLoot):
def __init__(self, game, pos=(0, 0), size=(0, 0)):
Book.__init__(self, title='whispers_of_afterlife')
GameLoot.__init__(self, game, pos, size, i_type='whispers_of_afterlife')
def read(self):
print('Whispers of the Afterlife Book read')
class Necronomicon(Book, GameLoot):
def __init__(self, game, pos=(0, 0), size=(0, 0)):
Book.__init__(self, title='necronomicon')
GameLoot.__init__(self, game, pos, size, i_type='necronomicon')
def read(self):
print('The Necronomicon of Arcata')
class Equipment:
"""
The class represents game equipment like weapon, armor, jewelry, and other.
"""
def __init__(self, name='', e_type='', e_class=0, rarity='', condition=0, pic=None, price=0, properties=None):
self.name = name
self.e_type = e_type
self.e_class = e_class
self.rarity = rarity
self.properties = properties
self.condition = condition
self.current_condition = self.condition
self.pic = pic
self.price = price
self.increase_defence = self.properties.get('defence', 0)
self.increase_damage = self.properties.get('damage', 0)
self.increase_health = self.properties.get('health', 0)
self.increase_stamina = self.properties.get('stamina', 0)
self.increase_mana = self.properties.get('mana', 0)
self.increase_experience = self.properties.get('experience', 0)
self.distance_damage = self.properties.get('distance_damage', 0)
def __eq__(self, other):
return self.name == other.name
def __hash__(self):
return hash(self.name)
def load_default_equipment():
"""
Creates instances of the Equipment class and passes the equipment defined by default
at the beginning of the game to the "equipment" dictionary of the Player class instance.
"""
return {item["e_type"]: Equipment(**item) for item in DEFAULT_EQUIPMENT.values()}
# cache for equipment items
equipment_cache: defaultdict[str, Any] = defaultdict(dict)
def create_equipment(name=None, rareness=None):
"""
Creates specific or random instances of the Equipment class in the game when it needed.
"""
if name:
if name in equipment_cache:
return equipment_cache[name]
elif name in EQUIPMENT:
equipment_instance = Equipment(**EQUIPMENT[name])
equipment_cache[name] = equipment_instance
return equipment_instance
else:
raise ValueError("An object of the Equipment class with this name does not exist.")
else:
rarity = rareness if rareness else random.choices(['Common', 'Rare', 'Unique', 'Epic', 'Legendary', 'Mythical'], weights=[80, 12, 5, 2, 0.9, 0.1], k=1)[0]
random_item = random.choice(EQUIPMENTS_CATEGORIES[rarity])
if random_item in equipment_cache:
return equipment_cache[random_item]
else:
equipment_instance = Equipment(**EQUIPMENT[random_item])
equipment_cache[random_item] = equipment_instance
return equipment_instance
# Factory function to create books
def create_book(game, book=None, pos=(0, 0), size=(0, 0)):
library = {
'dungeon_shadows': DungeonShadows,
'forgotten_souls': ForgottenSouls,
'bridge_to_eternity': BridgeEternity,
'whispers_of_afterlife': WhispersAfterlife,
'necronomicon': Necronomicon
}
if book:
return library.get(book)(game, pos, size)
else:
random_book = random.choice(list(library.keys()))
return library[random_book](game)
class Chest:
def __init__(self, game, pos, size, lock=None, chest_class='common'):
self.game = game
self.pos = list(pos)
self.size = size
self.lock = lock
self.is_opened = False
self.animation = self.game.assets['chest/' + chest_class].copy()
self.chest_close = self.animation.images[0]
self.chest_opened = self.animation.images[-1]
self.rect = pygame.Rect(self.pos[0], self.pos[1], self.size[0], self.size[1])
self.message_panel = pygame.image.load(BASE_IMG_PATH + 'tiles/chest/panel/msg_panel.png')
self.font = pygame.font.Font('data/fonts/simple.ttf', 14)
self.keys_map = {
"steel_key": 'Rare', "red_key": 'Unique', "bronze_key": 'Epic', "purple_key": 'Legendary',
"gold_key": 'Mythical'
}
def open(self):
if not self.is_opened:
if self.lock is None or self.game.player.keys[self.lock] > 0:
self.is_opened = True
self.game.sfx['chest_open'].play()
if self.lock is not None:
self.game.player.keys[self.lock] -= 1
self.get_item()
else:
self.game.sfx['lock_closed'].play()
def get_item(self):
# Generate and add equipment to player's inventory
if self.lock:
equipment = create_equipment(rareness=self.keys_map[self.lock]) # Create equipment based on chest class
else:
equipment = create_equipment() # Create random equipment
additional_item = create_book(self.game)
self.game.player.inventory.append(equipment)
self.game.player.inventory.append(additional_item)
self.game.inventory_menu.refresh_inventory()
self.game.sfx['get_item'].play()
def update(self):
if self.is_opened and not self.animation.done:
self.animation.update()
def render(self, surf, offset=(0, 0)):
window_offset_x = 40
window_offset_y = 40
msg_offset_x = 28
msg_offset_y = 30
if not self.is_opened:
# Render closed chest
surf.blit(self.chest_close, (self.pos[0] - offset[0], self.pos[1] - offset[1]))
if self.rect.colliderect(self.game.player.rect()):
surf.blit(self.message_panel, (self.pos[0] - window_offset_x - offset[0], self.pos[1] - window_offset_y - offset[1]))
if self.lock is None or self.game.player.keys[self.lock] > 0:
message = self.font.render("TO OPEN PRESS X", True, (189, 165, 139))
surf.blit(message, (self.pos[0] - msg_offset_x - offset[0], self.pos[1] - msg_offset_y - offset[1]))
else:
message = self.font.render(f"NEED A {self.lock.replace('_', ' ').upper()}", True, (189, 165, 139))
surf.blit(message, (self.pos[0] - msg_offset_x - offset[0], self.pos[1] - msg_offset_y - offset[1]))
else:
# Render open chest
if self.animation.done:
surf.blit(self.chest_opened, (self.pos[0] - offset[0], self.pos[1] - offset[1]))
else:
surf.blit(self.animation.current_sprite(), (self.pos[0] - offset[0], self.pos[1] - offset[1]))
# pygame.draw.rect(surf, (0, 255, 0), (self.rect.x - offset[0], self.rect.y - offset[1],
# self.rect.width, self.rect.height), 1)
# chests
class CommonChest(Chest):
def __init__(self, game, pos, size):
super().__init__(game, pos, size)
class RareChest(Chest):
def __init__(self, game, pos, size):
super().__init__(game, pos, size, lock='steel_key', chest_class='rare')
class UniqueChest(Chest):
def __init__(self, game, pos, size):
super().__init__(game, pos, size, lock='red_key', chest_class='unique')
class EpicChest(Chest):
def __init__(self, game, pos, size):
super().__init__(game, pos, size, lock='bronze_key', chest_class='epic')
class LegendaryChest(Chest):
def __init__(self, game, pos, size):
super().__init__(game, pos, size, lock='purple_key', chest_class='legendary')
class MythicalChest(Chest):
def __init__(self, game, pos, size):
super().__init__(game, pos, size, lock='gold_key', chest_class='mythical')
# keys
class SteelKey(GameLoot):
def __init__(self, game, pos, size):
super().__init__(game, pos, size, 'steel_key')
class RedKey(GameLoot):
def __init__(self, game, pos, size):
super().__init__(game, pos, size, 'red_key')
class BronzeKey(GameLoot):
def __init__(self, game, pos, size):
super().__init__(game, pos, size, 'bronze_key')
class PurpleKey(GameLoot):
def __init__(self, game, pos, size):
super().__init__(game, pos, size, 'purple_key')
class GoldKey(GameLoot):
def __init__(self, game, pos, size):
super().__init__(game, pos, size, 'gold_key')
class Merchant:
"""
Class representing traders and merchants in the game.
"""
def __init__(self, game, pos, size=(32, 32)):
self.game = game
self.animation = self.game.assets['merchant'].copy()
self.pos = list(pos)
self.size = size
self.flip = False
self.rect = pygame.Rect(self.pos[0], self.pos[1], self.size[0] + 16, self.size[1] + 16)
self.stuff = self.generate_stuff()
@staticmethod
def generate_stuff():
stuff = create_scroll() + create_poison()
while len(stuff) < 36:
item = create_equipment()
if item not in stuff:
stuff.append(item)
return [stuff[i:i + 6] for i in range(0, len(stuff), 6)]
def look_stuff(self):
self.game.merchant_window.stuff = self.stuff
self.game.player.trading = True
def update(self):
self.animation.update()
for trader in self.game.merchants.copy():
if trader.rect.colliderect(self.game.player.rect()):
pass
def render(self, surf, offset=(0, 0)):
surf.blit(pygame.transform.flip(self.animation.current_sprite(), self.flip, False),
(self.pos[0] - offset[0], self.pos[1] - offset[1]))