From 2e932873c18650931184e250da07f522af920ac9 Mon Sep 17 00:00:00 2001 From: Neil Date: Thu, 18 Apr 2013 17:08:32 +0200 Subject: [PATCH] Make AI destory gates --- erdslangetjie/__main__.py | 2 +- erdslangetjie/level.py | 21 ++++++++++++++------- erdslangetjie/player.py | 12 +++++++++++- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/erdslangetjie/__main__.py b/erdslangetjie/__main__.py index ad07468..462d795 100644 --- a/erdslangetjie/__main__.py +++ b/erdslangetjie/__main__.py @@ -188,7 +188,7 @@ class GameWindow(RelativeLayout): self.do_nemesis_move() def do_nemesis_move(self): - self.nemesis.move(self.level_obj) + self.nemesis.move(self.level_obj, self.player.pos) self.check_state() self.reset_timer() self.draw_nemesis() diff --git a/erdslangetjie/level.py b/erdslangetjie/level.py index fc23cc6..092ef7b 100644 --- a/erdslangetjie/level.py +++ b/erdslangetjie/level.py @@ -229,7 +229,7 @@ class Level(object): return True if tile == GATE: print tile, pos, self._gates[pos] - if self._gates[pos] != -1: + if self._gates[pos] > 0: return True return False @@ -237,11 +237,20 @@ class Level(object): return abs(pos1[0] - pos2[0]) + abs(pos1[1] - pos2[1]) def is_gate(self, pos): + if not self._in_limits(pos): + return False return self._data[pos[1]][pos[0]] == GATE def is_button(self, pos): + if not self._in_limits(pos): + return False return self._data[pos[1]][pos[0]] == BUTTON + def is_wall(self, pos): + if not self._in_limits(pos): + return True + return self._data[pos[1]][pos[0]] == WALL + def trigger_button(self, pos): if not self.is_button(pos): return False @@ -256,20 +265,18 @@ class Level(object): gate_pos = cand mindist = dist if gate_pos: - self._buttons[pos] == 'pressed' + 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): - return False + return if self._gates[pos] == -1 or self._gates[pos] == 0: - return False + return self._gates[pos] = self._gates[pos] - 1 - self._fix_gate_tile(pos) - self._changed.append((pos, self.get_single_tile(pos))) - return True + self._fix_tile(pos) def get_changed_tiles(self): ret = self._changed[:] diff --git a/erdslangetjie/player.py b/erdslangetjie/player.py index e0ac771..7c80263 100644 --- a/erdslangetjie/player.py +++ b/erdslangetjie/player.py @@ -43,9 +43,10 @@ class Nemesis(FigureSprite): super(Nemesis, self).__init__() self.sprite = load_image('sprites/nemesis.png') self.reset_pos() + self.deadends = [] self.been = [] - def move(self, level): + def move(self, level, player_pos): if not self.on_board(): # Advance towards the map a step at a time self.pos = (self.pos[0] + 1, self.pos[1]) @@ -53,6 +54,15 @@ class Nemesis(FigureSprite): self.pos = level.enter_pos return False # AI goes here + # First, if we're standing next to a gate, we attack it + neighbours = [(self.pos[0] + 1, self.pos[1]), + (self.pos[0] - 1, self.pos[1]), + (self.pos[0], self.pos[1] + 1), + (self.pos[0], self.pos[1] - 1)] + for cand in neighbours: + if level.is_gate(cand) and level.blocked(cand): + level.damage_gate(cand) + return True steps = 0 self.been.append(self.pos) for direction in [(1, 0), (-1, 0), (0, -1), (0, 1)]: -- 2.34.1