Esempio n. 1
0
 def render_all(state):
   for y in range(MapConstants.MAP_HEIGHT):
     for x in range(MapConstants.MAP_WIDTH):
       alive = state.game_map.game_map[x][y].alive
       targeted = state.game_map.game_map[x][y].targeted
       # it is visible
       if targeted:
         libtcod.console_set_char_background(state.con, x, y, MapConstants.COLOR_TARGETED, libtcod.BKGND_SET)
       elif alive:
         libtcod.console_set_char_background(state.con, x, y, MapConstants.COLOR_LIGHT_WALL, libtcod.BKGND_SET)
       else:
         libtcod.console_set_char_background(state.con, x, y, MapConstants.COLOR_lIGHT_GROUND, libtcod.BKGND_SET)
       state.game_map.game_map[x][y].explored = True
   libtcod.console_blit(state.con, 0, 0, MapConstants.SCREEN_WIDTH, MapConstants.SCREEN_HEIGHT, 0, 0, 0)
   # prepare to render the GUI panel
   libtcod.console_set_default_background(state.status_panel.get_panel(), libtcod.black)
   libtcod.console_clear(state.status_panel.get_panel())
   y = 1
   for (line, color) in state.status_panel.game_messages:
     libtcod.console_set_default_foreground(state.status_panel.get_panel(), color)
     libtcod.console_print_ex(state.status_panel.get_panel(), MapConstants.MSG_X, y, libtcod.BKGND_NONE, libtcod.LEFT,
       line)
     y += 1
   # state.status_panel.render_bar(1, 1, MapConstants.BAR_WIDTH, 'game_map_id', state.game_map.game_map_id,
   #                               state.player.fighter.max_hp(state),
   #                               libtcod.light_red, libtcod.darker_red)
   libtcod.console_print_ex(state.status_panel.get_panel(), 1, 4, libtcod.BKGND_NONE, libtcod.LEFT,
                            'Mode: ' + str(state.game_type))
   libtcod.console_print_ex(state.status_panel.get_panel(), 1, 5, libtcod.BKGND_NONE, libtcod.LEFT,
     'Game map id:  ' + str(state.game_map.game_map_id))
   libtcod.console_print_ex(state.status_panel.get_panel(), 1, 6, libtcod.BKGND_NONE, libtcod.LEFT,
     'Turn: ' + str(state.turn))
   # blit the contents of "panel" to the root console
   libtcod.console_blit(state.status_panel.get_panel(), 0, 0, MapConstants.SCREEN_WIDTH, MapConstants.PANEL_HEIGHT, 0,
     0, MapConstants.PANEL_Y)
Esempio n. 2
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
Esempio n. 3
0
 def render_all(state):
     if state.fov_recompute:
         # recompute FOV if needed
         state.fov_recompute = False
         if state.game_type == Constants.BATTLE:
             libtcod.map_compute_fov(
                 state.fov_map,
                 state.player.x,
                 state.player.y,
                 MapConstants.BATTLE_TORCH_RADIUS,
                 MapConstants.FOV_LIGHT_WALLS,
                 MapConstants.FOV_ALGO,
             )
         else:
             libtcod.map_compute_fov(
                 state.fov_map,
                 state.player.x,
                 state.player.y,
                 MapConstants.TORCH_RADIUS,
                 MapConstants.FOV_LIGHT_WALLS,
                 MapConstants.FOV_ALGO,
             )
         for y in range(MapConstants.MAP_HEIGHT):
             for x in range(MapConstants.MAP_WIDTH):
                 visible = libtcod.map_is_in_fov(state.fov_map, x, y)
                 wall = state.game_map.get_map()[x][y].block_sight
                 targeted = state.game_map.get_map()[x][y].targeted
                 if not visible:
                     # if not visible right now, player can only see if explored
                     if state.game_map.get_map()[x][y].explored:
                         if wall:
                             libtcod.console_set_char_background(
                                 state.con, x, y, MapConstants.COLOR_DARK_WALL, libtcod.BKGND_SET
                             )
                         elif targeted:
                             libtcod.console_set_char_background(
                                 state.con, x, y, MapConstants.COLOR_TARGETED, libtcod.BKGND_SET
                             )
                         else:
                             libtcod.console_set_char_background(
                                 state.con, x, y, MapConstants.COLOR_DARK_GROUND, libtcod.BKGND_SET
                             )
                 else:
                     # it is visible
                     if wall:
                         libtcod.console_set_char_background(
                             state.con, x, y, MapConstants.COLOR_LIGHT_WALL, libtcod.BKGND_SET
                         )
                     elif targeted:
                         libtcod.console_set_char_background(
                             state.con, x, y, MapConstants.COLOR_TARGETED, libtcod.BKGND_SET
                         )
                     else:
                         libtcod.console_set_char_background(
                             state.con, x, y, MapConstants.COLOR_lIGHT_GROUND, libtcod.BKGND_SET
                         )
                     state.game_map.get_map()[x][y].explored = True
     # draw all objects in the list
     for object in state.objects:
         if object != state.player:
             object.draw(state)
     state.player.draw(state)
     libtcod.console_blit(state.con, 0, 0, MapConstants.SCREEN_WIDTH, MapConstants.SCREEN_HEIGHT, 0, 0, 0)
     # prepare to render the GUI panel
     libtcod.console_set_default_background(state.status_panel.get_panel(), libtcod.black)
     libtcod.console_clear(state.status_panel.get_panel())
     # print the game messages, one line at a time
     y = 1
     for (line, color) in state.status_panel.game_messages:
         libtcod.console_set_default_foreground(state.status_panel.get_panel(), color)
         libtcod.console_print_ex(
             state.status_panel.get_panel(), MapConstants.MSG_X, y, libtcod.BKGND_NONE, libtcod.LEFT, line
         )
         y += 1
     # show the player's stats
     state.status_panel.render_bar(
         1,
         1,
         MapConstants.BAR_WIDTH,
         "HP",
         state.player.fighter.hp,
         state.player.fighter.max_hp(state),
         libtcod.light_red,
         libtcod.darker_red,
     )
     state.status_panel.render_bar(
         1,
         2,
         MapConstants.BAR_WIDTH,
         "MP",
         state.player.caster.mp,
         state.player.caster.max_mp(state),
         libtcod.light_blue,
         libtcod.darker_blue,
     )
     libtcod.console_print_ex(
         state.status_panel.get_panel(),
         1,
         5,
         libtcod.BKGND_NONE,
         libtcod.LEFT,
         "Player level: " + str(state.player.level),
     )
     libtcod.console_print_ex(
         state.status_panel.get_panel(),
         1,
         6,
         libtcod.BKGND_NONE,
         libtcod.LEFT,
         "Dungeon level: " + str(state.dungeon_level),
     )
     libtcod.console_print_ex(
         state.status_panel.get_panel(),
         1,
         7,
         libtcod.BKGND_NONE,
         libtcod.LEFT,
         "Game State: " + str(state.get_game_state()),
     )
     libtcod.console_print_ex(
         state.status_panel.get_panel(),
         1,
         8,
         libtcod.BKGND_NONE,
         libtcod.LEFT,
         "Player Action: " + str(state.get_player_action()),
     )
     libtcod.console_print_ex(
         state.status_panel.get_panel(), 1, 9, libtcod.BKGND_NONE, libtcod.LEFT, "Score: " + str(state.score)
     )
     # blit the contents of "panel" to the root console
     libtcod.console_blit(
         state.status_panel.get_panel(),
         0,
         0,
         MapConstants.SCREEN_WIDTH,
         MapConstants.PANEL_HEIGHT,
         0,
         0,
         MapConstants.PANEL_Y,
     )