我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用pygame.K_KP2。
def handleMessage(self, msg): newMsg = None # Message to be sent out if msg.content['type'] is 'keydown': # Keypress if msg.content['key'] == pygame.K_KP6: # Move right newMsg = Message(MsgType.SCENE) newMsg.content = {'objID': 'player', 'action': 'move', 'param': [1,0]} if msg.content['key'] == pygame.K_KP4: # Move left newMsg = Message(MsgType.SCENE) newMsg.content = {'objID': 'player', 'action': 'move', 'param': [-1,0]} if msg.content['key'] == pygame.K_KP8: # Move up newMsg = Message(MsgType.SCENE) newMsg.content = {'objID': 'player', 'action': 'move', 'param': [0,-1]} if msg.content['key'] == pygame.K_KP2: # Move down newMsg = Message(MsgType.SCENE) newMsg.content = {'objID': 'player', 'action': 'move', 'param': [0,1]} if msg.content['key'] == pygame.K_KP9: # Move NE newMsg = Message(MsgType.SCENE) newMsg.content = {'objID': 'player', 'action': 'move', 'param': [1,-1]} if msg.content['key'] == pygame.K_KP7: # Move NW newMsg = Message(MsgType.SCENE) newMsg.content = {'objID': 'player', 'action': 'move', 'param': [-1,-1]} if msg.content['key'] == pygame.K_KP3: # Move SE newMsg = Message(MsgType.SCENE) newMsg.content = {'objID': 'player', 'action': 'move', 'param': [1,1]} if msg.content['key'] == pygame.K_KP1: # Move SW newMsg = Message(MsgType.SCENE) newMsg.content = {'objID': 'player', 'action': 'move', 'param': [-1,1]} if newMsg is not None: self.messageBus.postMessage(newMsg)
def character_key(self, key): ''' Handles the rest of the keys ''' if key == pygame.K_LSHIFT or key == pygame.K_RSHIFT: return # Skip the event for the shift key itself try: # Handle the number pad if key == pygame.K_KP0: key = pygame.K_0 elif key == pygame.K_KP1: key = pygame.K_1 elif key == pygame.K_KP2: key = pygame.K_2 elif key == pygame.K_KP3: key = pygame.K_3 elif key == pygame.K_KP4: key = pygame.K_4 elif key == pygame.K_KP5: key = pygame.K_5 elif key == pygame.K_KP6: key = pygame.K_6 elif key == pygame.K_KP7: key = pygame.K_7 elif key == pygame.K_KP8: key = pygame.K_8 elif key == pygame.K_KP9: key = pygame.K_9 elif pygame.key.get_mods() & pygame.KMOD_SHIFT: if key >= 97 and key <= 122: key = ord(chr(key).upper()) else: if KEY_TO_SHIFT_MAP.get(chr(key), None) is not None: key = ord(KEY_TO_SHIFT_MAP[chr(key)]) self.text = ''.join(( self.text[:self.cursor_position], chr(key), self.text[self.cursor_position:] )) self.cursor_position += 1 except ValueError: self.game_engine.hud.display_hint("Attempt to enter an invalid character!", 2) return