def dotext(con, x, y, text, fg=ltc.white, bg=None, flag=None, align=ltc.LEFT): if not flag: flag = ltc.BKGND_SET if bg else ltc.BKGND_NONE if not bg: bg = ltc.black # if there are any colour coded in the text, replace them with the libtcod colour chars if '$' in text: # replace common colour codes text = text.replace(colctr_stop, chr(ltc.COLCTRL_STOP)) text = text.replace(colctr_fg, chr(ltc.COLCTRL_FORE_RGB)) text = text.replace(colctr_bg, chr(ltc.COLCTRL_BACK_RGB)) # note that libtcod can only handle 5 colour codes in the same print operation i = 1 for code, colour in __colours__.iteritems(): if code in text: if i > 5: # too many colour codes, just remove the control sequences text = text.replace(code, '') else: # replace the control sequence with the libtcod code # and assign the correct colour to the libtcod code ltc_code = __ltc_colour_codes__[i] text = text.replace(code, chr(ltc_code)) ltc.console_set_color_control(ltc_code, colour, bg) ltc.console_set_default_foreground(con, fg) ltc.console_set_default_background(con, bg) ltc.console_print_ex(con, x, y, flag, align, text)
def draw_gui(gui_panel, con, leveldata, mouse): ltc.console_set_default_background(gui_panel, ltc.black) guiutils.draw_bar(gui_panel, 2, 2, GUI_BAR_WIDTH, 'HP', leveldata.player.fighter.hp, leveldata.player.fighter.max_hp, ltc.light_red, ltc.dark_red) guiutils.draw_panel_border(gui_panel, PANEL_WIDTH, PANEL_HEIGHT) src.rlmsglog.msg_log.draw_log(gui_panel, MSG_LOG_X, MSG_LOG_Y, MSG_LOG_W, MSG_LOG_H) objs_under_mouse = guiutils.get_names_at_loc(leveldata, mouse.cx, mouse.cy) guiutils.dotext(con, 0, 0, objs_under_mouse)
def draw_bar(con, x, y, total_width, name, value, maximum, bar_color, back_color): # render a bar (HP, experience, etc). First calculate the width of the bar bar_width = int(float(value) / maximum * total_width) # render the background first ltc.console_set_default_background(con, back_color) ltc.console_rect(con, x, y, total_width, 1, False, ltc.BKGND_SCREEN) # now render the bar on top ltc.console_set_default_background(con, bar_color) if bar_width > 0: ltc.console_rect(con, x, y, bar_width, 1, False, ltc.BKGND_SCREEN) # now, centered text with values text = name + ':' + str(value) + '/' + str(maximum) dotext(con, x + total_width / 2, y, text, align=ltc.CENTER)
def show_player_stats(): player_stats=libtcod.console_new(20,20) libtcod.console_set_default_background(player_stats,libtcod.darkest_grey) libtcod.console_set_default_foreground(player_stats, libtcod.white) # libtcod.console_clear(player_stats) libtcod.console_print_frame(player_stats, 0, 0, libtcod.console_get_width(player_stats), libtcod.console_get_height(player_stats), clear=True) height=0 libtcod.console_print_rect(player_stats, 1, 1, libtcod.console_get_width(player_stats)-2, libtcod.console_get_height(player_stats)-2, "Name: %s \nHealth: %s/%s\nView distance: %s\nStrength: %s\nTo hit: %s\nExp: %s"# %(P.player.name,P.player.health,P.player.max_health, P.player.view_distance, P.player.strength,P.player.to_hit,P.player.exp)) libtcod.console_print_ex(player_stats, libtcod.console_get_width(player_stats)//2, 0, libtcod.BKGND_DEFAULT, libtcod.CENTER, "Player Stats") libtcod.console_print_ex(player_stats, libtcod.console_get_width(player_stats)//2, libtcod.console_get_height(player_stats)-1, libtcod.BKGND_DEFAULT, libtcod.CENTER, "[spacebar]") libtcod.console_blit(player_stats,0,0, libtcod.console_get_width(player_stats), libtcod.console_get_height(player_stats), 0,5,5, 1.0,0.1) key = libtcod.console_check_for_keypress(libtcod.KEY_PRESSED) while not (libtcod.KEY_SPACE==key.vk): key = libtcod.console_check_for_keypress(libtcod.KEY_PRESSED) libtcod.console_blit(player_stats,0,0, libtcod.console_get_width(player_stats), libtcod.console_get_height(player_stats), 0,5,5, 1.0,0.1) libtcod.console_flush() R.render()
def test_area(self, area, fcolor, bcolor): """ Draw routines to test game areas. Draws some test things in a specific area. Arguments: area : area to be tested fcolor : foreground color to use for test bcolor : background color to use for test """ libtcod.console_set_default_background(area['con'], bcolor) libtcod.console_set_default_foreground(area['con'], fcolor) libtcod.console_clear(area['con']) libtcod.console_print_frame(area['con'], 0, 0, area['w'], area['h'], False, None, area['name'] + ": " + str(area)) for i in range(area['h']): libtcod.console_print_ex(area['con'], 0, i, libtcod.BKGND_NONE, libtcod.LEFT, str(i + 1)) libtcod.console_blit(area['con'], 0, 0, area['w'], area['h'], 0, area['x'], area['y'])
def draw_items(): height = 0 libtcod.console_set_default_background(cons.items_console, libtcod.red) libtcod.console_set_default_foreground(cons.items_console, libtcod.white) libtcod.console_clear(cons.items_console) for item in tile_items: height+=libtcod.console_print_rect_ex(cons.items_console, 0, height, libtcod.console_get_width(cons.items_console), libtcod.console_get_height(cons.items_console), libtcod.BKGND_NONE, libtcod.LEFT, item.name) libtcod.console_blit(cons.items_console,0,0, libtcod.console_get_width(cons.items_console), height, 0,1,1, 1.0,0.5) libtcod.console_flush()
def item_selector(items, default=None, equipped=[], title="INVENTORY"): libtcod.console_clear(cons.menu_console) libtcod.console_set_default_background(cons.menu_console, libtcod.black) libtcod.console_set_default_foreground(cons.menu_console, libtcod.white) libtcod.console_rect(cons.menu_console, 0, 0, M.MAP_WIDTH, M.MAP_HEIGHT, True) libtcod.console_print_ex(cons.menu_console, 40, 0, libtcod.BKGND_NONE, libtcod.CENTER, title) libtcod.console_print_ex(cons.menu_console, 1, M.SCREEN_HEIGHT-1, libtcod.LEFT, libtcod.BKGND_NONE, "[j / k]: Highlight item [SPACEBAR]: Select [q]: quit") count = 0 for item in items: libtcod.console_print_ex(cons.menu_console, 1, count+3, libtcod.BKGND_NONE, libtcod.LEFT, item.name) if item in equipped: libtcod.console_print_ex(cons.menu_console, libtcod.console_get_width(cons.menu_console)-1, count+3, libtcod.BKGND_NONE, libtcod.RIGHT, "(EQUIPPED)") count = count + 1 if default: count = items.index(default) else: count = count -1 key = libtcod.console_check_for_keypress(True) while not key.vk == libtcod.KEY_SPACE and not ord('q') == key.c: for i in range(len(items[count].name)): libtcod.console_set_char_background(cons.menu_console, i+1, count+3, libtcod.white) libtcod.console_set_char_foreground(cons.menu_console, i+1, count+3, libtcod.black) if key.pressed and key.c == ord('k') and count > 0: for i in range(len(items[count].name)): libtcod.console_set_char_background(cons.menu_console, i+1, count+3, libtcod.black) libtcod.console_set_char_foreground(cons.menu_console, i+1, count+3, libtcod.white) count = count -1 elif key.pressed and key.c == ord('j') and count < len(items)-1: for i in range(len(items[count].name)): libtcod.console_set_char_background(cons.menu_console, i+1, count+3, libtcod.black) libtcod.console_set_char_foreground(cons.menu_console, i+1, count+3, libtcod.white) count = count +1 key = libtcod.console_check_for_keypress(True) libtcod.console_blit(cons.menu_console,0,0,M.SCREEN_WIDTH,M.SCREEN_HEIGHT,0,0,0,1) libtcod.console_flush() if ord('q') == key.c: count=-1 return count
def clear_console(con): ltc.console_set_default_background(con, ltc.BKGND_NONE) ltc.console_clear(con)
def __init__(self, libtcod_window): self.default_fg = self.color_map[Color.Normal] self.default_bg = self.color_map[Color.Black] self.win = libtcod_window libtcod.console_set_default_foreground(self.win, self.default_fg) libtcod.console_set_default_background(self.win, self.default_bg)