-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_handler.py
More file actions
50 lines (42 loc) · 1.78 KB
/
object_handler.py
File metadata and controls
50 lines (42 loc) · 1.78 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
from sprite_object import *
from npc import *
from random import choices, randrange
class ObjectHandler:
def __init__(self, game):
self.game = game
self.sprite_list = []
self.npc_list = []
self.npc_sprite_path = 'resources/sprites/npc/'
self.static_sprite_path = 'resources/sprites/static_sprites/'
self.anim_sprite_path = 'resources/sprites/animated_sprites/'
add_sprite = self.add_sprite
add_npc = self.add_npc
self.npc_positions = {}
# spawn npc
self.enemies = 20 # npc count
self.npc_types = [SoldierNPC, CacoDemonNPC, CyberDemonNPC]
self.weights = [70, 20, 10]
self.restricted_area = {(i, j) for i in range(10) for j in range(10)}
self.spawn_npc()
def spawn_npc(self):
for i in range(self.enemies):
npc = choices(self.npc_types, self.weights)[0]
pos = x, y = randrange(self.game.map.cols), randrange(self.game.map.rows)
while (pos in self.game.map.world_map) or (pos in self.restricted_area):
pos = x, y = randrange(self.game.map.cols), randrange(self.game.map.rows)
self.add_npc(npc(self.game, pos=(x + 0.5, y + 0.5)))
def check_win(self):
if not len(self.npc_positions):
self.game.object_renderer.win()
pg.display.flip()
pg.time.delay(1500)
self.game.new_game()
def update(self):
self.npc_positions = {npc.map_pos for npc in self.npc_list if npc.alive}
[sprite.update() for sprite in self.sprite_list]
[npc.update() for npc in self.npc_list]
self.check_win()
def add_npc(self, npc):
self.npc_list.append(npc)
def add_sprite(self, sprite):
self.sprite_list.append(sprite)