Changeset 102:2e913a89e69d


Ignore:
Timestamp:
Apr 4, 2011, 6:23:14 PM (9 years ago)
Author:
Simon Cross <hodgestar@…>
Branch:
default
Message:

Link up key presses to calls to action functions on player and to quitting level.

Location:
skaapsteker
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • skaapsteker/levelscene.py

    r88 r102  
    66import sprites.player
    77import pygame
     8from pygame.locals import KEYDOWN, K_UP, K_DOWN, K_LEFT, K_RIGHT, K_x, K_z, \
     9                          K_q, K_ESCAPE
    810
    911class LevelScene(engine.Scene):
     12
     13    KEY_TO_PLAYER_ACTION = {
     14        K_LEFT: 'action_left',
     15        K_RIGHT: 'action_right',
     16        K_UP: 'action_up',
     17        K_DOWN: 'action_down',
     18        K_x: 'action_fire1',
     19        K_z: 'action_fire2',
     20    }
    1021
    1122    def __init__(self, leveldef, player=None):
     
    2637
    2738        self._world.add(self._player)
     39        self._build_action_map()
     40
     41    def _build_action_map(self):
     42        self._key_map = {}
     43        for key, action in self.KEY_TO_PLAYER_ACTION.items():
     44            self._key_map[key] = getattr(self._player, action)
     45        for key in [K_q, K_ESCAPE]:
     46            self._key_map[key] = self._quit
     47
     48    def _quit(self):
     49        import menuscene # avoid circular import
     50        engine.ChangeScene.post(menuscene.MenuScene())
    2851
    2952    def draw(self, screen_surface):
     
    3962
    4063    def dispatch(self, ev):
    41         # handle events here!
    42         self._player.dispatch(ev)
    43         pass
     64        if ev.type is KEYDOWN:
     65            action = self._key_map.get(ev.key)
     66            if action is not None:
     67                action()
  • skaapsteker/sprites/player.py

    r100 r102  
    4343        self.rect = self.image.get_rect(topleft=(pos[0]*TILE_SIZE[0], pos[1]*TILE_SIZE[1]))
    4444
     45    def action_left(self):
     46        print "L"
    4547
    46     def dispatch(self, ev):
    47         pass
     48    def action_right(self):
     49        print "R"
     50
     51    def action_up(self):
     52        print "U"
     53
     54    def action_down(self):
     55        print "D"
     56
     57    def action_fire1(self):
     58        print "F1"
     59
     60    def action_fire2(self):
     61        print "F2"
    4862
    4963    def _get_action(self):
     
    5367            return 'jumpin'
    5468        return 'standing'
    55 
    5669
    5770    def _make_key(self, action=None):
Note: See TracChangeset for help on using the changeset viewer.