def createBar(con, x, y, width, current, max, back_color, front_color, text): # Creates a bar from given values and displays it at given coords. # Calculate bar width. bar_width = int(float(current) / max * width) # Create background bar first. libtcod.console_set_default_background(con, back_color) libtcod.console_rect(con, x, y, width, 1, False, libtcod.BKGND_SET) # Otherwise the bar will go backwards. if bar_width > 0: libtcod.console_set_default_background(con, front_color) libtcod.console_rect(con, x, y, bar_width, 1, False, libtcod.BKGND_SET) if current != max: width += 1 libtcod.console_set_default_foreground(con, libtcod.white) libtcod.console_print_ex(con, int(x + width / 2), y, libtcod.BKGND_NONE, libtcod.CENTER, text + str(current) + '/' + str(max)) libtcod.console_set_default_background(con, libtcod.black)
def render_bar(self, x, y, total_width, name, value, maximum, bar_color, back_color, text_color=libtcod.white, show_values=False, title_inset=True): #render a bar (HP, experience, etc). first calculate the width of the bar bar_width = int(round(value / maximum * total_width)) # Make sure the value doesn't have a million repeating decimals value = round(value, 1) #render the background first libtcod.console_set_default_background(self.con, back_color) libtcod.console_rect(self.con, x, y, total_width, 1, False, libtcod.BKGND_SCREEN) #now render the bar on top libtcod.console_set_default_background(self.con, bar_color) if bar_width > 0: libtcod.console_rect(self.con, x, y, bar_width, 1, False, libtcod.BKGND_SCREEN) # This will cause the title to appear above the bar if not title_inset: y = y - 1 #finally, some centered text with the values libtcod.console_set_default_foreground(self.con, text_color) if show_values: libtcod.console_print_ex(self.con, (x + int(round(total_width / 2))), y, libtcod.BKGND_NONE, libtcod.CENTER, name + ': ' + str(value) + '/' + str(maximum)) else: libtcod.console_print_ex(self.con, (x + int(round(total_width / 2))), y, libtcod.BKGND_NONE, libtcod.CENTER, name) libtcod.console_set_default_background(self.con, self.backcolor) libtcod.console_set_default_background(self.con, self.frontcolor)
def render_bar(x, y, total_width, name, value, maximum, bar_color, back_color): """ Generic status bar renderer. Can be used for health bar, mana bar, experience bar, dungeon level, etc. """ global panel # determine the width of the bar to render. bar_width = int(float(value) / maximum * total_width) # render the background bar. libtcod.console_set_default_background(panel, back_color) libtcod.console_rect(panel, x, y, total_width, 1, False, libtcod.BKGND_SCREEN) # render the foreground bar. libtcod.console_set_default_background(panel, bar_color) if bar_width > 0: libtcod.console_rect(panel, x, y, bar_width, 1, False, libtcod.BKGND_SCREEN) # render some centered text with the values msg = '{}: {}/{}'.format(name, value, maximum) libtcod.console_set_default_foreground(panel, libtcod.white) libtcod.console_print_ex(panel, x + total_width / 2, y, libtcod.BKGND_NONE, libtcod.CENTER, msg)
def draw(self): """Draws borders around the info panel.""" libt.console_set_default_background(self.handler.gui, data.COLOURS['gui_bg']) libt.console_clear(self.handler.gui) upper_height = config.BORDER_WIDTH / 2 left_height = config.GUI_HEIGHT - upper_height*2 libt.console_set_default_background(self.handler.gui, data.COLOURS['gui_border']) # Upper border libt.console_rect(self.handler.gui, 0, 0, config.GUI_WIDTH, upper_height, False, libt.BKGND_SCREEN) # Lower border libt.console_rect(self.handler.gui, 0, config.GUI_HEIGHT - config.BORDER_WIDTH/2, config.GUI_WIDTH, upper_height, False, libt.BKGND_SCREEN) # Left border libt.console_rect(self.handler.gui, 0, upper_height, config.BORDER_WIDTH / 2, left_height, False, libt.BKGND_SCREEN) # Right border libt.console_rect(self.handler.gui, config.GUI_WIDTH - config.BORDER_WIDTH/2, upper_height, config.BORDER_WIDTH / 2, left_height, False, libt.BKGND_SCREEN) # Middle border libt.console_rect(self.handler.gui, (config.GUI_WIDTH - 1)/2 - config.BORDER_WIDTH/2, upper_height, config.BORDER_WIDTH, left_height, False, libt.BKGND_SCREEN) # Hover details libt.console_set_default_foreground(self.handler.gui, data.COLOURS['text']) libt.console_print_ex(self.handler.gui, (config.GUI_WIDTH - 1)/2, 0, libt.BKGND_NONE, libt.CENTER, self.objects_under_mouse())
def renderBar(self, panel, x, y, total_width, name, value, maximum, bar_color, back_color): """ Helper function to render interface bars """ # 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 libtcod.console_set_default_background(panel, back_color) libtcod.console_rect(panel, x, y, total_width, 1, False, libtcod.BKGND_SCREEN) # now render the bar on top libtcod.console_set_default_background(panel, bar_color) if bar_width > 0: libtcod.console_rect(panel, x, y, bar_width, 1, False, libtcod.BKGND_SCREEN) # finally, some centered text with the values libtcod.console_set_default_foreground(panel, libtcod.white) libtcod.console_print_ex( panel, x + total_width / 2, y, libtcod.BKGND_NONE, libtcod.CENTER, name + ": " + str(value) + "/" + str(maximum), )
def add_bar(self, x, y, name, value, maximum, bar_color, back_color): """ Draw a bar in this panel's buffer. :param x: Location in X :param y: Location in Y :param name: The bar's name, displayed in the bar. :param value: The bar's current value. :param maximum: The bar's maximum value. :param bar_color: The color of the filled portion of the bar :param back_color: The color of the background of the bar. :return: Nothing. """ total_width = (self.width - (self.margin * 4)) #Compute the maximum length of the bar. current_width = int(float(value) / maximum * total_width) #Compute the length of the bar. if current_width > total_width: current_width = total_width #Render the bar's BG libtcod.console_set_default_background(self.console, back_color) libtcod.console_rect(self.console, x, y, total_width - 1, 1, False, libtcod.BKGND_SCREEN) #Render the bar's colored section libtcod.console_set_default_background(self.console, bar_color) if current_width > 0: libtcod.console_rect(self.console, x, y, current_width, 1, False, libtcod.BKGND_SCREEN) #Render text on the bar libtcod.console_set_default_foreground(self.console, color_ui_text) libtcod.console_print_ex(self.console, x + total_width / 2, y, libtcod.BKGND_NONE, libtcod.CENTER, name + ": " + str(value) + "/" + str(maximum))
def draw(self, main_map): #Settings libtcod.console_set_default_background(self.console, libtcod.black) libtcod.console_set_alignment(self.console, libtcod.CENTER) if main_map.selected_unit: start = (self.width - 4)/2 #Draw all units in the unit image for x in range(self.width - 4): for y in range(self.width - 4): libtcod.image_put_pixel(self.unit_image, x, y, main_map.map_list[main_map.selected_unit.x + x - start][main_map.selected_unit.y + y - start].color) for u in main_map.units: if u.x == (main_map.selected_unit.x + x - start) and u.y == (main_map.selected_unit.y + y - start): libtcod.console_set_char_foreground(self.console, x + 2, y + 4, u.color) libtcod.console_set_char(self.console, x + 2, y + 4, u.char) libtcod.console_print_frame(self.console, 0, 0, self.width, self.height, False, libtcod.BKGND_NONE, main_map.selected_unit.name) libtcod.console_rect(self.console, 0,0, 20, 1, False) libtcod.image_blit_rect(self.unit_image, self.console, 2, 4, self.width - 4, self.width - 4, libtcod.BKGND_SET) libtcod.console_set_alignment(self.console, libtcod.LEFT) #Unit stats statx = self.width + 1 libtcod.console_print(self.console, 2, statx, 'Speed') libtcod.console_print(self.console, 2, statx + 1, 'Attack') libtcod.console_print(self.console, 2, statx + 2, 'Armor') libtcod.console_set_alignment(self.console, libtcod.RIGHT) libtcod.console_print(self.console, self.width - 2, statx, str(main_map.selected_unit.speed)) libtcod.console_print(self.console, self.width - 2, statx + 1, str(main_map.selected_unit.attack)) libtcod.console_print(self.console, self.width - 2, statx + 2, str(main_map.selected_unit.armor)) libtcod.console_blit(self.console, 0, 0, self.width, self.height, 0, 1, 60 - self.height/2, 1, 0.75)
def character_description(typ, id): libtcod.console_set_default_foreground(0, libtcod.white) libtcod.console_set_default_background(0, libtcod.black) libtcod.console_rect(0, 1, 11, 52, 10, True, libtcod.BKGND_SET) if typ == 'race': libtcod.console_print_rect(0, 2, 12, 50, 10, game.RACE_DESC[id]) if typ == 'class': libtcod.console_print_rect(0, 2, 12, 50, 10, game.CLASS_DESC[id])
def render_bar(self, con, x, y, w, value, max_value, bar_bg_color, bar_fg_color, text_color): ratio = int(w*(float(value)/max_value)) libtcod.console_set_default_background(con, bar_fg_color) libtcod.console_rect(con, x, y, ratio, 1, False, libtcod.BKGND_SET) libtcod.console_set_default_background(con, bar_bg_color) libtcod.console_rect(con, x+ratio, y, w-ratio, 1, False, libtcod.BKGND_SET) libtcod.console_set_default_background(con, text_color) libtcod.console_print_rect(con, x+1, y, w, 1, "%03d / %03d" % (value, max_value))
def generate_world(source_map, dynamic_spawns='Sparse', wildlife_spawns='Sparse', simulate_ticks=1000, combat_test=False, save=True, thread=True): WORLD_INFO['inittime'] = time.time() WORLD_INFO['start_age'] = simulate_ticks WORLD_INFO['dynamic_spawns'] = dynamic_spawns WORLD_INFO['wildlife_spawns'] = wildlife_spawns WORLD_INFO['real_time_of_day'] = int(round(WORLD_INFO['length_of_day']*.10)) WORLD_INFO['seed'] = time.time() WORLD_INFO['combat_test'] = combat_test WORLD_INFO['title'] = 'Operation %s' % language.generate_scheme_title().title() random.seed(WORLD_INFO['seed']) if WORLD_INFO['dynamic_spawns'] == 'Sparse': WORLD_INFO['dynamic_spawn_interval'] = [350, (1000, 1200)] elif WORLD_INFO['dynamic_spawns'] == 'Medium': WORLD_INFO['dynamic_spawn_interval'] = [350, (800, 999)] elif WORLD_INFO['dynamic_spawns'] == 'Heavy': WORLD_INFO['dynamic_spawn_interval'] = [350, (600, 799)] else: WORLD_INFO['dynamic_spawn_interval'] = [-1, (600, 799)] if WORLD_INFO['wildlife_spawns'] == 'Sparse': WORLD_INFO['wildlife_spawn_interval'] = [2500, (770, 990)] elif WORLD_INFO['wildlife_spawns'] == 'Medium': WORLD_INFO['wildlife_spawn_interval'] = [2500, (550, 700)] elif WORLD_INFO['wildlife_spawns'] == 'Heavy': WORLD_INFO['wildlife_spawn_interval'] = [2500, (250, 445)] else: WORLD_INFO['wildlife_spawn_interval'] = [-1, (250, 445)] weather.change_weather() create_region_spawns() randomize_item_spawns() lfe.focus_on(create_player()) alife.camps.create_all_camps() if thread: tcod.console_rect(0,0,0,WINDOW_SIZE[0],WINDOW_SIZE[1],True,flag=tcod.BKGND_DEFAULT) _r = Runner(simulate_ticks) _r.start() while _r.running: draw_world_stats() if not SETTINGS['running']: return False else: simulate_life(simulate_ticks) WORLD_INFO['id'] = 0 if save: WORLD_INFO['id'] = profiles.create_world() save_world(create=True) logging.info('World generation complete (took %.2fs)' % (time.time()-WORLD_INFO['inittime']))
def draw_messages(player, start_pos = 0): X, Y, W, H = 30, 40, 50, 10 libtcod.console_set_background_color(0, libtcod.black) libtcod.console_rect(0, X, Y, W, H, True, libtcod.BKGND_SET) libtcod.console_set_foreground_color(0, libtcod.white) if start_pos > 0: offset = 1 libtcod.console_print_left(0, X + 1, Y + 0, libtcod.BKGND_NONE, '? Too many messages; [any] to see more') else: offset = 0 try: for m, message in enumerate(player.message_log[start_pos:]): color = message.has_seen and libtcod.grey or libtcod.white libtcod.console_set_foreground_color(0, color) wrapped = wrap_text(message.text, W - 3, subsequent_indent = ' ') for line, text in enumerate(wrapped): if line == 0: if message.symbol1: libtcod.console_put_char_ex( 0, X + ((message.symbol2 is None) and 1 or 0), Y + offset, message.symbol1[0], message.symbol1[1] * (message.has_seen and 0.5 or 1), message.symbol1[2] * (message.has_seen and 0.5 or 1)) if message.symbol2: libtcod.console_put_char_ex( 0, X + 1, Y + offset, message.symbol2[0], message.symbol2[1] * (message.has_seen and 0.5 or 1), message.symbol2[2] * (message.has_seen and 0.5 or 1)) parsed_text, parse_data = parse_colors(text) libtcod.console_print_left(0, X + 3, Y + offset, libtcod.BKGND_NONE, parsed_text%parse_data) offset += 1 if offset >= H: if (not message.has_seen and line + 1 < len(wrapped)) or (m + 1 < len(player.message_log) and not player.message_log[start_pos+m+1].has_seen): raise TooManyMessages() else: raise LogIsFull() except LogIsFull: pass except TooManyMessages: draw_messages(player, start_pos + 1) return for message in player.message_log[start_pos:]: message.has_seen = True if start_pos > 0: libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) draw_messages(player)
def render(self, entity): """Renders an entity's inventory in the given window. Arguments: entity - the entity whose inventory you want to render """ # Background libtcod.console_set_default_background(0, libtcod.black) libtcod.console_set_default_foreground(0, libtcod.black) libtcod.console_rect(0, self.x_offset, self.y_offset, self.width, self.height, True, libtcod.BKGND_SET) libtcod.console_set_default_foreground(0, libtcod.white) libtcod.console_print(0, self.x_offset + 1, self.y_offset + 1, entity.name) if entity.inventory is None: libtcod.console_print(0, self.x_offset + 1, self.y_offset + 3, "This entity has no inventory. %s" % DEBUG_MSG) else: char_list = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" # Draw actions text - highlight the selected one libtcod.console_set_color_control(libtcod.COLCTRL_1, libtcod.blue, libtcod.black) libtcod.console_set_color_control(libtcod.COLCTRL_2, libtcod.blue, libtcod.white) libtcod.console_set_color_control(libtcod.COLCTRL_3, libtcod.black, libtcod.white) action_text = "%cU%cse%c %cD%crop%c %cT%chrow%c %cC%consume%c %cE%cquip%c %cW%cear%c" action_text_format = [libtcod.COLCTRL_1, libtcod.COLCTRL_STOP, libtcod.COLCTRL_STOP] * 6 if self.action_filter is not None: index = ["use", "drop", "throw", "consume", "equip", "wear"].index(self.action_filter) action_text_format[index * 3 + 0] = libtcod.COLCTRL_2 action_text_format[index * 3 + 1] = libtcod.COLCTRL_3 action_text = action_text % tuple(action_text_format) libtcod.console_print(0, self.x_offset + 1, self.y_offset + 3, action_text) # Set up screen offsets for rendering lcol = self.x_offset + 1 rcol = self.x_offset + 25 lrow = rrow = self.y_offset + 5 # In the case of no items, render_types will return 0, but still render a line (none) - so we need to # take this line into account when adjusting offsets. char_index = 0 num_weapons = self.render_types(entity.inventory, lcol, lrow, EntityType.WEAPON, "WEAPONS", char_list[char_index:]) lrow += max(1, num_weapons) + 3 char_index += num_weapons num_clothing = self.render_types(entity.inventory, rcol, rrow, EntityType.CLOTHING, "CLOTHING", char_list[char_index:]) rrow += max(1, num_clothing) + 3 char_index += num_clothing num_comestibles = self.render_types(entity.inventory, lcol, lrow, EntityType.COMESTIBLE, "COMESTIBLES", char_list[char_index:]) lrow += max(1, num_comestibles) + 3 char_index += num_comestibles num_medecine = self.render_types(entity.inventory, rcol, rrow, EntityType.MEDECINE, "MEDECINE", char_list[char_index:]) rrow += max(1, num_medecine) + 3 char_index += num_medecine num_other = self.render_types(entity.inventory, lcol, lrow, EntityType.OTHER, "OTHER", char_list[char_index:])
def start_menu(): title.init_blood() libtcod.console_credits_reset() finished_libtcod_credits = False selection = 0 while True: title.update() libtcod.console_clear(0) title.draw_blood() title.draw_title() libtcod.console_set_background_color(0, libtcod.dark_grey) libtcod.console_rect(0, 24, 14, 32, 7, False, libtcod.BKGND_MULTIPLY) libtcod.console_set_foreground_color(0, libtcod.red) libtcod.console_print_center(0, 40, 15, libtcod.BKGND_NONE, "Brutal RL: Slaves to Slaughter") libtcod.console_set_foreground_color(0, libtcod.white) libtcod.console_print_left(0, 35, 17, libtcod.BKGND_NONE, "New Game") libtcod.console_print_left(0, 35, 18, libtcod.BKGND_NONE, "Load Game") libtcod.console_print_left(0, 35, 19, libtcod.BKGND_NONE, "Quit") libtcod.console_print_left(0, 33, 17 + selection, libtcod.BKGND_NONE, ">") libtcod.console_print_left(0, 45, 17 + selection, libtcod.BKGND_NONE, "<") if not finished_libtcod_credits: finished_libtcod_credits = libtcod.console_credits_render(65, 43, True) libtcod.console_flush() key = libtcod.console_check_for_keypress(libtcod.KEY_PRESSED) if key.vk == libtcod.KEY_ESCAPE: if selection == 2: raise SetState("quit") else: selection = 2 elif key.vk in DIRECTION_KEYS and DIRECTION_KEYS[key.vk][0] == 0: selection += DIRECTION_KEYS[key.vk][1] selection %= 3 elif key.vk in [libtcod.KEY_ENTER, libtcod.KEY_KPENTER]: raise SetState([new_game, load_game, "quit"][selection]) mouse = libtcod.mouse_get_status() if mouse.lbutton: title.set_full(mouse.cx, mouse.cy) elif mouse.rbutton: title.set_empty(mouse.cx, mouse.cy)
def render_bar(x, y, total_width, name, value, maximum, bar_color, back_color): bar_width = int(float(value) / maximum * total_width) libtcod.console_set_default_background(panel, back_color) libtcod.console_rect(panel, x, y, total_width, 1, False, libtcod.BKGND_SCREEN) libtcod.console_set_default_background(panel, bar_color) if bar_width > 0: libtcod.console_rect(panel, x, y, bar_width, 1, False, libtcod.BKGND_SCREEN) libtcod.console_set_default_foreground(panel, libtcod.white) libtcod.console_print_ex(panel, x + total_width / 2, y, libtcod.BKGND_NONE, libtcod.CENTER, name + ': ' + str(value) + '/' + str(maximum))
def apply_screen_impact(): """ | Render the whole screen in the color set in gvar.screen_impact | and fade out. """ if gvar.screen_impact is not None: color = libtcod.color_lerp(libtcod.black, gvar.screen_impact[0], float(gvar.screen_impact[1])) libtcod.console_set_default_background(gvar.con, color) libtcod.console_rect(gvar.con, 1, 1, gvar.SCREEN_WIDTH, gvar.SCREEN_HEIGHT, False, libtcod.BKGND_SET) gvar.screen_impact[1] -= 0.25 if gvar.screen_impact[1] < 0: gvar.screen_impact = None
def render_bar(x, y, total_width, name, value, maximum, bar_color, back_color): """ | Renders a bar (HP, experience, etc) | Not currently used, but could be used in the future again """ bar_width = int(float(value) / maximum * total_width) libtcod.console_set_default_background(gvar.panel, back_color) libtcod.console_rect(gvar.panel, x, y, total_width, 1, False, libtcod.BKGND_SCREEN) libtcod.console_set_default_background(gvar.panel, bar_color) if bar_width > 0: libtcod.console_rect(gvar.panel, x, y, bar_width, 1, False, libtcod.BKGND_SCREEN) libtcod.console_set_default_foreground(gvar.panel, libtcod.white) libtcod.console_print_ex(gvar.panel, x + total_width / 2, y, libtcod.BKGND_NONE, libtcod.CENTER, name + ': ' + str(value) + '/' + str(maximum))
def render_bar(x, y, total_width, name, value, maximum, bar_color, back_color): # Render a bar (e.g., HP, experience, etc). First; calculate the width of the bar: bar_width = int(float(value) / maximum * total_width) # Render the background color. libtcod.console_set_default_background(panel, back_color) libtcod.console_rect(panel, x, y, total_width, 1, False, libtcod.BKGND_SCREEN) # Now render the bar on top. libtcod.console_set_default_background(panel, bar_color) if bar_width > 0: libtcod.console_rect(panel, x, y, bar_width, 1, False, libtcod.BKGND_SCREEN) # Then, centered text with current and max values. libtcod.console_set_default_foreground(panel, libtcod.white) libtcod.console_print_ex(panel, x + total_width // 2, y, libtcod.BKGND_NONE, libtcod.CENTER, name + ': ' + str(value) + '/' + str(maximum))
def render(self): bar_width = int(float(self.value) / self.maximum * self.width) libtcod.console_set_default_background(self.panel, self.back_color) libtcod.console_rect(self.panel, self.x + bar_width, self.y , self.width - bar_width, 1, False, libtcod.BKGND_SCREEN) libtcod.console_set_default_background(self.panel, self.bar_color) if bar_width > 0: libtcod.console_rect(self.panel, self.x, self.y, bar_width, 1, False, libtcod.BKGND_SCREEN) libtcod.console_set_default_foreground(self.panel, libtcod.white) libtcod.console_print_ex(self.panel, self.x + self.width / 2, self.y, libtcod.BKGND_NONE, libtcod.CENTER, self.name + ': ' + str(self.value) + '/' + str(self.maximum))
def menu(header, options, width, highlighted=[]): """Basic, general-purpose menu. Allows the user to choose from up to 26 text options.""" 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 if header == "": header_height = 0 else: header_height = tcod.console_get_height_rect(0, 0, 0, width, SCREEN_HEIGHT, header) height = len(options) + header_height # create an off-screen console that represents the menu's window window = tcod.console_new(width, height) # print the header, with auto-wrap tcod.console_set_default_foreground(window, tcod.white) tcod.console_print_rect(window, 0, 0, width, height, header) # print all the options y = header_height letter_index = ord("a") for option_text in options: text = "(" + chr(letter_index) + ") " + option_text tcod.console_print(window, 0, y, text) y += 1 letter_index += 1 for index in highlighted: w = len(options[index]) + 4 tcod.console_set_default_background(window, tcod.grey) y = index + header_height tcod.console_rect(window, 0, y, w, 1, False, flag=tcod.BKGND_SET) # blit the contents of "window" to the root console x = SCREEN_WIDTH // 2 - width // 2 y = SCREEN_HEIGHT // 2 - height // 2 tcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7) # present the root console to the player and wait for a key-press tcod.console_flush() tcod.sys_wait_for_event(tcod.EVENT_KEY_PRESS, key, mouse, True) # special case: changing to/from fullscreen if key.vk == tcod.KEY_F11: tcod.console_set_fullscreen(not tcod.console_is_fullscreen()) elif key.vk == tcod.KEY_ESCAPE: return "escape" else: # 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
def render_bar(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 libtcod.console_set_default_background(panel, back_color) libtcod.console_rect(panel, x,y, total_width, 1, False, libtcod.BKGND_SCREEN) # Now render the bar on top libtcod.console_set_default_background(panel, bar_color) if bar_width > 0: libtcod.console_rect(panel, x, y, bar_width, 1, False, libtcod.BKGND_SCREEN) #Finally some centered text with the values libtcod.console_set_default_foreground(panel, libtcod.white) libtcod.console_print_ex(panel, x + total_width / 2, y, libtcod.BKGND_NONE, libtcod.CENTER, name + ': ' + str(value) + '/' + str(maximum))
def renderScreen( panel ): tcod.console_clear( panel ) tcod.console_set_default_background( panel, tcod.Color( 210, 125, 44 ) ) tcod.console_rect( panel, 0, 0, 40, 20, True, tcod.BKGND_SET ) tcod.console_set_default_background( panel, tcod.Color( 132,126,135 ) ) tcod.console_rect( panel, 1, 1, 38, 18, True, tcod.BKGND_SET ) tcod.console_print_ex( panel, 20, 4, tcod.BKGND_NONE, tcod.CENTER, 'You died...' ) tcod.console_print_ex( panel, 20, 6, tcod.BKGND_NONE, tcod.CENTER, 'You scored %d points.' % ( self.score ) ) if time.time() - startTime > 2: tcod.console_print_ex( panel, 20, 14, tcod.BKGND_NONE, tcod.CENTER, 'Press escape to restart.' )
def draw_hud(player): libtcod.console_set_foreground_color(0, libtcod.white) X, Y, W, H = 0, 40, 30, 10 libtcod.console_set_background_color(0, libtcod.black) libtcod.console_rect(0, X, Y, W, H, True, libtcod.BKGND_SET) libtcod.console_put_char_ex(0, X + 1, Y + 1, player.symbol, player.color, libtcod.black) libtcod.console_print_left(0, X + 3, Y + 1, libtcod.BKGND_NONE, player.name) libtcod.console_print_left(0, X + 1, Y + 8, libtcod.BKGND_NONE, 'In {:s}'.format(player.map.name)) for i in range(10): if i * player.max_health > player.health * 10: libtcod.console_put_char_ex(0, X + 1 + i, Y + 3, ' ', libtcod.black, libtcod.darker_red) elif (i+1) * player.max_health > player.health * 10: libtcod.console_put_char_ex(0, X + 1 + i, Y + 3, ' ', libtcod.black, libtcod.color_lerp(libtcod.darker_red, libtcod.red, 10.*player.health/player.max_health - i)) else: libtcod.console_put_char_ex(0, X + 1 + i, Y + 3, ' ', libtcod.black, libtcod.red) for i in range(10): if player.energy > i: libtcod.console_put_char_ex(0, X + 1 + i, Y + 4, 'O', libtcod.blue + libtcod.white * 0.2, libtcod.black) else: libtcod.console_put_char_ex(0, X + 1 + i, Y + 4, 'O', libtcod.desaturated_blue * 0.5, libtcod.black) libtcod.console_print_left(0, X + 12, Y + 3, libtcod.BKGND_NONE, 'MH: - {}'.format(player.mainhand and player.mainhand.name or '-')) if player.mainhand: libtcod.console_put_char_ex(0, X + 16, Y + 3, player.mainhand.symbol, player.mainhand.color, libtcod.black) libtcod.console_print_left(0, X + 12, Y + 4, libtcod.BKGND_NONE, 'OH: - {}'.format(player.offhand and player.offhand.name or player.mainhand and player.mainhand.wield_twohands and player.mainhand.name or '-')) if player.offhand: libtcod.console_put_char_ex(0, X + 16, Y + 4, player.offhand.symbol, player.offhand.color, libtcod.black) elif player.mainhand and player.mainhand.wield_twohands: libtcod.console_put_char_ex(0, X + 16, Y + 4, player.mainhand.symbol, player.mainhand.color, libtcod.black) libtcod.console_print_left(0, X + 12, Y + 5, libtcod.BKGND_NONE, 'AR: - {}'.format(player.wearing and player.wearing.name or '-')) if player.wearing: libtcod.console_put_char_ex(0, X + 16, Y + 5, player.wearing.symbol, player.wearing.color, libtcod.black) draw_messages(player)
def render_bar(x, y, total_width, name, value, maximum, bar_color, back_color): global panel #render a bar bar_width = int(float(value) / maximum * total_width) libtcod.console_set_default_background(panel, back_color) libtcod.console_rect(panel, x, y, total_width, 1, False, libtcod.BKGND_SCREEN) libtcod.console_set_default_background(panel, bar_color) if bar_width > 0: libtcod.console_rect(panel, x, y, bar_width, 1, False, libtcod.BKGND_SCREEN) #text libtcod.console_set_default_foreground(panel, libtcod.white) libtcod.console_print_ex(panel, x+total_width/2, y, libtcod.BKGND_NONE, libtcod.CENTER, "{0}: {1}/{2}".format(name, value, maximum))
def render_bar(x, y, total_width, name, value, maximum, bar_color, back_color, text_color): bar_width = int(float(value) / maximum * total_width) #draw maximum bar libtcod.console_set_default_background(panel, back_color) libtcod.console_rect(panel, x, y, total_width, 1, False, libtcod.BKGND_SCREEN) #draw filled portion of bar if bar_width > 0: libtcod.console_set_default_background(panel, bar_color) libtcod.console_rect(panel, x, y, bar_width, 1, False, libtcod.BKGND_SCREEN) #draw label with exact numbers and what the bar represents libtcod.console_set_default_foreground(panel, text_color) libtcod.console_print_ex(panel, x + total_width / 2, y, libtcod.BKGND_NONE, libtcod.CENTER, name + ': ' + str(value) + '/' + str(maximum))
def render_all(): offset_row = player.loc.row - (MAP_WIDTH/2) offset_col = player.loc.col - (MAP_HEIGHT/2) lt.map_compute_fov(area.fov_map, player.loc.col, player.loc.row, SIGHT_RADIUS, True, FOV_ALGO) area.draw(offset_col, offset_row, MAP_WIDTH, MAP_HEIGHT) for item in area.items: item["loc"].draw(area) player.loc.draw(area) lt.console_set_key_color(area.con, lt.black) # Panel Rendering lt.console_clear(panel) lt.console_set_default_foreground(panel, lt.white) lt.console_print(panel, 1, 2, 'Overall:') text, color = injury_level(player.body.overall_health()) lt.console_set_default_foreground(panel, color) lt.console_print(panel, 1, 3, text) lt.console_set_default_foreground(panel, lt.white) lt.console_print(panel, 1, 5, 'Chest:') text, color = injury_level(player.body.chest.health()) lt.console_set_default_foreground(panel, color) lt.console_print(panel, 1, 6, text) lt.console_set_default_foreground(panel, lt.white) lt.console_print(panel, 1, 8, 'Head:') text, color = injury_level(player.body.head.health()) lt.console_set_default_foreground(panel, color) lt.console_print(panel, 1, 9, text) # Fog background behind the map. lt.console_set_default_background(frame, area.fog) lt.console_rect(frame, 1, 1, MAP_WIDTH, MAP_HEIGHT, True, lt.BKGND_SET) # A frame around the map lt.console_set_default_foreground(frame, lt.desaturated_amber) lt.console_set_default_background(frame, lt.Color(70, 60, 0)) for n in xrange(MAP_WIDTH + 2): lt.console_put_char(frame, n, 0, "+", lt.BKGND_SET) lt.console_put_char(frame, n, MAP_HEIGHT + 1, "+", lt.BKGND_SET) lt.console_put_char(frame, 0, n, "+", lt.BKGND_SET) lt.console_put_char(frame, MAP_WIDTH + 1, n, "+", lt.BKGND_SET) # Blit all lt.console_blit(frame, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0) lt.console_blit(panel, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, MAP_WIDTH + 2, 1) lt.console_blit(area.con, offset_row, offset_col, MAP_WIDTH, MAP_HEIGHT, 0, 1, 1) lt.console_flush()
def render_bar(x, y, total_width, name, value, maximum, bar_color, back_color, Game): #render a bar (HP, exp, etc). first calc the width of the bar bar_width = int(float(value) / maximum * total_width) #render the background first libtcod.console_set_default_background(Game.panel, back_color) libtcod.console_rect(Game.panel, x, y, total_width, 1, False, libtcod.BKGND_SCREEN) #now render the bar on top libtcod.console_set_default_background(Game.panel, bar_color) if bar_width > 0: libtcod.console_rect(Game.panel, x, y, bar_width, 1, False, libtcod.BKGND_SCREEN) #lastly add text libtcod.console_set_default_foreground(Game.panel, libtcod.white) libtcod.console_print_ex(Game.panel, x + total_width/2, y, libtcod.BKGND_NONE, libtcod.CENTER, name + ': ' + str(value) + '/' + str(maximum))
def render_bar(x, y, total_width, name, value, maximum, bar_color, back_color): float_value = float(value) float_max = float(maximum) bar_width = int((float_value/float_max)*total_width) #bar_width = int((float(value)/maximum) * total_width) libtcod.console_set_background_color(panel, back_color) libtcod.console_rect(panel, x, y, total_width, 1, False) libtcod.console_set_background_color(panel, bar_color) if bar_width > 0: libtcod.console_rect(panel, x, y, bar_width, 1, False) libtcod.console_set_foreground_color(panel, libtcod.white) libtcod.console_print_center(panel, x + total_width/2, y, libtcod.BKGND_NONE, name + ': ' + str(value) + '/' + str(maximum))
def render_bar(x, y, total_width, name, value, maximum, bar_color, back_color): #render a bar (HP, experience, etc) bar_width = int(float(value) / maximum * total_width) #render background libtcod.console_set_default_background(panel, back_color) libtcod.console_rect(panel, x, y, total_width, 1, False, libtcod.BKGND_SCREEN) #now render the bar on top libtcod.console_set_default_background(panel, bar_color) if bar_width > 0: libtcod.console_rect(panel, x, y, bar_width, 1, False, libtcod.BKGND_SCREEN) #sexy text libtcod.console_set_default_foreground(panel, libtcod.darkest_grey) libtcod.console_print_ex(panel, x + total_width / 2, y, libtcod.BKGND_NONE, libtcod.CENTER, name + ': ' + str(value) + '/' +str(maximum))
def render_bar(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 libtcod.console_set_background_color(panel, back_color) libtcod.console_rect(panel, x, y, total_width, 1, False) # now render the bar on top libtcod.console_set_background_color(panel, bar_color) if bar_width > 0: libtcod.console_rect(panel, x, y, bar_width, 1, False) # finally, some centered text with the values libtcod.console_set_foreground_color(panel, libtcod.white) libtcod.console_print_center(panel, x + total_width / 2, y, libtcod.BKGND_NONE, name + ': ' + str(value) + '/' + str(maximum)) # show the player's stats libtcod.console_set_foreground_color(con, libtcod.white) libtcod.console_print_left(con, 1, SCREEN_HEIGHT - 2, libtcod.BKGND_NONE, 'HP: ' + str(player.fighter.hp) + '/' + str(player.fighter.max_hp) +' ')
def render_bar(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 libtcod.console_set_background_color(panel, back_color) libtcod.console_rect(panel, x, y, total_width, 1, False) #now render the bar on top libtcod.console_set_background_color(panel, bar_color) if bar_width > 0: libtcod.console_rect(panel, x, y, bar_width, 1, False) #finally, some centered text with the values libtcod.console_set_foreground_color(panel, libtcod.black) libtcod.console_print_center(panel, x + total_width / 2, y, libtcod.BKGND_NONE, name + ': ' + str(value) + '/' + str(maximum))
def generate_world(source_map, dynamic_spawns='Sparse', wildlife_spawns='Sparse', simulate_ticks=1000, combat_test=False, save=True, thread=True): WORLD_INFO['inittime'] = time.time() WORLD_INFO['start_age'] = simulate_ticks WORLD_INFO['dynamic_spawns'] = dynamic_spawns WORLD_INFO['wildlife_spawns'] = wildlife_spawns WORLD_INFO['real_time_of_day'] = int( round(WORLD_INFO['length_of_day'] * .10)) WORLD_INFO['seed'] = time.time() WORLD_INFO['combat_test'] = combat_test WORLD_INFO['title'] = 'Operation %s' % language.generate_scheme_title( ).title() random.seed(WORLD_INFO['seed']) if WORLD_INFO['dynamic_spawns'] == 'Sparse': WORLD_INFO['dynamic_spawn_interval'] = [350, (1000, 1200)] elif WORLD_INFO['dynamic_spawns'] == 'Medium': WORLD_INFO['dynamic_spawn_interval'] = [350, (800, 999)] elif WORLD_INFO['dynamic_spawns'] == 'Heavy': WORLD_INFO['dynamic_spawn_interval'] = [350, (600, 799)] else: WORLD_INFO['dynamic_spawn_interval'] = [-1, (600, 799)] if WORLD_INFO['wildlife_spawns'] == 'Sparse': WORLD_INFO['wildlife_spawn_interval'] = [2500, (770, 990)] elif WORLD_INFO['wildlife_spawns'] == 'Medium': WORLD_INFO['wildlife_spawn_interval'] = [2500, (550, 700)] elif WORLD_INFO['wildlife_spawns'] == 'Heavy': WORLD_INFO['wildlife_spawn_interval'] = [2500, (250, 445)] else: WORLD_INFO['wildlife_spawn_interval'] = [-1, (250, 445)] weather.change_weather() create_region_spawns() randomize_item_spawns() lfe.focus_on(create_player()) alife.camps.create_all_camps() if thread: tcod.console_rect(0, 0, 0, WINDOW_SIZE[0], WINDOW_SIZE[1], True, flag=tcod.BKGND_DEFAULT) _r = Runner(simulate_ticks) _r.start() while _r.running: draw_world_stats() if not SETTINGS['running']: return False else: simulate_life(simulate_ticks) WORLD_INFO['id'] = 0 if save: WORLD_INFO['id'] = profiles.create_world() save_world(create=True) logging.info('World generation complete (took %.2fs)' % (time.time() - WORLD_INFO['inittime']))
def clear_console(console): # blacks out the specified screenspace libtcod.console_clear(console) libtcod.console_set_default_background(console, libtcod.black) libtcod.console_rect(console, 0, 0, gameconfig.SCREEN_WIDTH, gameconfig.SCREEN_HEIGHT, True, libtcod.BKGND_SET)
def draw(self, color=rl.white): if self.subBox != None: # If there is a sub-box, draw that instead. self.subBox.draw() return rl.console_rect(0, self.x, self.y, self.w, self.h, True) # Reset background color for the box. # Draw the corners. rl.console_put_char_ex(0, self.x, self.y, rl.CHAR_DNW, rl.console_get_default_foreground(0), rl.console_get_default_background(0)) rl.console_put_char_ex(0, self.x + self.w - 1, self.y, rl.CHAR_DNE, rl.console_get_default_foreground(0), rl.console_get_default_background(0)) rl.console_put_char_ex(0, self.x, self.y + self.h - 1, rl.CHAR_DSW, rl.console_get_default_foreground(0), rl.console_get_default_background(0)) rl.console_put_char_ex(0, self.x + self.w - 1, self.y + self.h - 1, rl.CHAR_DSE, rl.console_get_default_foreground(0), rl.console_get_default_background(0)) # Draw the walls. for i in range(self.y + 1, self.y + self.h - 1): rl.console_put_char_ex(0, self.x, i, rl.CHAR_DVLINE, rl.console_get_default_foreground(0), rl.console_get_default_background(0)) rl.console_put_char_ex(0, self.x + self.w - 1, i, rl.CHAR_DVLINE, rl.console_get_default_foreground(0), rl.console_get_default_background(0)) for i in range(self.y + 1, self.y + self.h - 4): rl.console_put_char_ex(0, self.dividerX, i, rl.CHAR_DVLINE, rl.console_get_default_foreground(0), rl.console_get_default_background(0)) for i in range(self.x + 1, self.x + self.w - 1): rl.console_put_char_ex(0, i, self.y, rl.CHAR_DHLINE, rl.console_get_default_foreground(0), rl.console_get_default_background(0)) rl.console_put_char_ex(0, i, self.y + self.h - 1, rl.CHAR_DHLINE, rl.console_get_default_foreground(0), rl.console_get_default_background(0)) rl.console_put_char_ex(0, i, self.y + self.h - 4, rl.CHAR_DHLINE, rl.console_get_default_foreground(0), rl.console_get_default_background(0)) # Draw the dividing joints between sections. rl.console_put_char_ex(0, self.dividerX, self.y, rl.CHAR_DTEES, rl.console_get_default_foreground(0), rl.console_get_default_background(0)) rl.console_put_char_ex(0, self.dividerX, self.y + self.h - 4, rl.CHAR_DTEEN, rl.console_get_default_foreground(0), rl.console_get_default_background(0)) rl.console_put_char_ex(0, self.x, self.y + self.h - 4, rl.CHAR_DTEEE, rl.console_get_default_foreground(0), rl.console_get_default_background(0)) rl.console_put_char_ex(0, self.x + self.w - 1, self.y + self.h - 4, rl.CHAR_DTEEW, rl.console_get_default_foreground(0), rl.console_get_default_background(0)) if self.title != None: # Draw the title, if present. rl.console_print(0, self.x + 2, self.y, " {0} ".format(self.title)) # Draw every option and its current value (or "..." if it leads to a new menu). for i, option in enumerate( self.menuObj[self.optOffset:min(self.optOffset + self.optCap, len(self.menuObj) )]): # Draw the options. if option[0] == None: rl.console_print(0, self.x + 4, self.y + 1 + i, option[3][0][3]) else: rl.console_print(0, self.x + 4, self.y + 1 + i, option[0]) if isinstance(option[3], list): rl.console_print(0, self.dividerX + 2, self.y + 1 + i, "...") else: rl.console_print( 0, self.dividerX + 2, self.y + 1 + i, str(option[3])[0:min(len(str(option[3])), 76 - self.dividerX)]) # Draw the description of the current option or the current input, whichever is needed. color = "" # The color to draw the input in. Or more specifically, the ascii mid-string color change code. Since the default is white, by default nothing needs to be done here. if self.inputMode != "": if self.inputIsValid( self.currentInput, self.menuObj[self.selectedOption][1], self.menuObj[self.selectedOption] [4]) == False: # If the input isn't valid, make the color red. color = chr(rl.COLCTRL_FORE_RGB) + chr(255) + chr(64) + chr(64) if self.inputMode == "Text": rl.console_print( 0, 2, self.y + self.h - 3, "Input the new value. Max length is {0}\n{2}{1}{3}".format( self.menuObj[self.selectedOption][4], self.currentInput[ self.inputOffset:min(self.inputOffset + 76, len(self.currentInput))], color, chr(rl.COLCTRL_STOP))) elif self.inputMode == "Number": rl.console_print( 0, 2, self.y + self.h - 3, "Input the new value. Max value is {0}\n{2}{1}{3}".format( self.menuObj[self.selectedOption][4], self.currentInput[ self.inputOffset:min(self.inputOffset + 76, len(self.currentInput))], color, chr(rl.COLCTRL_STOP))) else: rl.console_print_rect(0, 2, self.y + self.h - 3, 76, 2, self.menuObj[self.selectedOption][2]) rl.console_print(0, self.x + 2, self.y + 1 + self.selectedOption - self.optOffset, chr(rl.CHAR_ARROW2_E)) # Draw input cursor. if self.inputMode != "": rl.console_set_char_background( 0, 2 + self.cursorPos - self.inputOffset, self.y + self.h - 2, rl.white) rl.console_set_char_foreground( 0, 2 + self.cursorPos - self.inputOffset, self.y + self.h - 2, rl.black) # Draw scroll arrows as needed. if int(time.clock() * 2) % 2 == 0: if self.optOffset > 0: rl.console_print(0, self.x + int(self.w / 2), self.y, chr(rl.CHAR_ARROW2_N)) if self.optOffset + self.optCap < len(self.menuObj): rl.console_print(0, self.x + int(self.w / 2), self.y + self.h - 1, chr(rl.CHAR_ARROW2_S)) if self.inputOffset > 0: rl.console_print(0, self.x, self.y + self.h - 2, chr(rl.CHAR_ARROW2_W)) if self.inputOffset < len(self.currentInput) - 75: rl.console_print(0, self.x + self.w - 1, self.y + self.h - 2, chr(rl.CHAR_ARROW2_E))
def test_console_rect(console): libtcodpy.console_rect(console, 0, 0, 4, 4, False, libtcodpy.BKGND_SET)
def render_vert(panel, x, y, width, height, color): libtcod.console_set_default_background(panel, color) libtcod.console_rect(panel, x, y, width, height, False, libtcod.BKGND_SCREEN)
def render(self, entity): """Renders an entity's inventory in the given window. Arguments: entity - the entity whose inventory you want to render """ # Background libtcod.console_set_default_background(0, libtcod.black) libtcod.console_set_default_foreground(0, libtcod.black) libtcod.console_rect(0, self.x_offset, self.y_offset, self.width, self.height, True, libtcod.BKGND_SET) libtcod.console_set_default_foreground(0, libtcod.white) libtcod.console_print(0, self.x_offset + 1, self.y_offset + 1, entity.name) if entity.inventory is None: libtcod.console_print( 0, self.x_offset + 1, self.y_offset + 3, "This entity has no inventory. %s" % DEBUG_MSG) else: char_list = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" # Draw actions text - highlight the selected one libtcod.console_set_color_control(libtcod.COLCTRL_1, libtcod.blue, libtcod.black) libtcod.console_set_color_control(libtcod.COLCTRL_2, libtcod.blue, libtcod.white) libtcod.console_set_color_control(libtcod.COLCTRL_3, libtcod.black, libtcod.white) action_text = "%cU%cse%c %cD%crop%c %cT%chrow%c %cC%consume%c %cE%cquip%c %cW%cear%c" action_text_format = [ libtcod.COLCTRL_1, libtcod.COLCTRL_STOP, libtcod.COLCTRL_STOP ] * 6 if self.action_filter is not None: index = ["use", "drop", "throw", "consume", "equip", "wear"].index(self.action_filter) action_text_format[index * 3 + 0] = libtcod.COLCTRL_2 action_text_format[index * 3 + 1] = libtcod.COLCTRL_3 action_text = action_text % tuple(action_text_format) libtcod.console_print(0, self.x_offset + 1, self.y_offset + 3, action_text) # Set up screen offsets for rendering lcol = self.x_offset + 1 rcol = self.x_offset + 25 lrow = rrow = self.y_offset + 5 # In the case of no items, render_types will return 0, but still render a line (none) - so we need to # take this line into account when adjusting offsets. char_index = 0 num_weapons = self.render_types(entity.inventory, lcol, lrow, EntityType.WEAPON, "WEAPONS", char_list[char_index:]) lrow += max(1, num_weapons) + 3 char_index += num_weapons num_clothing = self.render_types(entity.inventory, rcol, rrow, EntityType.CLOTHING, "CLOTHING", char_list[char_index:]) rrow += max(1, num_clothing) + 3 char_index += num_clothing num_comestibles = self.render_types(entity.inventory, lcol, lrow, EntityType.COMESTIBLE, "COMESTIBLES", char_list[char_index:]) lrow += max(1, num_comestibles) + 3 char_index += num_comestibles num_medecine = self.render_types(entity.inventory, rcol, rrow, EntityType.MEDECINE, "MEDECINE", char_list[char_index:]) rrow += max(1, num_medecine) + 3 char_index += num_medecine num_other = self.render_types(entity.inventory, lcol, lrow, EntityType.OTHER, "OTHER", char_list[char_index:])
def menu(header, options, width, color=None, text_color=libtcod.white, alpha=1, center=False, additional_line=0, option_descriptions=None, flush=True, hidden=False, xOffset=0, yOffset=0): """ | Render a Full-Screen menu, overwriting the console. | @TODO Add multi-page support """ if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options.') header_height = libtcod.console_get_height_rect( gvar.con, 0, 0, width, gvar.SCREEN_HEIGHT, header) + 1 height = gvar.SCREEN_HEIGHT if flush == True: gvar.window = libtcod.console_new(gvar.SCREEN_WIDTH, gvar.SCREEN_HEIGHT) if not hidden: #print the background libtcod.console_set_default_foreground(gvar.window, text_color) if color is not None: libtcod.console_set_default_background(gvar.window, color) libtcod.console_rect(gvar.window, 1, 1, gvar.SCREEN_WIDTH - 2, height - 2, False, libtcod.BKGND_SET) #print the header with separating line if header != '': libtcod.console_print_rect_ex(gvar.window, 2, 2, width, height, libtcod.BKGND_NONE, libtcod.LEFT, header) libtcod.console_hline(gvar.window, 1, header_height + 1, gvar.SCREEN_WIDTH - 2, libtcod.BKGND_NONE) #print options y = header_height + 2 letter_index = ord('a') for option_text in options: index = options.index(option_text) if index == additional_line and additional_line != 0: libtcod.console_hline(gvar.window, 1, y, gvar.SCREEN_WIDTH - 2, libtcod.BKGND_NONE) y += 1 text = chr(letter_index).upper() + ' - ' + option_text libtcod.console_print_ex(gvar.window, 2, y, libtcod.BKGND_NONE, libtcod.LEFT, text) if option_descriptions is not None: libtcod.console_print_ex(gvar.window, 25, y, libtcod.BKGND_NONE, libtcod.LEFT, option_descriptions[index]) y += 1 letter_index += 1 #blit the contents of "window" to the root console if center == True: x = (gvar.SCREEN_WIDTH / 2 - width / 2) - 2 y = (gvar.SCREEN_HEIGHT / 2 - height / 2) - 2 else: x = 0 y = 0 libtcod.console_blit(gvar.window, 0, 0, width, height, 0, xOffset + x, yOffset + y, 1.0, alpha) #present the root console to the player and wait for a key-press libtcod.console_flush() key = libtcod.Key() mouse = libtcod.Mouse() key_pressed = libtcod.sys_wait_for_event(libtcod.EVENT_KEY_PRESS, key, mouse, True) if not key_pressed: return None if key.vk == libtcod.KEY_F10: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) if key.vk == libtcod.KEY_ESCAPE: return 'exit' #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
def menu(header, options, width): # general selection menu 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(gameconfig.con, 0, 0, width, gameconfig.SCREEN_HEIGHT, header) if header == '': header_height = 0 height = len(options) + header_height #create an off-screen console that represents the menu's window window = libtcod.console_new(width, height) libtcod.console_set_default_background(window, gameconfig.MENU_BKGND) libtcod.console_rect(window, 0, 0, width, height, False, libtcod.BKGND_SCREEN) #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) y = header_height letter_index = ord('a') for option_text in options: text = '(' + chr(letter_index) + ') ' + option_text libtcod.console_print_ex(window, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, text) y += 1 letter_index += 1 #blit window contents x = gameconfig.SCREEN_WIDTH / 2 - width / 2 y = gameconfig.SCREEN_HEIGHT / 2 - height / 2 libtcod.console_set_default_background(window, libtcod.red) 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() selected = 0 while True: key = libtcod.console_wait_for_keypress(True) if options: # selection loop if key.vk == libtcod.KEY_DOWN or key.vk == libtcod.KEY_UP: if key.vk == libtcod.KEY_DOWN: if selected < len(options): selected += 1 else: selected = 1 if key.vk == libtcod.KEY_UP: if selected > 1: selected -= 1 else: selected = len(options) # hightlight selected option libtcod.console_set_default_background(window, gameconfig.MENU_BKGND) libtcod.console_rect(window, 0, 0, width, height, False, libtcod.BKGND_SET) libtcod.console_set_default_background( window, gameconfig.MENU_SELECT_BKGND) libtcod.console_rect(window, 0, selected - 1 + header_height, width, 1, False, libtcod.BKGND_SCREEN) libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 1.0) libtcod.console_flush() if key.vk == libtcod.KEY_ENTER: return (selected - 1) # convert ascii to index index = key.c - ord('a') if index >= 0 and index < len(options): return index if key.vk == libtcod.KEY_ESCAPE: return None
def render_pip(panel, x, y, symbol, color1, color2): libtcod.console_set_default_background(panel, color2) libtcod.console_rect(panel, x + 1, y, 1, 1, False, libtcod.BKGND_SCREEN) libtcod.console_set_default_foreground(panel, color1) libtcod.console_print_ex(panel, x, y, libtcod.BKGND_NONE, libtcod.CENTER, '{0}'.format(symbol))
def test_console_printing_advanced(console): libtcodpy.console_rect(console, 0, 0, 4, 4, False, libtcodpy.BKGND_SET) libtcodpy.console_hline(console, 0, 0, 4) libtcodpy.console_vline(console, 0, 0, 4) libtcodpy.console_print_frame(console, 0, 0, 11, 11)
def render_side_panel_clear(self, i, bar_length=11, bar_offset_x=4): libtcod.console_set_default_background(self.con_panels[i], libtcod.black) libtcod.console_rect(self.con_panels[i], bar_offset_x - 1, 0, bar_length + 1, 40, True, libtcod.BKGND_SET)