Hack'ish mouse-based navigation (hopefully portable to touch devices)
authorNeil <neil@dip.sun.ac.za>
Tue, 16 Apr 2013 11:44:56 +0000 (13:44 +0200)
committerNeil <neil@dip.sun.ac.za>
Tue, 16 Apr 2013 11:44:56 +0000 (13:44 +0200)
erdslangetjie/__main__.py

index 0948e7401cea4df7e647fcf710fc1755c6487a38..8746745ccd6cecd127947db63347f7db375303c8 100644 (file)
@@ -25,6 +25,8 @@ class GameWindow(FloatLayout):
         self.level_obj.load_tiles()
         self.tiles = {}
 
+        self.mouse_move = False
+
         self.player = ThePlayer()
         self.nemesis = Nemesis()
         if not self.level_obj.enter_pos:
@@ -100,10 +102,13 @@ class GameWindow(FloatLayout):
         elif keycode[0] == pygame.K_RIGHT:
             direction = (1, 0)
         if direction:
-            self.nemesis.move(self.level_obj)
-            self.draw_nemesis()
-            self.player.move(direction, self.level_obj)
-            self.draw_player()
+            self.do_move(direction)
+
+    def do_move(self, direction):
+        self.nemesis.move(self.level_obj)
+        self.draw_nemesis()
+        self.player.move(direction, self.level_obj)
+        self.draw_player()
         self.check_state()
 
     def check_state(self):
@@ -124,6 +129,28 @@ class GameWindow(FloatLayout):
             print 'You lost!'
             sys.exit(1)
 
+    def _calc_mouse_pos(self, pos):
+        return (int(pos[0] / TILE_SIZE), int(pos[1] / TILE_SIZE))
+
+    def on_touch_down(self, touch):
+        pos = self._calc_mouse_pos(touch.pos)
+        if pos == self.player.pos:
+            self.mouse_move = True
+            self.mouse_start = pos
+
+    def on_touch_up(self, touch):
+        self.mouse_move = False
+
+    def on_touch_move(self, touch):
+        if self.mouse_move:
+            pos = self._calc_mouse_pos(touch.pos)
+            if (pos[0] - self.mouse_start[0] != 0) or (
+                    pos[1] - self.mouse_start[1] != 0):
+                direction = (pos[0] - self.mouse_start[0],
+                        pos[1] - self.mouse_start[1])
+                self.do_move(direction)
+                self.mouse_start = pos
+
 
 class GameApp(App):