From 2d169e25d7944e4c4e051648e3866a1987f9f0a6 Mon Sep 17 00:00:00 2001 From: Neil Date: Tue, 16 Apr 2013 13:44:56 +0200 Subject: [PATCH] Hack'ish mouse-based navigation (hopefully portable to touch devices) --- erdslangetjie/__main__.py | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/erdslangetjie/__main__.py b/erdslangetjie/__main__.py index 0948e74..8746745 100644 --- a/erdslangetjie/__main__.py +++ b/erdslangetjie/__main__.py @@ -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): -- 2.34.1