4 from data import load_image, load, filepath
14 def __init__(self, levelfile):
19 # Because of how kivy's coordinate system works,
20 # we reverse the lines so things match up between
21 # the file and the display (top of file == top of display)
22 for line in reversed(levelfile.readlines()):
23 self.data.append(list(line.strip('\n')))
26 """Load the list of tiles for the level"""
30 for j, line in enumerate(self.data):
32 for i, c in enumerate(line):
34 tile_line.append(load_image('tiles/floor.png'))
36 tile_line.append(self.get_wall_tile(i, j))
37 elif c == ENTRY or c == EXIT:
40 raise RuntimeError('Multiple entry points')
41 self.enter_pos = (i, j)
43 self.exit_pos.append((i, j))
44 tile_line.append(load_image('tiles/door.png'))
45 self.tiles.append(tile_line)
51 return len(self.tiles[0]), len(self.tiles)
53 def at_exit(self, pos):
54 return pos in self.exit_pos
56 def get_wall_tile(self, x, y):
57 # Is the neighbour in this direction also a wall?
58 left = right = top = bottom = False
61 elif self.data[y][x - 1] == WALL:
63 if x == len(self.data[0]) - 1:
65 elif self.data[y][x + 1] == WALL:
69 elif self.data[y - 1][x] == WALL:
71 if y == len(self.data) - 1:
73 elif self.data[y + 1][x] == WALL:
75 if top and bottom and left and right:
76 return load_image('tiles/cwall.png')
77 elif bottom and left and right:
78 return load_image('tiles/bottom_wall.png')
79 elif top and left and right:
80 return load_image('tiles/top_wall.png')
81 elif top and bottom and right:
82 return load_image('tiles/left_wall.png')
83 elif top and bottom and left:
84 return load_image('tiles/right_wall.png')
86 return load_image('tiles/vert_wall.png')
88 return load_image('tiles/horiz_wall.png')
90 return load_image('tiles/corner_lt.png')
92 return load_image('tiles/corner_lb.png')
94 return load_image('tiles/corner_rt.png')
95 elif right and bottom:
96 return load_image('tiles/corner_rb.png')
98 return load_image('tiles/end_top.png')
100 return load_image('tiles/end_bottom.png')
102 return load_image('tiles/end_right.png')
104 return load_image('tiles/end_left.png')
105 return load_image('tiles/pillar.png')
107 def blocked(self, pos):
113 tile = self.data[pos[1]][pos[0]]
121 class LevelList(object):
123 LEVELS = 'level_list'
127 level_list = load(self.LEVELS)
128 for line in level_list:
130 if os.path.exists(filepath(line)):
131 level_file = load(line)
132 self.levels.append(Level(level_file))
135 print 'Level list includes non-existant level %s' % line
139 def get_current_level(self):
140 if self._cur_level < len(self.levels):
141 return self.levels[self._cur_level]
145 def advance_to_next_level(self):
147 return self.get_current_level()