# gate up
image = load_image('tiles/gate_up.png')
elif c == BUTTON:
- image = load_image('tiles/button.png')
- self._buttons[pos] = 'active'
+ 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')
def get_tiles(self):
return self._tiles
(pos[0] - 1, pos[1] + 1), (pos[0] + 1, pos[1] - 1)]:
if not self._in_limits(new_pos):
continue
- tile = self._data[new_pos[1]][new_pos[0]]
- new_tile = self._get_tile_image(new_pos, tile)
- self._tiles[new_pos[1]][new_pos[0]] = new_tile
- self._changed.append((new_pos, new_tile))
+ # Update display to changed status
+ self._fix_tile(new_pos)
+
+ def _fix_tile(self, pos):
+ tile = self._data[pos[1]][pos[0]]
+ new_tile = self._get_tile_image(pos, tile)
+ self._tiles[pos[1]][pos[0]] = new_tile
+ self._changed.append((pos, new_tile))
def get_size(self):
return len(self._tiles[0]), len(self._tiles)
if tile == WALL or tile == ENTRY:
return True
if tile == GATE:
+ print tile, pos, self._gates[pos]
if self._gates[pos] != -1:
return True
return False
+ def calc_dist(self, pos1, pos2):
+ return abs(pos1[0] - pos2[0]) + abs(pos1[1] - pos2[1])
+
def is_gate(self, pos):
return self._data[pos[1]][pos[0]] == GATE
if not self._buttons[pos] == 'active':
return False
# Find the closest gate down gate and trigger it
- gate_pos = pos
-
- self._changed.append((pos, self.get_single_tile(pos)))
- self._changed.append((gate_pos, self.get_single_tile(pos)))
+ mindist = 99999
+ gate_pos = None
+ for cand in self._gates:
+ dist = self.calc_dist(pos, cand)
+ if dist < mindist:
+ gate_pos = cand
+ mindist = dist
+ if gate_pos:
+ self._buttons[pos] == 'pressed'
+ self._gates[gate_pos] = 3 # Raise gate
+ self._fix_tile(pos)
+ self._fix_tile(gate_pos)
def damage_gate(self, pos):
if not self.is_gate(pos):