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 can_bullet(self, x, y, layer):
50 if self.tiles[y][x][layer]['behaviour']:
51 for door in self.doors:
52 if (x, y) == door.game_pos and door.is_closed():
57 def is_keypad(self, x, y):
58 for keypad in self.keypads:
59 if (x, y) == keypad.game_pos:
63 def is_grate(self, x, y):
64 if (x, y) in self.grates:
68 def press_keypad(self, x, y, roaches):
69 for keypad in self.keypads:
70 if (x, y) == keypad.game_pos:
71 keypad.activate(roaches)
73 def get_friends(self):
74 return self._friends[:]
76 def friend_at(self, x, y):
77 for f in self.friends:
78 if f.game_pos == (x, y):
81 def remove_friend(self, friend):
82 self.friends.remove(friend)
84 def item_at(self, x, y):
86 if i.game_pos == (x, y):
89 def remove_item(self, item):
90 self.items.remove(item)
92 def is_exit(self, x, y):
93 return self.exit and (x, y) == tuple(self.exit["pos"])
95 def get_exit_level(self):
96 return self.exit["next level"]
98 def enemy_at(self, x, y):
99 for enemy in self.enemies:
100 if (x, y) == enemy.game_pos:
104 def get_enemy(self, x, y):
105 for enemy in self.enemies:
106 if (x, y) == enemy.game_pos:
110 def remove_enemy(self, enemy):
111 self.enemies.remove(enemy)
112 for generator in self.enemy_generators:
113 generator.killed(enemy)
115 def add_enemy(self, enemy, x, y):
116 """Add an enemy to an empty floor space near x, y"""
119 if self.can_walk(x, y, 'floor'):
120 if not self.friend_at(x, y):
121 if not self.enemy_at(x, y):
123 enemy.game_pos = (x, y)
124 self.enemies.append(enemy)
126 x += random.randint(-1, 1)
127 y += random.randint(-1, 1)