4 from data import load_image, load, filepath
16 def __init__(self, levelfile):
24 # Because of how kivy's coordinate system works,
25 # we reverse the lines so things match up between
26 # the file and the display (top of file == top of display)
27 for line in reversed(levelfile.readlines()):
28 self._data.append(list(line.strip('\n')))
31 """Load the list of tiles for the level"""
38 for j, line in enumerate(self._data):
40 for i, c in enumerate(line):
42 tile_line.append(load_image('tiles/floor.png'))
44 tile_line.append(self._get_wall_tile(i, j))
47 raise RuntimeError('Multiple entry points')
48 self.enter_pos = (i, j)
49 tile_line.append(load_image('tiles/entry.png'))
51 self.exit_pos.append((i, j))
52 tile_line.append(load_image('tiles/door.png'))
54 tile_line.append('tiles/gate_down.png')
55 self._gates[(i, j)] = -1 # down
57 tile_line.append('tiles/button.png')
58 self._buttons[(i, j)] = 'active'
59 self._tiles.append(tile_line)
64 def get_single_tile(self, pos):
65 return self._tiles[pos[1]][pos[0]]
68 return len(self._tiles[0]), len(self._tiles)
70 def at_exit(self, pos):
71 return pos in self.exit_pos
73 def _get_wall_tile(self, x, y):
74 # Is the neighbour in this direction also a wall?
75 left = right = top = bottom = False
78 elif self._data[y][x - 1] == WALL:
80 if x == len(self._data[0]) - 1:
82 elif self._data[y][x + 1] == WALL:
86 elif self._data[y - 1][x] == WALL:
88 if y == len(self._data) - 1:
90 elif self._data[y + 1][x] == WALL:
92 if top and bottom and left and right:
93 return load_image('tiles/cwall.png')
94 elif bottom and left and right:
95 return load_image('tiles/bottom_wall.png')
96 elif top and left and right:
97 return load_image('tiles/top_wall.png')
98 elif top and bottom and right:
99 return load_image('tiles/left_wall.png')
100 elif top and bottom and left:
101 return load_image('tiles/right_wall.png')
103 return load_image('tiles/vert_wall.png')
105 return load_image('tiles/horiz_wall.png')
107 return load_image('tiles/corner_lt.png')
108 elif left and bottom:
109 return load_image('tiles/corner_lb.png')
111 return load_image('tiles/corner_rt.png')
112 elif right and bottom:
113 return load_image('tiles/corner_rb.png')
115 return load_image('tiles/end_top.png')
117 return load_image('tiles/end_bottom.png')
119 return load_image('tiles/end_right.png')
121 return load_image('tiles/end_left.png')
122 return load_image('tiles/pillar.png')
124 def blocked(self, pos):
130 tile = self._data[pos[1]][pos[0]]
133 if tile == WALL or tile == ENTRY:
136 if self._gates[pos] != 'down':
140 def is_gate(self, pos):
141 return self._data[pos[1]][pos[0]] == GATE
143 def is_button(self, pos):
144 return self._data[pos[1]][pos[0]] == BUTTON
146 def trigger_button(self, pos):
147 if not self.is_button(pos):
149 if not self._buttons[pos] == 'active':
151 # Find the closest gate down gate and trigger it
154 self._changed.append((pos, self.get_single_tile(pos)))
155 self._changed.append((gate_pos, self.get_single_tile(pos)))
157 def damage_gate(self, pos):
158 if not self.is_gate(pos):
160 if self._gates[pos] == -1 or self._gates[pos] == 0:
162 self._gates[pos] = self._gates[pos] - 1
163 self._fix_gate_tile(pos)
164 self._changed.append((pos, self.get_single_tile(pos)))
167 def get_changed_tiles(self):
168 ret = self._changed[:]
173 class LevelList(object):
175 LEVELS = 'level_list'
179 level_list = load(self.LEVELS)
180 for line in level_list:
182 if os.path.exists(filepath(line)):
183 level_file = load(line)
184 self.levels.append(Level(level_file))
187 print 'Level list includes non-existant level %s' % line
191 def get_current_level(self):
192 if self._cur_level < len(self.levels):
193 return self.levels[self._cur_level]
197 def advance_to_next_level(self):
199 return self.get_current_level()