diff options
author | Rafael Ortiz <rafael@activitycentral.com> | 2012-01-16 19:07:59 (GMT) |
---|---|---|
committer | Rafael Ortiz <rafael@activitycentral.com> | 2012-01-16 19:07:59 (GMT) |
commit | 98746b92a0274531c6d47e0d9e5362083eb6e9c1 (patch) | |
tree | 3f443e455ed9acfe2b12abdff77bfce5331b584c | |
parent | 008c34d5c9b15d3ff18d723f4a2818bbd1fcca02 (diff) |
Adds support for touchpad input. By adding this, touchscreen input is also
supported.
The functionality is to use touchpad gestures instead of
pressing the arrow keys. The mouse button must be pressed
for the gesture to be interpreted, so jumpy touchpads won't
affect play. by John Watlington wad@laptop.org
-rw-r--r-- | game.py | 33 |
1 files changed, 32 insertions, 1 deletions
@@ -137,6 +137,7 @@ class MazeGame: # clear and mark the whole screen as dirty self.screen.fill((0,0,0)) self.markRectDirty(pygame.Rect(0,0,99999,99999)) + self.mouse_in_use = 0 def markRectDirty(self, rect): """Mark an area that needs to be redrawn. This lets us @@ -181,8 +182,38 @@ class MazeGame: mesh.broadcast("move:%s,%d,%d,%d,%d" % (player.nick, player.position[0], player.position[1], player.direction[0], player.direction[1])) elif event.type == pygame.KEYUP: pass - elif event.type in (pygame.MOUSEMOTION, pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP): + elif event.type == pygame.MOUSEMOTION: pass + elif event.type == pygame.MOUSEBUTTONDOWN: + self.mouse_in_use = 1 + self.prev_mouse_pos = pygame.mouse.get_pos() + + elif event.type == pygame.MOUSEBUTTONUP: + if self.mouse_in_use: + new_mouse_pos = pygame.mouse.get_pos() + mouse_movement = ( new_mouse_pos[0] - self.prev_mouse_pos[0], + new_mouse_pos[1] - self.prev_mouse_pos[1] ) + + if ((abs(mouse_movement[0]) > 10) or + (abs(mouse_movement[1]) > 10)) : + player = self.localplayers[0] + player.hidden = False + if abs(mouse_movement[0]) > abs(mouse_movement[1]): # x movement larger + if mouse_movement[0] > 0: # direction == pygame.K_RIGHT + player.direction=(1,0) + else: # direction == pygame.K_LEFT + player.direction=(-1,0) + else: + if mouse_movement[1] < 0: # direction == pygame.K_UP + player.direction=(0,-1) + else: # direction == pygame.K_DOWN + player.direction=(0,1) + + if len(self.remoteplayers)>0: + mesh.broadcast("move:%s,%d,%d,%d,%d" % (player.nick, player.position[0], player.position[1], player.direction[0], player.direction[1])) + + self.mouse_in_use = 0 + elif event.type == mesh.CONNECT: print "Connected to the mesh." elif event.type == mesh.PARTICIPANT_ADD: |