# HG changeset patch # User Simon Cross # Date 1302043279 -7200 # Node ID b7a8f4a677e1fb3bab6ce293dc5689f717534256 # Parent 77274af740918ff243b57f5b3458307465d4c4cd Replace key repeating with custom tracking of fast keys. diff -r 77274af74091 -r b7a8f4a677e1 skaapsteker/__main__.py --- a/skaapsteker/__main__.py Wed Apr 06 00:41:50 2011 +0200 +++ b/skaapsteker/__main__.py Wed Apr 06 00:41:19 2011 +0200 @@ -9,7 +9,6 @@ from . import options from .constants import SCREEN, FREQ, BITSIZE, CHANNELS, BUFFER, DEBUG -from .constants import KEY_REPEAT_DELAY_MS, KEY_REPEAT_INTERVAL_MS from .engine import Engine from .levelscene import LevelScene from .menuscene import MenuScene @@ -55,7 +54,6 @@ #pygame.display.set_icon(pygame.image.load( # data.filepath('icons/nine_tales24x24.png'))) pygame.display.set_caption("Nine Tales") - pygame.key.set_repeat(KEY_REPEAT_DELAY_MS, KEY_REPEAT_INTERVAL_MS) engine = Engine() if level is not None: diff -r 77274af74091 -r b7a8f4a677e1 skaapsteker/constants.py --- a/skaapsteker/constants.py Wed Apr 06 00:41:50 2011 +0200 +++ b/skaapsteker/constants.py Wed Apr 06 00:41:19 2011 +0200 @@ -7,9 +7,6 @@ CHANNELS = 2 # 1 == mono, 2 == stereo BUFFER = 1024 # audio buffer size in no. of samples -KEY_REPEAT_DELAY_MS = 250 -KEY_REPEAT_INTERVAL_MS = 50 - DEBUG = True EPSILON = 1e-10 diff -r 77274af74091 -r b7a8f4a677e1 skaapsteker/levelscene.py --- a/skaapsteker/levelscene.py Wed Apr 06 00:41:50 2011 +0200 +++ b/skaapsteker/levelscene.py Wed Apr 06 00:41:19 2011 +0200 @@ -1,6 +1,6 @@ """Scene wrapping a level object.""" -from pygame.locals import (KEYDOWN, K_DOWN, K_ESCAPE, K_LEFT, K_RIGHT, +from pygame.locals import (KEYDOWN, KEYUP, K_DOWN, K_ESCAPE, K_LEFT, K_RIGHT, K_SEMICOLON, K_UP, K_q, K_x, K_z) from . import options @@ -37,27 +37,26 @@ self._build_action_map() def _build_action_map(self): - KEY_TO_PLAYER_ACTION = { - K_LEFT: 'action_left', - K_RIGHT: 'action_right', - K_UP: 'action_up', - K_DOWN: 'action_down', + action = lambda s: getattr(self._player, 'action_%s' % s) + + self._fast_key_map = { + K_LEFT: action('left'), + K_RIGHT: action('right'), + K_UP: action('up'), + K_DOWN: action('down'), } - QUIT_KEYS = [K_ESCAPE] + self._fast_keys_down = set() + self._slow_key_map = { + K_ESCAPE: self._quit, + } if options['dvorak']: - KEY_TO_PLAYER_ACTION[K_SEMICOLON] = 'action_fire1' - KEY_TO_PLAYER_ACTION[K_q] = 'action_fire2' + self._slow_key_map[K_SEMICOLON] = action('fire1') + self._slow_key_map[K_q] = action('fire2') else: - KEY_TO_PLAYER_ACTION[K_x] = 'action_fire1' - KEY_TO_PLAYER_ACTION[K_z] = 'action_fire2' - QUIT_KEYS.append(K_q) - - self._key_map = {} - for key, action in KEY_TO_PLAYER_ACTION.items(): - self._key_map[key] = getattr(self._player, action) - for key in QUIT_KEYS: - self._key_map[key] = self._quit + self._slow_key_map[K_x] = action('fire1') + self._slow_key_map[K_z] = action('fire2') + self._slow_key_map[K_q] = self._quit def _quit(self): import menuscene # avoid circular import @@ -78,6 +77,9 @@ if self._clip_rect is None: self._clip_rect = pygame.Rect((0, 0), screen_surface.get_size()) + for key in self._fast_keys_down: + self._fast_key_map[key]() + self._world.update() self._update_clip_rect() @@ -99,6 +101,11 @@ def dispatch(self, ev): if ev.type is KEYDOWN: - action = self._key_map.get(ev.key) + if ev.key in self._fast_key_map: + self._fast_keys_down.add(ev.key) + action = self._slow_key_map.get(ev.key) if action is not None: action() + elif ev.type is KEYUP: + if ev.key in self._fast_key_map: + self._fast_keys_down.discard(ev.key)