def test_console_printing(console, fg, bg): libtcodpy.console_set_background_flag(console, libtcodpy.BKGND_SET) assert (libtcodpy.console_get_background_flag(console) == libtcodpy.BKGND_SET) libtcodpy.console_set_alignment(console, libtcodpy.LEFT) assert (libtcodpy.console_get_alignment(console) == libtcodpy.LEFT) libtcodpy.console_print(console, 0, 0, 'print') libtcodpy.console_print_ex(console, 0, 0, libtcodpy.BKGND_SET, libtcodpy.LEFT, 'print ex') assert (libtcodpy.console_print_rect( console, 0, 0, 8, 8, 'print rect') > 0 ) assert (libtcodpy.console_print_rect_ex( console, 0, 0, 8, 8, libtcodpy.BKGND_SET, libtcodpy.LEFT, 'print rect ex') > 0 ) assert (libtcodpy.console_get_height_rect( console, 0, 0, 8, 8, 'get height') > 0 ) libtcodpy.console_set_color_control(libtcodpy.COLCTRL_1, fg, bg)
def test_console_printing(console, fg, bg): libtcodpy.console_set_background_flag(console, libtcodpy.BKGND_SET) assert (libtcodpy.console_get_background_flag(console) == libtcodpy.BKGND_SET) libtcodpy.console_set_alignment(console, libtcodpy.LEFT) assert (libtcodpy.console_get_alignment(console) == libtcodpy.LEFT) libtcodpy.console_print(console, 0, 0, 'print') libtcodpy.console_print_ex(console, 0, 0, libtcodpy.BKGND_SET, libtcodpy.LEFT, 'print ex') assert (libtcodpy.console_print_rect( console, 0, 0, 8, 8, 'print rect') > 0 ) assert (libtcodpy.console_print_rect_ex( console, 0, 0, 8, 8, libtcodpy.BKGND_SET, libtcodpy.LEFT, 'print rect ex') > 0 ) assert (libtcodpy.console_get_height_rect( console, 0, 0, 8, 8, 'get height') > 0 ) libtcodpy.console_set_color_control(libtcodpy.COLCTRL_1, fg, bg)
def render(self) -> None: self.clear() self.console.default_fg = tcod.black self.console.default_bg = tcod.white tcod.console_set_color_control(tcod.COLCTRL_1, tcod.red, tcod.black) self.console.print_frame(1, 1, self.width - 2, self.height - 2, clear=False) self.console.print_(3, 3, GAME_OVER_TEXT)
def draw_credits(self): for key, color in self.SPLASH_SCREEN.credits_format_colors.items(): tcod.console_set_color_control(key, tuple(color), (0, 0, 0)) credits_height = self.SPLASH_SCREEN.credits.count('\n') + 1 self.console.print_box(0, self.SCREEN_HEIGHT - (credits_height + 1), self.SCREEN_WIDTH, credits_height, self.SPLASH_SCREEN.get_centered_credits( self.SCREEN_WIDTH), fg=tuple(self.SPLASH_SCREEN.credits_color), bg=(0, 0, 0)) return credits_height
def render(self) -> None: self.clear() self.console.default_fg = tcod.white self.console.default_bg = tcod.dark_grey tcod.console_set_color_control(tcod.COLCTRL_1, tcod.green, tcod.black) tcod.console_set_color_control(tcod.COLCTRL_2, tcod.yellow, tcod.black) self.console.print_frame( 1, 1, self.width - 2, self.height - 2, clear=False ) self.console.print_( 3, 3, INTRO_TEXT )
def esc_menu(con, menu_width, menu_height, screen_width, screen_height, turn_number): window = libtcod.console_new(menu_width, menu_height) libtcod.console_set_default_foreground(window, libtcod.white) libtcod.console_set_color_control(libtcod.COLCTRL_1, libtcod.light_yellow, libtcod.white) libtcod.console_print_rect_ex(window, 0, 1, menu_width, menu_height, libtcod.BKGND_NONE, libtcod.LEFT, 'Options') libtcod.console_print_rect_ex(window, 0, 3, menu_width, menu_height, libtcod.BKGND_NONE, libtcod.LEFT, 'a.) %c%s%celp' % (libtcod.COLCTRL_1, 'H', libtcod.COLCTRL_STOP)) libtcod.console_print_rect_ex(window, 0, 4, menu_width, menu_height, libtcod.BKGND_NONE, libtcod.LEFT, 'b.) %c%s%cesume' % (libtcod.COLCTRL_1, 'R', libtcod.COLCTRL_STOP)) libtcod.console_print_rect_ex(window, 0, 5, menu_width, menu_height, libtcod.BKGND_NONE, libtcod.LEFT, 'c.) Save & %c%s%cuit' % (libtcod.COLCTRL_1, 'Q', libtcod.COLCTRL_STOP)) libtcod.console_print_rect_ex(window, 0, 6, menu_width, menu_height, libtcod.BKGND_NONE, libtcod.LEFT, '\nTurns passed: {0}'.format(turn_number)) x = screen_width // 2 - menu_width // 2 y = screen_height // 2 - menu_height libtcod.console_blit(window, 0, 0, menu_width, int(menu_height/2 + 6), con, x, y, 1, 1)
def render(self) -> None: self.clear() self.console.default_fg = tcod.grey self.console.default_bg = tcod.black tcod.console_set_color_control(tcod.COLCTRL_1, tcod.red, tcod.black) tcod.console_set_color_control(tcod.COLCTRL_2, tcod.yellow, tcod.black) tcod.console_set_color_control(tcod.COLCTRL_3, tcod.green, tcod.black) tcod.console_set_color_control(tcod.COLCTRL_4, tcod.white, tcod.black) hp_percentage = (self.target.stats.hp / self.target.stats.max_hp) * 100 if hp_percentage >= 75.0: hp_color = tcod.COLCTRL_3 elif hp_percentage >= 25.0: hp_color = tcod.COLCTRL_2 else: hp_color = tcod.COLCTRL_1 torch_status = self.target.torch if torch_status: torch_color = tcod.COLCTRL_2 else: torch_color = tcod.COLCTRL_4 hud_lines = "\n".join([ "%c{}%c the %cBarbarian%c" % (tcod.COLCTRL_4, tcod.COLCTRL_STOP, tcod.COLCTRL_4, tcod.COLCTRL_STOP), "", "HP: %c{}%c/%c{}%c" % (hp_color, tcod.COLCTRL_STOP, tcod.COLCTRL_4, tcod.COLCTRL_STOP), "CAPS: %c{:.2f}%c/%c{:.2f}%c" % (tcod.COLCTRL_4, tcod.COLCTRL_STOP, tcod.COLCTRL_4, tcod.COLCTRL_STOP), "TORCH: %c{}%c" % (torch_color, tcod.COLCTRL_STOP) ]) self.console.print_( 0, 0, hud_lines.format(self.target.symbol, self.target.stats.hp, self.target.stats.max_hp, self.target.stats.cap, self.target.stats.max_cap, "ON" if self.target.torch else "OFF").ljust( self.width), tcod.BKGND_SET, tcod.LEFT)
def render_enemy_panel(game, con, panel_x, panel_y, width, height, color): setup_console(con, caption='Enemies', borders=True, bordercolor=color) # check for monsters in FOV spotted = [ ent for ent in game.npc_ents if ent.is_visible(game.fov_map) and ent.f.hp > 0 ] #spotted = [ent for ent in game.entities if ent.ai and ent.f.hp > 0 and ent.is_visible(game.fov_map)] if len(spotted): spotted.sort(key=game.player.distance_to_ent ) # sort the spotted array by distance to player # initial offsets from panel borders y = 2 for ent in spotted: # Go through the object names and wrap them according to the panel's width if y >= con.height - 2: # If the limit's of the con are reached, cut the con off tcod.console_set_default_foreground(con, colors.white) x = center_x_for_text(width, '~ ~ ~ MORE ~ ~ ~') print_line(con, x, y, '~ ~ ~ MORE ~ ~ ~') break char = '*' if ent.pos == game.cursor.pos and game.state in [ GameState.CURSOR_ACTIVE, GameState.CURSOR_TARGETING ] else f'{ent.char}' # Draw creature name and stats # tcod.console_set_default_foreground(con, colors.gray) tcod.console_set_color_control(tcod.COLCTRL_1, ent.color, tcod.black) tcod.console_put_char_ex(con, 1, y, char, ent.color, tcod.black) print_line(con, 3, y, ent.full_name, color=ent.color) y += 1 x = 1 tcod.console_put_char_ex(con, x, y, chr(192), tcod.gray, tcod.black) tcod.console_set_color_control(tcod.COLCTRL_2, ent.f.hp_color, tcod.black) tcod.console_set_color_control(tcod.COLCTRL_3, ent.f.stamina_color, tcod.black) status_line = f'%c{ent.f.hp_string.title()}%c|%c{ent.f.stamina_string.title()}%c'\ % (tcod.COLCTRL_2, tcod.COLCTRL_STOP, tcod.COLCTRL_3, tcod.COLCTRL_STOP) for status, active in ent.f.effects.items( ): # Todo does not consider number of status effects > width of panel if active: status_line += f' %white%{status.name[0]}%%' print_line(con, x + 1, y, f'{status_line}') y += 1 con.blit(game.root, panel_x, panel_y, 0, 0, width, height)
def get_constants(): window_title = 'Twilight of the Pixie Goddess' # Screen screen_width = 130 screen_height = 80 # Healthbar bar_width = 20 # Bottom Panel panel_height = 8 panel_y = screen_height - panel_height side_panel_width = 40 side_panel_x = screen_width + 2 # Message box message_x = bar_width + 2 message_width = screen_width - bar_width - 2 message_height = panel_height - 1 # Map map_width = screen_width - 35 map_height = screen_height - 8 room_max_size = 14 room_min_size = 6 max_rooms = 120 # Field of View fov_algorithm = 0 fov_light_walls = True fov_radius = 10 # Entities max_monsters_per_room = 3 max_items_per_room = 2 colors = { 'dark_wall': libtcodpy.Color(17, 65, 89), 'dark_ground': libtcodpy.Color(42, 138, 138), 'light_wall': libtcodpy.Color(32, 121, 0), 'light_ground': libtcodpy.Color(89, 219, 85) } constants = { 'window_title': window_title, 'screen_width': screen_width, 'screen_height': screen_height, 'bar_width': bar_width, 'side_panel_width': side_panel_width, 'side_panel_x': side_panel_x, 'panel_height': panel_height, 'panel_y': panel_y, 'message_x': message_x, 'message_width': message_width, 'message_height': message_height, 'map_width': map_width, 'map_height': map_height, 'room_max_size': room_max_size, 'room_min_size': room_min_size, 'max_rooms': max_rooms, 'fov_algorithm': fov_algorithm, 'fov_light_walls': fov_light_walls, 'fov_radius': fov_radius, 'max_monsters_per_room': max_monsters_per_room, 'max_items_per_room': max_items_per_room, 'colors': colors } libtcodpy.console_set_color_control(libtcodpy.COLCTRL_1, libtcodpy.Color(47, 121, 251), libtcodpy.black) # Sky blue libtcodpy.console_set_color_control(libtcodpy.COLCTRL_2, libtcodpy.Color(246, 89, 154), libtcodpy.black) # Pink libtcodpy.console_set_color_control(libtcodpy.COLCTRL_3, libtcodpy.Color(121, 121, 121), libtcodpy.black) # Grey libtcodpy.console_set_color_control(libtcodpy.COLCTRL_4, libtcodpy.Color(231, 0, 89), libtcodpy.black) # Redish Pink libtcodpy.console_set_color_control(libtcodpy.COLCTRL_5, libtcodpy.Color(89, 219, 85), libtcodpy.black) # Light green return constants
def render_all(con, panel, entities, player, game_map, fov_map, fov_recompute, message_log, screen_width, screen_height, bar_width, panel_height, panel_y, mouse, colors, game_state): if fov_recompute: # Draw all the tiles in the game map for y in range(game_map.height): for x in range(game_map.width): visible = libtcod.map_is_in_fov(fov_map, x, y) wall = game_map.tiles[x][y].block_sight if visible: if wall: libtcod.console_set_char_background( con, x, y, colors.get('light_wall'), libtcod.BKGND_SET) else: libtcod.console_set_char_background( con, x, y, colors.get('light_ground'), libtcod.BKGND_SET) game_map.tiles[x][y].explored = True elif game_map.tiles[x][y].explored: if wall: libtcod.console_set_char_background( con, x, y, colors.get('dark_wall'), libtcod.BKGND_SET) else: libtcod.console_set_char_background( con, x, y, colors.get('dark_ground'), libtcod.BKGND_SET) entities_in_render_order = sorted(entities, key=lambda x: x.render_order.value) # Draw all entities in the list for entity in entities_in_render_order: draw_entity(con, entity, fov_map, game_map) libtcod.console_blit(con, 0, 0, screen_width, screen_height, 0, 0, 0) libtcod.console_set_default_background(panel, libtcod.black) libtcod.console_clear(panel) # Print the game messages, one line at a time libtcod.console_set_color_control(libtcod.COLCTRL_2, libtcod.light_amber, libtcod.black) #NPC message highlighting libtcod.console_set_color_control( libtcod.COLCTRL_1, libtcod.light_gray, libtcod.black) #Player message highlighting libtcod.console_set_color_control( libtcod.COLCTRL_3, libtcod.dark_red, libtcod.black) #NPC Hit message highlighting libtcod.console_set_color_control( libtcod.COLCTRL_4, libtcod.darker_red, libtcod.black) # NPC Hit message highlighting y = 1 for message in message_log.messages: libtcod.console_set_default_foreground(panel, message.color) libtcod.console_print_ex(panel, message_log.x, y, libtcod.BKGND_NONE, libtcod.LEFT, message.text) y += 1 render_bar(panel, 1, 1, bar_width, 'HP', round(player.fighter.hp), player.fighter.max_hp, libtcod.light_red, libtcod.darker_red) libtcod.console_print_ex( panel, 1, 3, libtcod.BKGND_NONE, libtcod.LEFT, 'Dungeon level: {0}'.format(game_map.dungeon_level)) libtcod.console_set_default_foreground(panel, libtcod.light_gray) libtcod.console_print_ex(panel, 1, 0, libtcod.BKGND_NONE, libtcod.LEFT, get_names_under_mouse(mouse, entities, fov_map)) libtcod.console_blit(panel, 0, 0, screen_width, panel_height, 0, 0, panel_y) if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY, GameStates.SHOW_WEAPON_INVENTORY, GameStates.SHOW_SCROLL_INVENTORY): if game_state == GameStates.SHOW_INVENTORY: inventory_title = 'Press the key next to an item to use it, or Esc to cancel.\n' inventory_menu(con, inventory_title, player, 50, screen_width, screen_height) elif game_state == GameStates.SHOW_WEAPON_INVENTORY: inventory_title = 'Press the key next to an item to equip/unequip it, or Esc to cancel.\n' weapon_inventory_menu(con, inventory_title, player, 50, screen_width, screen_height) elif game_state == GameStates.SHOW_SCROLL_INVENTORY: inventory_title = 'Press the key next to an item to read it, or Esc to cancel.\n' scroll_inventory_menu(con, inventory_title, player, 50, screen_width, screen_height) else: inventory_title = 'Press the key next to an item to drop it, or Esc to cancel.\n' inventory_menu(con, inventory_title, player, 50, screen_width, screen_height) elif game_state == GameStates.LEVEL_UP: level_up_menu(con, 'Level up! Choose a stat to raise:', player, 40, screen_width, screen_height) elif game_state == GameStates.CHARACTER_SCREEN: character_screen(player, 30, 10, screen_width, screen_height)
def print_line(con, x, y, string, max_length=None, color=None, fgcolor=colors.white, bgcolor=colors.black, color_coefficient=None, alignment=tcod.LEFT, background=tcod.BKGND_DEFAULT): """ Prints a string to tcods console, supporting custom color-code wrappers. """ color_coded_words = COLOR_WRAP_PATTERN.findall(string) col_ctrls = () if color_coded_words: for i, word in enumerate(color_coded_words): color_code = color_code_pattern.match(word) if color_code.group( 1 )[0: 5] == 'Color': # if the string is wrapped as %Color(int,int,int)%String%% color_str = eval(color_code.group(1)) else: # if the string is wrapped as %color_name%String%% color_str = eval( f'colors.{color_code.group(1)}' ) # Resolve the color-code as a config.colors.py entry: %red%->Color(colors.red) if color_coefficient: color_str = tuple( int(color_coefficient * x) for x in color_str) new_word = word.replace( color_code.group(), '%c' ) # Replace the custom color-code with tcod's color-wrappers: %red%word% -> %cword%c new_word = new_word.replace('%%', '%c') string = string.replace(word, new_word) # Update the original string col_ctrl = eval(f'tcod.COLCTRL_{i+1}') tcod.console_set_color_control(col_ctrl, color_str, bgcolor) col_ctrls += (col_ctrl, tcod.COLCTRL_STOP) if color: if not '%c' in string: # If no tcod wrappers are present, wrap the entire string string = f'%c{string}%c' if color_coefficient: color = tuple(int(color_coefficient * x) for x in color) tcod.console_set_color_control(tcod.COLCTRL_1, color, bgcolor) col_ctrls = (tcod.COLCTRL_1, tcod.COLCTRL_STOP) string = string % col_ctrls if fgcolor and color_coefficient: fgcolor = tuple(int(color_coefficient * x) for x in fgcolor) if max_length is not None and len(string) > max_length: string = dynamic_wrap(string, max_length) con.print(x, y, string, fg=fgcolor, alignment=alignment, bg_blend=background)
def role_menu(con, screen_width, screen_height, role): names = ['Joe', 'Kyle', 'Brett', 'Devon', 'Kelsey'] roles = [ 'Rageoholic', 'Beastmaster', 'Sense Bender', 'Bald Bro', 'Eldritch Blast' ] window = libtcod.console.Console(screen_width, screen_height, 'F') text_box = libtcod.console.Console(screen_width, screen_height, 'F') window_width = 80 window_height = 55 text_box_width = 4 text_box_height = 0 window.draw_frame(0, 0, window_width, window_height, 'Choose your Class') ##TODO: can shrink this to one for loop and use color control codes to write a single string with multiple colors. See how I do the stat blocks below. #Print Role name y = window_height - 5 letter_index = ord('a') for name in names: text = '(' + chr(letter_index) + ') ' text_box.print_box(0, y, window_width, 1, text, None, None, libtcod.BKGND_NONE, libtcod.LEFT) y += 1 letter_index += 1 #Print Role description y = window_height - 5 role_index = 0 for name in names: text = name + ': ' text_width = len(text) text_box.print_box(4, y, window_width, 1, text, libtcod.yellow, None, libtcod.BKGND_NONE, libtcod.LEFT) text_box.print_box(4 + text_width, y, window_width, 1, roles[role_index], libtcod.azure, None, libtcod.BKGND_NONE, libtcod.LEFT) if (len(text) + len(roles[role_index])) > text_box_width: text_box_width = len(text) + len(roles[role_index]) + 4 role_index += 1 y += 1 letter_index += 1 text_box_height += 1 #Print Select Message text = 'Press a letter to view a class. Press \'Enter\' to select it.' window.print(int(window_width / 2 - len(text) / 2), window_height - 3, text, libtcod.white, None, libtcod.BKGND_NONE, libtcod.LEFT) #Print role image (images should be 75 x 75 pixels) role.portrait.blit_2x(window, 2, 4, 0, 0) #Print role name and description libtcod.console_set_color_control(libtcod.COLCTRL_1, libtcod.yellow, libtcod.black) libtcod.console_set_color_control(libtcod.COLCTRL_2, libtcod.azure, libtcod.black) libtcod.console_set_color_control(libtcod.COLCTRL_3, libtcod.white, libtcod.black) libtcod.console_set_color_control(libtcod.COLCTRL_4, libtcod.green, libtcod.black) text = '%c..-------~ %c{0} %c~-------..'.format(role.owner.name) % ( libtcod.COLCTRL_3, libtcod.COLCTRL_1, libtcod.COLCTRL_3) window.print_box(42, 5, 37, 1, text, libtcod.white, None, libtcod.BKGND_NONE, libtcod.CENTER) text = '{0}'.format(role.name) window.print_box(42, 7, 37, 1, text, libtcod.azure, None, libtcod.BKGND_NONE, libtcod.CENTER) #Print role description text = '{0}'.format(role.description) description_height = window.get_height_rect(42, 9, 37, window_height, text) window.print_box(42, 9, 37, description_height, text, libtcod.white, None, libtcod.BKGND_NONE, libtcod.LEFT) #Print role stats text = '%c.--~ Con: %c{0}%c Str: %c{1}%c Def: %c{2}%c ~--.'.format( role.con, role.base_power, role.base_defense) % ( libtcod.COLCTRL_3, libtcod.COLCTRL_4, libtcod.COLCTRL_3, libtcod.COLCTRL_4, libtcod.COLCTRL_3, libtcod.COLCTRL_4, libtcod.COLCTRL_3) window.print_box(42, description_height + 11, 37, 1, text, libtcod.white, None, libtcod.BKGND_NONE, libtcod.CENTER) dmg = 'd'.join(map(str, role.dmg)) hitdie = 'd'.join(map(str, role.hitdie)) text = '%c.--~ Dmg: %c{0}%c HitDie: %c{1}%c ~--.'.format(dmg, hitdie) % ( libtcod.COLCTRL_3, libtcod.COLCTRL_4, libtcod.COLCTRL_3, libtcod.COLCTRL_4, libtcod.COLCTRL_3) window.print_box(42, description_height + 13, 37, 1, text, libtcod.white, None, libtcod.BKGND_NONE, libtcod.CENTER) #Print role abilities window_x = int(screen_width / 2 - window_width / 2) window_y = int(screen_height / 2 - window_height / 2) text_box_x = int(screen_width / 2 - text_box_width / 2) text_box_y = int(screen_height / 2 - text_box_height / 2) libtcod.console_blit(window, 0, 0, screen_width, screen_height, 0, window_x, window_y, 1.0, 1.0) libtcod.console_blit(text_box, 0, 0, screen_width, screen_height, 0, text_box_x, 0, 1.0, 0)