From: Neil Date: Tue, 16 Apr 2013 11:44:56 +0000 (+0200) Subject: Hack'ish mouse-based navigation (hopefully portable to touch devices) X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=2d169e25d7944e4c4e051648e3866a1987f9f0a6;p=erdslangetjie.git Hack'ish mouse-based navigation (hopefully portable to touch devices) --- 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):