Changeset 169:b7a8f4a677e1


Ignore:
Timestamp:
Apr 5, 2011, 10:41:19 PM (9 years ago)
Author:
Simon Cross <hodgestar@…>
Branch:
default
rebase_source:
b6cde2928e837df25ef6dee0321255049f2878ad
Message:

Replace key repeating with custom tracking of fast keys.

Location:
skaapsteker
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • skaapsteker/__main__.py

    r166 r169  
    1010from . import options
    1111from .constants import SCREEN, FREQ, BITSIZE, CHANNELS, BUFFER, DEBUG
    12 from .constants import KEY_REPEAT_DELAY_MS, KEY_REPEAT_INTERVAL_MS
    1312from .engine import Engine
    1413from .levelscene import LevelScene
     
    5655    #    data.filepath('icons/nine_tales24x24.png')))
    5756    pygame.display.set_caption("Nine Tales")
    58     pygame.key.set_repeat(KEY_REPEAT_DELAY_MS, KEY_REPEAT_INTERVAL_MS)
    5957
    6058    engine = Engine()
  • skaapsteker/constants.py

    r166 r169  
    77CHANNELS = 2   # 1 == mono, 2 == stereo
    88BUFFER = 1024  # audio buffer size in no. of samples
    9 
    10 KEY_REPEAT_DELAY_MS = 250
    11 KEY_REPEAT_INTERVAL_MS = 50
    129
    1310DEBUG = True
  • skaapsteker/levelscene.py

    r165 r169  
    11"""Scene wrapping a level object."""
    22
    3 from pygame.locals import (KEYDOWN, K_DOWN, K_ESCAPE, K_LEFT, K_RIGHT,
     3from pygame.locals import (KEYDOWN, KEYUP, K_DOWN, K_ESCAPE, K_LEFT, K_RIGHT,
    44                           K_SEMICOLON, K_UP, K_q, K_x, K_z)
    55
     
    3838
    3939    def _build_action_map(self):
    40         KEY_TO_PLAYER_ACTION = {
    41             K_LEFT: 'action_left',
    42             K_RIGHT: 'action_right',
    43             K_UP: 'action_up',
    44             K_DOWN: 'action_down',
     40        action = lambda s: getattr(self._player, 'action_%s' % s)
     41
     42        self._fast_key_map = {
     43            K_LEFT: action('left'),
     44            K_RIGHT: action('right'),
     45            K_UP: action('up'),
     46            K_DOWN: action('down'),
    4547        }
    46         QUIT_KEYS = [K_ESCAPE]
     48        self._fast_keys_down = set()
    4749
     50        self._slow_key_map = {
     51            K_ESCAPE: self._quit,
     52        }
    4853        if options['dvorak']:
    49             KEY_TO_PLAYER_ACTION[K_SEMICOLON] = 'action_fire1'
    50             KEY_TO_PLAYER_ACTION[K_q] = 'action_fire2'
     54            self._slow_key_map[K_SEMICOLON] = action('fire1')
     55            self._slow_key_map[K_q] = action('fire2')
    5156        else:
    52             KEY_TO_PLAYER_ACTION[K_x] = 'action_fire1'
    53             KEY_TO_PLAYER_ACTION[K_z] = 'action_fire2'
    54             QUIT_KEYS.append(K_q)
    55 
    56         self._key_map = {}
    57         for key, action in KEY_TO_PLAYER_ACTION.items():
    58             self._key_map[key] = getattr(self._player, action)
    59         for key in QUIT_KEYS:
    60             self._key_map[key] = self._quit
     57            self._slow_key_map[K_x] = action('fire1')
     58            self._slow_key_map[K_z] = action('fire2')
     59            self._slow_key_map[K_q] = self._quit
    6160
    6261    def _quit(self):
     
    7877        if self._clip_rect is None:
    7978            self._clip_rect = pygame.Rect((0, 0), screen_surface.get_size())
     79
     80        for key in self._fast_keys_down:
     81            self._fast_key_map[key]()
    8082
    8183        self._world.update()
     
    100102    def dispatch(self, ev):
    101103        if ev.type is KEYDOWN:
    102             action = self._key_map.get(ev.key)
     104            if ev.key in self._fast_key_map:
     105                self._fast_keys_down.add(ev.key)
     106            action = self._slow_key_map.get(ev.key)
    103107            if action is not None:
    104108                action()
     109        elif ev.type is KEYUP:
     110            if ev.key in self._fast_key_map:
     111                self._fast_keys_down.discard(ev.key)
Note: See TracChangeset for help on using the changeset viewer.