- if c == ' ':
- tile_line.append(load_image('tiles/floor.png'))
- elif c == '.':
- tile_line.append(load_image('tiles/wall.bmp'))
- elif c == 'E' or c == 'X':
- if c == 'E':
- if self.enter_pos:
- raise RuntimeError('Multiple entry points')
- self.enter_pos = (i, j)
- else:
- self.exit_pos.append((i, j))
- tile_line.append(load_image('tiles/door.png'))
- self.tiles.append(tile_line)
+ tile_image = self._get_tile_image((i, j), c)
+ tile_line.append(tile_image)
+ self._tiles.append(tile_line)
+
+ def _get_tile_image(self, pos, c):
+ image = None
+ if c == FLOOR:
+ image = load_image('tiles/floor.png')
+ elif c == WALL:
+ image = self._get_wall_tile(pos)
+ elif c == ENTRY:
+ self.enter_pos = pos
+ image = load_image('tiles/entry.png')
+ elif c == EXIT:
+ self.exit_pos.append(pos)
+ image = load_image('tiles/door.png')
+ elif c == GATE:
+ if pos not in self._gates:
+ self._gates[pos] = -1 # down
+ image = load_image('tiles/gate_down.png')
+ else:
+ state = self._gates[pos]
+ if state == -1:
+ image = load_image('tiles/gate_down.png')
+ elif state == 0:
+ # destroyed
+ image = load_image('tiles/floor.png')
+ elif state == 1:
+ # badly damaged
+ image = load_image('tiles/gate_dented.png')
+ elif state == 2:
+ # lightly damaged
+ image = load_image('tiles/gate_bent.png')
+ else:
+ # gate up
+ image = load_image('tiles/gate_up.png')
+ elif c == BUTTON:
+ if not pos in self._buttons:
+ image = load_image('tiles/button.png')
+ self._buttons[pos] = 'active'
+ elif self._buttons[pos] == 'active':
+ image = load_image('tiles/button.png')
+ else:
+ image = load_image('tiles/floor.png')
+ if image is None:
+ raise RuntimeError('Unknown tile type %s at %s' % (c, pos))
+ return image
+
+ def validate(self):
+ entry_points = 0
+ exit_points = 0
+ gates = 0
+ buttons = 0
+ for line in self._data:
+ if ENTRY in line:
+ entry_points += line.count(ENTRY)
+ if EXIT in line:
+ exit_points += line.count(EXIT)
+ if BUTTON in line:
+ buttons += line.count(BUTTON)
+ if GATE in line:
+ gates += line.count(GATE)
+ if entry_points == 0:
+ raise RuntimeError('No entry point')
+ if entry_points > 1:
+ raise RuntimeError('Multiple entry points')
+ if exit_points == 0:
+ raise RuntimeError('No exit')
+ if gates != buttons:
+ raise RuntimeError('The number of buttons and gates differ')