From 6b9b0ccf122251850a75970305531bc4b4e8fca8 Mon Sep 17 00:00:00 2001 From: Neil Date: Wed, 17 Apr 2013 21:28:00 +0200 Subject: [PATCH] Hook up very, very stupid nemesis movement for testing logic --- erdslangetjie/__main__.py | 50 ++++++++++++++++++++++++--------------- erdslangetjie/player.py | 17 ++++++++++++- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/erdslangetjie/__main__.py b/erdslangetjie/__main__.py index aa261e4..db6669e 100644 --- a/erdslangetjie/__main__.py +++ b/erdslangetjie/__main__.py @@ -9,6 +9,7 @@ from kivy.uix.scrollview import ScrollView from kivy.uix.label import Label from kivy.graphics import Color, Rectangle from kivy.utils import platform +from kivy.clock import Clock from erdslangetjie.level import LevelList from erdslangetjie.data import load_image @@ -42,6 +43,7 @@ class GameWindow(RelativeLayout): raise RuntimeError('No entry point') self.player_tile = None self.nemesis_tile = None + self.timer_set = False self.player.pos = self.level_obj.enter_pos if platform() != 'android': @@ -172,42 +174,52 @@ class GameWindow(RelativeLayout): def do_move(self, direction): if not self.level_obj: return - self.nemesis.move(self.level_obj) - self.draw_nemesis() self.player.move(direction, self.level_obj) self.draw_player() self.check_state() - self.reset_timer() + if not self.timer_set: + self.reset_timer() - def timed_move(self): + def timed_move(self, event): if not self.level_obj: return - self.nemesis.move(self.level_obj) + self.nemesis.move(self.level_obj, self.check_caught) self.draw_nemesis() self.check_state() self.reset_timer() def reset_timer(self): - pass + self.timer_set = True + Clock.unschedule(self.timed_move) + Clock.schedule_once(self.timed_move, 0.5) + + def check_caught(self): + return self.nemesis.pos == self.player.pos + + def reset_level(self): + Clock.unschedule(self.timed_move) + self.timer_set = False + self.remove_widget(self.nemesis_tile) + self.nemesis.reset_pos() + if self.level_obj: + self.level_obj.load_tiles() + self.player.pos = self.level_obj.enter_pos + self.remove_widget(self.player_tile) + self.view.scroll_x = 0 + self.view.scroll_y = 0 + self.build() + self.draw_nemesis() + self.draw_player() + return True + return False def check_state(self): if self.level_obj.at_exit(self.player.pos): # Jump to next level self.level_obj = self.level_list.advance_to_next_level() - self.remove_widget(self.nemesis_tile) - self.nemesis.reset_pos() - if self.level_obj: - self.level_obj.load_tiles() - self.player.pos = self.level_obj.enter_pos - self.remove_widget(self.player_tile) - self.view.scroll_x = 0 - self.view.scroll_y = 0 - self.build() - self.draw_nemesis() - self.draw_player() - else: + if not self.reset_level(): self.app.game_over(True) - elif self.nemesis.pos == self.player.pos: + elif self.check_caught(): # Caught self.app.game_over(False) diff --git a/erdslangetjie/player.py b/erdslangetjie/player.py index 5bb6472..fd44554 100644 --- a/erdslangetjie/player.py +++ b/erdslangetjie/player.py @@ -43,8 +43,9 @@ class Nemesis(FigureSprite): super(Nemesis, self).__init__() self.sprite = load_image('sprites/nemesis.png') self.reset_pos() + self.been = [] - def move(self, level): + def move(self, level, caught): if not self.on_board(): # Advance towards the map a step at a time self.pos = (self.pos[0] + 1, self.pos[1]) @@ -52,6 +53,20 @@ class Nemesis(FigureSprite): self.pos = level.enter_pos return False # AI goes here + steps = 0 + self.been.append(self.pos) + for direction in [(1, 0), (-1, 0), (0, -1), (0, 1)] * 2: + new_pos = (self.pos[0] + direction[0], self.pos[1] + direction[1]) + if self.can_move(direction, level) and new_pos not in self.been: + self.pos = new_pos + self.been.append(new_pos) + steps += 1 + if steps >= 2: + break + if caught(): + break + if steps == 0: + self.been = [] return True def reset_pos(self): -- 2.34.1