def handle_keys(): global playerx, playery #key = libtcod.console_check_for_keypress() #real-time key = libtcod.console_wait_for_keypress(True) #turn-based if key.vk == libtcod.KEY_ENTER and key.lalt: #Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif key.vk == libtcod.KEY_ESCAPE: return True #exit game #movement keys if libtcod.console_is_key_pressed(libtcod.KEY_UP): playery -= 1 elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN): playery += 1 elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT): playerx -= 1 elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT): playerx += 1
def _handle_key(self): # Configure for turn-based key = cod.console_wait_for_keypress(True) key_char = chr(key.c) # Alt+Enter: toggle fullscreen if key.vk == cod.KEY_ENTER and key.lalt: cod.console_set_fullscreen(not cod.console_is_fullscreen()) return KeyResult() # Exit Game elif key_char == 'q': return KeyResult(end_game=True) # Movement dx = dy = None if cod.console_is_key_pressed(cod.KEY_UP) or key_char == 'k': dx = 0 dy = -1 elif cod.console_is_key_pressed(cod.KEY_DOWN) or key_char == 'j': dx = 0 dy = 1 elif cod.console_is_key_pressed(cod.KEY_LEFT) or key_char == 'h': dx = -1 dy = 0 elif cod.console_is_key_pressed(cod.KEY_RIGHT) or key_char == 'l': dx = 1 dy = 0 elif key_char == 'y': dx = -1 dy = -1 elif key_char == 'u': dx = 1 dy = -1 elif key_char == 'b': dx = -1 dy = 1 elif key_char == 'n': dx = 1 dy = 1 if dx is not None: return self._handle_player_move_or_attack(dx, dy) # Unbound key pressed return KeyResult()