def target_tile(max_range=None): #return the position of a tile left-clicked in player's FOV (optionally in a range), or (None,None) if right-clicked. while True: #render the screen. this erases the inventory and shows the names of objects under the mouse. render_graphics() libtcod.console_flush() key = libtcod.console_check_for_keypress() mouse = libtcod.mouse_get_status() #get mouse position and click status (x, y) = (mouse.cx, mouse.cy) if mouse.rbutton_pressed or key.vk == libtcod.KEY_ESCAPE: return (None, None) #cancel if the player right-clicked or pressed Escape #accept the target if the player clicked in FOV, and in case a range is specified, if it's in that range if (mouse.lbutton_pressed and libtcod.map_is_in_fov(fov_map, x, y) and (max_range is None or player.distance(x, y) <= max_range)): return (x, y)
def target_tile(max_range=None): #return the position of a tile left-clicked in player's FOV (optionally in a range), or (None,None) if right-clicked. while True: #render the screen. this erases the inventory and shows the names of objects under the mouse. render_graphics() libtcod.console_flush() key = libtcod.console_check_for_keypress() mouse = libtcod.mouse_get_status( ) #get mouse position and click status (x, y) = (mouse.cx, mouse.cy) if mouse.rbutton_pressed or key.vk == libtcod.KEY_ESCAPE: return (None, None ) #cancel if the player right-clicked or pressed Escape #accept the target if the player clicked in FOV, and in case a range is specified, if it's in that range if (mouse.lbutton_pressed and libtcod.map_is_in_fov(fov_map, x, y) and (max_range is None or player.distance(x, y) <= max_range)): return (x, y)
def handle_input(): key = libtcod.console_check_for_keypress(libtcod.KEY_PRESSED) mouse = libtcod.mouse_get_status() (x, y) = (mouse.cx, mouse.cy) 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 'exit' #exit game elif chr(key.c) == 'r': reset() if game_state == 'playing': #movement keys if key.vk == libtcod.KEY_KP8 or key.vk == libtcod.KEY_UP: player_move_or_attack(0, -1) elif key.vk == libtcod.KEY_KP2 or key.vk == libtcod.KEY_DOWN: player_move_or_attack(0, 1) elif key.vk == libtcod.KEY_KP4 or key.vk == libtcod.KEY_LEFT: player_move_or_attack(-1, 0) elif key.vk == libtcod.KEY_KP6 or key.vk == libtcod.KEY_RIGHT: player_move_or_attack(1, 0) elif key.vk == libtcod.KEY_KP7: player_move_or_attack(-1, -1) elif key.vk == libtcod.KEY_KP9: player_move_or_attack(1, -1) elif key.vk == libtcod.KEY_KP1: player_move_or_attack(-1, 1) elif key.vk == libtcod.KEY_KP3: player_move_or_attack(1, 1) #if nearest player neighbor is clicked elif mouse.lbutton: angle = math.atan2(player.y-y, player.x-x)/math.pi*180 if angle > 157.5: player_move_or_attack(1, 0) elif 157.5 > angle > 112.5: player_move_or_attack(1, -1) elif 112.5 > angle > 67.5: player_move_or_attack(0, -1) elif 67.5 > angle > 22.5: player_move_or_attack(-1, -1) elif 22.5 > angle > -22.5: player_move_or_attack(-1, 0) elif -22.5 > angle > -67.5: player_move_or_attack(-1, 1) elif -67.5 > angle > -112.5: player_move_or_attack(0, 1) elif -112.5 > angle > -157.5: player_move_or_attack(1, 1) elif -157.5 > angle: player_move_or_attack(1, 0) else: #test for other keys key_char = chr(key.c) if key_char == 'n': for object in objects: #look for an item in the player's tile if object.x == player.x and object.y == player.y and isinstance(object, Object): if object.name == 'hole': next_level() break else: no_hole = True if no_hole == True: message('There is no hole down there.') if key_char == 'g': #pick up an item for object in objects: #look for an item in the player's tile if object.x == player.x and object.y == player.y and isinstance(object, Item): object.pick_up() break if key_char == 'i': #show the inventory; if an item is selected, use it chosen_item = inventory_menu('Press the key next to an item to use it, or any other to cancel.\n') if chosen_item is not None: chosen_item.use() if key_char == 'd': #show the inventory; if an item is selected, drop it chosen_item = inventory_menu('Press the key next to an item to drop it, or any other to cancel.\n') if chosen_item is not None: chosen_item.drop() return 'didnt-take-turn'
def handle_input(): key = libtcod.console_check_for_keypress(libtcod.KEY_PRESSED) mouse = libtcod.mouse_get_status() (x, y) = (mouse.cx, mouse.cy) 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 'exit' #exit game elif chr(key.c) == 'r': reset() if game_state == 'playing': #movement keys if key.vk == libtcod.KEY_KP8 or key.vk == libtcod.KEY_UP: player_move_or_attack(0, -1) elif key.vk == libtcod.KEY_KP2 or key.vk == libtcod.KEY_DOWN: player_move_or_attack(0, 1) elif key.vk == libtcod.KEY_KP4 or key.vk == libtcod.KEY_LEFT: player_move_or_attack(-1, 0) elif key.vk == libtcod.KEY_KP6 or key.vk == libtcod.KEY_RIGHT: player_move_or_attack(1, 0) elif key.vk == libtcod.KEY_KP7: player_move_or_attack(-1, -1) elif key.vk == libtcod.KEY_KP9: player_move_or_attack(1, -1) elif key.vk == libtcod.KEY_KP1: player_move_or_attack(-1, 1) elif key.vk == libtcod.KEY_KP3: player_move_or_attack(1, 1) #if nearest player neighbor is clicked elif mouse.lbutton: angle = math.atan2(player.y - y, player.x - x) / math.pi * 180 if angle > 157.5: player_move_or_attack(1, 0) elif 157.5 > angle > 112.5: player_move_or_attack(1, -1) elif 112.5 > angle > 67.5: player_move_or_attack(0, -1) elif 67.5 > angle > 22.5: player_move_or_attack(-1, -1) elif 22.5 > angle > -22.5: player_move_or_attack(-1, 0) elif -22.5 > angle > -67.5: player_move_or_attack(-1, 1) elif -67.5 > angle > -112.5: player_move_or_attack(0, 1) elif -112.5 > angle > -157.5: player_move_or_attack(1, 1) elif -157.5 > angle: player_move_or_attack(1, 0) else: #test for other keys key_char = chr(key.c) if key_char == 'n': for object in objects: #look for an item in the player's tile if object.x == player.x and object.y == player.y and isinstance( object, Object): if object.name == 'hole': next_level() break else: no_hole = True if no_hole == True: message('There is no hole down there.') if key_char == 'g': #pick up an item for object in objects: #look for an item in the player's tile if object.x == player.x and object.y == player.y and isinstance( object, Item): object.pick_up() break if key_char == 'i': #show the inventory; if an item is selected, use it chosen_item = inventory_menu( 'Press the key next to an item to use it, or any other to cancel.\n' ) if chosen_item is not None: chosen_item.use() if key_char == 'd': #show the inventory; if an item is selected, drop it chosen_item = inventory_menu( 'Press the key next to an item to drop it, or any other to cancel.\n' ) if chosen_item is not None: chosen_item.drop() return 'didnt-take-turn'