Changeset 100:96bdfadeb461


Ignore:
Timestamp:
Sep 2, 2013, 10:01:25 AM (7 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
Message:

Cleaner direction key management.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • nagslang/screens/area.py

    r97 r100  
    1313
    1414class ControlKeys(object):
     15    direction_keys = {
     16        (0, 1): set([pygame.locals.K_UP, pygame.locals.K_w]),
     17        (0, -1): set([pygame.locals.K_DOWN, pygame.locals.K_s]),
     18        (-1, 0): set([pygame.locals.K_LEFT, pygame.locals.K_a]),
     19        (1, 0): set([pygame.locals.K_RIGHT, pygame.locals.K_d]),
     20    }
     21
    1522    def __init__(self):
    1623        self.keys_down = set()
     
    2734        elif ev.type == pygame.locals.KEYUP:
    2835            self.key_up(ev.key)
     36
     37    def get_direction(self):
     38        dx, dy = 0, 0
     39        for (tx, ty), keys in self.direction_keys.iteritems():
     40            if self.keys_down & keys:
     41                dx += tx
     42                dy += ty
     43        return (dx, dy)
    2944
    3045
     
    96111
    97112    def tick_protagonist(self):
    98         dx, dy = 0, 0
    99         for key, tx, ty in [
    100                 # Arrows
    101                 (pygame.locals.K_UP, 0, 1), (pygame.locals.K_DOWN, 0, -1),
    102                 (pygame.locals.K_LEFT, -1, 0), (pygame.locals.K_RIGHT, 1, 0),
    103                 # WASD
    104                 (pygame.locals.K_w, 0, 1), (pygame.locals.K_s, 0, -1),
    105                 (pygame.locals.K_a, -1, 0), (pygame.locals.K_d, 1, 0),
    106         ]:
    107             if key in self.keys.keys_down:
    108                 dx += tx
    109                 dy += ty
     113        dx, dy = self.keys.get_direction()
    110114        self.protagonist.set_direction(dx, dy)
    111115
Note: See TracChangeset for help on using the changeset viewer.