Ejemplo n.º 1
0
    def display_menu(self, header, options, width, con, override_height=None, option_char=True):
        if len(options) > 26:
            raise ValueError("Cannot have a menu with more than 26 options.")
        # calculate total height for the header (after auto-wrap) and one line per option
        header_height = libtcod.console_get_height_rect(con, 0, 0, width, MapConstants.SCREEN_HEIGHT, header)
        if header == "":
            header_height = 0
        height = len(options) + header_height + override_height
        if override_height:
            height = override_height

        # create an off-screen console that represents the menu's window
        window = libtcod.console_new(width, height)

        # print the header, with auto-wrap
        libtcod.console_set_default_foreground(window, libtcod.white)
        libtcod.console_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, header)

        # print all the options
        y = header_height
        letter_index = ord("a")
        for option_text in options:
            if option_char:
                text = "(" + chr(letter_index) + ") " + str(option_text)
            else:
                text = str(option_text)
            libtcod.console_print_ex(window, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, text)
            y += 1
            letter_index += 1

        # blit the contents of "window" to the root console
        x = MapConstants.SCREEN_WIDTH / 2 - width / 2
        y = MapConstants.SCREEN_HEIGHT / 2 - height / 2
        libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 1.0)

        # present the root console to the player and wait for a key-press
        libtcod.console_flush()
        key = libtcod.console_wait_for_keypress(True)

        if key.vk == libtcod.KEY_ENTER and key.lalt:  # (special case) Alt+Enter: toggle fullscreen
            libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())

        # convert the ASCII code to an index; if it corresponds to an option, return it
        index = key.c - ord("a")
        if index >= 0 and index < len(options):
            return index
        return None
Ejemplo n.º 2
0
  def handle_keys(state):
    key = Input.wait_for_keypress(state)
    while key.vk == libtcod.KEY_SHIFT:
      key = Input.wait_for_keypress(state)

    if key.vk == libtcod.KEY_ENTER and key.lalt:  # Toggle fullscreen
      libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
    elif key.vk == libtcod.KEY_ESCAPE:
      state.set_player_action(Constants.EXIT)

    game_state = state.get_game_state()
    if game_state == Constants.PLAYING:
      Input.handle_playing_keys(key, state)
    elif game_state == Constants.TARGETING:
      Input.handle_targeting_keys(key, state)
    elif game_state == Constants.FOUND_TARGET:
      state.set_game_state(Constants.FOUND_TARGET)

    if state.get_player_action() == Constants.DID_NOT_TAKE_TURN:
      state.set_player_action(None)
    elif state.get_player_action() == Constants.NOT_VALID_KEY:
      state.set_player_action(Constants.DID_NOT_TAKE_TURN)