Hook up very, very stupid nemesis movement for testing logic
authorNeil <neil@dip.sun.ac.za>
Wed, 17 Apr 2013 19:28:00 +0000 (21:28 +0200)
committerNeil <neil@dip.sun.ac.za>
Wed, 17 Apr 2013 19:28:00 +0000 (21:28 +0200)
erdslangetjie/__main__.py
erdslangetjie/player.py

index aa261e447d0ae52feb4ba678befc5960aa5bbda2..db6669e83ff966a75ef6bcb18cdbdd7e01404831 100644 (file)
@@ -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)
 
index 5bb6472b1b88fca7b942d28a60e97e8eb35690ff..fd445549ed530acbf1f71cb5d8e20af3dbedda08 100644 (file)
@@ -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):