Tweak window size
[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         # Because of how kivy's coordinate system works,
18         # we reverse the lines so things match up between
19         # the file and the display (top of file == top of display)
20         for line in reversed(levelfile.readlines()):
21             self.data.append(list(line.strip('\n')))
22
23     def load_tiles(self):
24         """Load the list of tiles for the level"""
25         self.tiles = []
26         self.exit_pos = []
27         self.enter_pos = None
28         for j, line in enumerate(self.data):
29             tile_line = []
30             for i, c in enumerate(line):
31                 if c == ' ':
32                     tile_line.append(load_image('tiles/floor.bmp'))
33                 elif c == '.':
34                     tile_line.append(load_image('tiles/wall.bmp'))
35                 elif c == 'E' or c == 'X':
36                     if c == 'E':
37                         if self.enter_pos:
38                             raise RuntimeError('Multiple entry points')
39                         self.enter_pos = (i, j)
40                     else:
41                         self.exit_pos.append((i, j))
42                     tile_line.append(load_image('tiles/door.bmp'))
43             self.tiles.append(tile_line)
44
45     def get_tiles(self):
46         return self.tiles
47
48     def at_exit(self, pos):
49         return pos in self.exit_pos
50
51     def blocked(self, pos):
52         if pos[0] < 0:
53             return True
54         if pos[1] < 0:
55             return True
56         try:
57             tile = self.data[pos[1]][pos[0]]
58         except IndexError:
59             return True
60         if tile == '.':
61             return True
62         return False