Changeset 265:7628467eecd9


Ignore:
Timestamp:
04/08/11 13:06:24 (12 years ago)
Author:
Neil Muller <drnlmuller@…>
Branch:
default
Phase:
public
Rebase:
37373330663536663231336666346463393061386132333531656239643136316439616364653036
Message:

Add double-key tap support

Location:
skaapsteker
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • skaapsteker/constants.py

    r169 r265  
    1212EPSILON = 1e-10
    1313
     14# Time parameter for double key taps checked.
     15# This is for both key down time and for gap between taps
     16DOUBLE_TAP_TIME = 0.15
     17
    1418# Layer defination
    1519class Layers(object):
  • skaapsteker/levelscene.py

    r264 r265  
    55
    66import pygame
     7import time
    78
    89from . import options
     
    1011from . import level
    1112from . import physics
     13from . import constants
    1214from .sprites import player
    1315from .widgets.text import Text
     
    4244        self._build_action_map()
    4345
    44 
    4546    def setup_player(self):
    4647        doorway = self._level.doorways[self.game_state.world.player.doorway]
     
    5051        self._player.set_pos(doorway._starting_tile_pos)
    5152
     53        # Double tap stuff
     54        self._last_keys_down = []
     55        self._last_keydown_time = None
     56        self._last_keyup_time = None
    5257
    5358    def _build_action_map(self):
     
    7681            self._slow_key_map[K_z] = action('fire2')
    7782            self._slow_key_map[K_q] = self._quit
     83
     84        self._key_tap_map = {
     85                (K_LEFT, K_LEFT) : action('double_left'),
     86                (K_RIGHT, K_RIGHT) : action('double_right'),
     87                (K_DOWN, K_DOWN) : action('double_down'),
     88                }
    7889
    7990    def _quit(self, pause=True):
     
    181192            if action is not None:
    182193                action()
     194            self._last_keydown_time = time.time()
     195            if self._last_keyup_time and (time.time() - self._last_keyup_time) < constants.DOUBLE_TAP_TIME:
     196                # Part of a possible sequence
     197                self._last_keys_down.append(ev.key)
     198                self._last_keys_down = self._last_keys_down[-4:]
     199            else:
     200                # Start a new sequence
     201                self._last_keys_down = [ev.key]
     202                if action is not None:
     203                    action()
    183204        elif ev.type is KEYUP:
    184205            if ev.key in self._fast_key_map:
    185206                self._fast_keys_down.discard(ev.key)
     207            if time.time() - self._last_keydown_time < constants.DOUBLE_TAP_TIME and self._last_keys_down and ev.key == self._last_keys_down[-1]:
     208                self._last_keyup_time = time.time()
     209                # Still in possible sequence
     210                action = self._key_tap_map.get(tuple(self._last_keys_down))
     211                if action is not None:
     212                    action()
     213            else:
     214                # No longer meet the conditions for a tap sequence
     215                self._last_keys_down = []
     216                self._last_keyup_time = None
     217
    186218        elif engine.PlayerDied.matches(ev):
    187219            self._player_dead = True
Note: See TracChangeset for help on using the changeset viewer.