Strip newlines off map when loading
[erdslangetjie.git] / erdslangetjie / level.py
1 # The level object
2
3 from data import load_image
4
5
6 class Level(object):
7
8     def __init__(self):
9         self.data = []
10         self.exit_pos = []
11         self.enter_pos = None
12         self.tiles = []
13
14     def load(self, levelfile):
15         """Load the level"""
16         self.data = []
17         for line in levelfile.readlines():
18             self.data.append(list(line.strip('\n')))
19
20     def load_tiles(self):
21         """Load the list of tiles for the level"""
22         self.tiles = []
23         self.exit_pos = []
24         self.enter_pos = None
25         for j, line in enumerate(self.data):
26             tile_line = []
27             for i, c in enumerate(line):
28                 if c == ' ':
29                     tile_line.append(load_image('tiles/floor.bmp'))
30                 elif c == '.':
31                     tile_line.append(load_image('tiles/wall.bmp'))
32                 elif c == 'E' or c == 'X':
33                     if c == 'E':
34                         if self.enter_pos:
35                             raise RuntimeError('Multiple entry points')
36                         self.enter_pos = (i, j)
37                     else:
38                         self.exit_pos.append((i, j))
39                     tile_line.append(load_image('tiles/door.bmp'))
40             self.tiles.append(tile_line)
41
42     def get_tiles(self):
43         return self.tiles
44
45     def at_exit(self, pos):
46         return pos in self.exit_pos