1 """ Class holding the level info """
9 self.width = self.height = 0
15 self.start_pos = (0, 0)
18 self.enemy_generators = []
21 def get_neighbors(self, x, y):
22 # 4 -connected neighbors
23 return [self.tiles[y][x-1] if x > 0 else None,
24 self.tiles[y][x+1] if x < self.width - 1 else None,
25 self.tiles[y-1][x] if y > 0 else None,
26 self.tiles[y+1][x] if y < self.height - 1 else None]
28 def can_walk(self, x, y, layer):
29 if 'walk' in self.tiles[y][x][layer]['behaviour']:
31 for door in self.doors:
32 if (x, y) == door.game_pos and door.is_closed():
37 def can_fly(self, x, y, layer):
38 if 'fly' in self.tiles[y][x][layer]['behaviour']:
39 for door in self.doors:
40 if (x, y) == door.game_pos and door.is_closed():
46 def can_crawl(self, x, y, layer):
47 return 'crawl' in self.tiles[y][x][layer]['behaviour']
49 def is_keypad(self, x, y):
50 for keypad in self.keypads:
51 if (x, y) == keypad.game_pos:
55 def is_grate(self, x, y):
56 if (x, y) in self.grates:
60 def press_keypad(self, x, y, roaches):
61 for keypad in self.keypads:
62 if (x, y) == keypad.game_pos:
63 keypad.activate(roaches)
65 def get_friends(self):
66 return self._friends[:]
68 def friend_at(self, x, y):
69 for f in self.friends:
70 if f.game_pos == (x, y):
73 def remove_friend(self, friend):
74 self.friends.remove(friend)
76 def item_at(self, x, y):
78 if i.game_pos == (x, y):
81 def remove_item(self, item):
82 self.items.remove(item)
84 def is_exit(self, x, y):
85 return self.exit and (x, y) == tuple(self.exit["pos"])
87 def get_exit_level(self):
88 return self.exit["next level"]
90 def enemy_at(self, x, y):
91 for enemy in self.enemies:
92 if (x, y) == enemy.game_pos:
96 def get_enemy(self, x, y):
97 for enemy in self.enemies:
98 if (x, y) == enemy.game_pos:
102 def remove_enemy(self, enemy):
103 self.enemies.remove(enemy)
104 for generator in self.enemy_generators:
105 generator.killed(enemy)
107 def add_enemy(self, enemy, x, y):
108 """Add an enemy to an empty floor space near x, y"""
111 if self.can_walk(x, y, 'floor'):
112 if not self.friend_at(x, y):
113 if not self.enemy_at(x, y):
115 enemy.game_pos = (x, y)
116 self.enemies.append(enemy)
118 x += random.randint(-1, 1)
119 y += random.randint(-1, 1)