def message(self, queue, mess_area): """ Displays a message queue in the message area. It takes only the last messages from the queue, depending on the screen area height. It also wraps up message texts when message length is too long. Arguments: queue : game's messages queue mess_area : the screen area where messages are displayed """ libtcod.console_clear(mess_area['con']) qmess = [] for i in range(mess_area['h']): try: qmess.append(queue[(i + 1) * -1]) except IndexError: break qlines = [] for mess in qmess: str_lines = textwrap.wrap(mess.message, mess_area['w']) qlines.extend([util.Message(line, mess.properties) for line in str_lines]) for i in range(mess_area['h']): try: libtcod.console_set_default_foreground(mess_area['con'], getcolorbyname(qlines[i].properties['color'])) libtcod.console_print_ex(mess_area['con'], 0, mess_area['h'] - i - 1, libtcod.BKGND_NONE, libtcod.LEFT, qlines[i].message) self.flush(mess_area) except IndexError: break
def draw(self, con, x, y, lit=False): col_bg = self.lit_col_bg if lit else self.unlit_col_bg ltc.console_set_char_background(con, x, y, col_bg, ltc.BKGND_SET) if self.char: col_fg = self.lit_col_fg if lit else self.unlit_col_fg ltc.console_set_default_foreground(con, col_fg) ltc.console_put_char(con, x, y, self.char, ltc.BKGND_NONE)
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_panel_border(con, w, h): ltc.console_set_default_foreground(con, ltc.white) ltc.console_put_char(con, 0, 0, ltc.CHAR_RADIO_UNSET) ltc.console_put_char(con, w - 1, 0, ltc.CHAR_RADIO_UNSET) ltc.console_put_char(con, 0, h - 1, ltc.CHAR_RADIO_UNSET) ltc.console_put_char(con, w - 1, h - 1, ltc.CHAR_RADIO_UNSET) for x in xrange(w - 2): ltc.console_put_char(con, x + 1, 0, ltc.CHAR_DHLINE) ltc.console_put_char(con, x + 1, h - 1, ltc.CHAR_DHLINE) for y in xrange(h - 2): ltc.console_put_char(con, 0, y + 1, ltc.CHAR_DVLINE) ltc.console_put_char(con, w - 1, y + 1, ltc.CHAR_DVLINE)
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 display_status (): global status if status: libtcod.console_rect(status_console, 0, 0, M.SCREEN_WIDTH, (M.SCREEN_HEIGHT - M.MAP_HEIGHT), True) libtcod.console_set_default_foreground (status_console, libtcod.white) while len(status) > M.SCREEN_WIDTH*2: display_statusline(status[:M.SCREEN_WIDTH*2]) key = libtcod.console_wait_for_keypress(True) while not key.vk == libtcod.KEY_SPACE: key = libtcod.console_wait_for_keypress(True) status = status[M.SCREEN_WIDTH*2:] display_statusline(status) libtcod.console_blit(status_console,0,0,M.SCREEN_WIDTH, (M.SCREEN_HEIGHT-M.MAP_HEIGHT-1),0,0,M.MAP_HEIGHT+1,1) libtcod.console_flush() else: display_statusline() libtcod.console_flush()
def __init__(self): self.state = 'playing' self.player_action = None self.map = Map() self.player = Object(self.map, self.map.start_x, self.map.start_y, '@', 'player', blocks=True) self.screen = Screen(self, self.map) self.screen.move(self.map.start_x - SCREEN_WIDTH/2, self.map.start_y - SCREEN_HEIGHT/2) self.fov_recompute = True self.con = libtcod.console_new(SCREEN_WIDTH, SCREEN_HEIGHT) libtcod.console_set_default_foreground(self.con, libtcod.white) self.pressed = set()
def render(): libtcod.console_rect(cons.game_console, 0, 0, M.MAP_WIDTH, M.MAP_HEIGHT, True) for y in range (M.MAP_HEIGHT): for x in range (M.MAP_WIDTH): # Draw black for all areas the player has not seen yet if not M.gameworld[x][y].explored: libtcod.console_set_char_background(cons.game_console, x, y, libtcod.black, libtcod.BKGND_SET) # Draw all the floors. if M.gameworld[x][y].is_floor () and M.gameworld[x][y].explored: libtcod.console_set_char_background (cons.game_console, x, y, M.DARK_FLOOR_COLOR, libtcod.BKGND_SET) # Draw all the walls. elif M.gameworld[x][y].explored: libtcod.console_set_char_background(cons.game_console, x, y, M.DARK_WALL_COLOR, libtcod.BKGND_SET) # Draw all the light floors. if (libtcod.map_is_in_fov (P.player.fov, x, y) and libtcod.map_is_walkable (P.player.fov, x, y)): libtcod.console_set_char_background (cons.game_console, x, y, M.FLOOR_COLOR, libtcod.BKGND_SET) for item in M.gameworld[x][y].items: libtcod.console_set_default_foreground (cons.game_console, item.color) libtcod.console_put_char (cons.game_console, x, y, item.char, libtcod.BKGND_NONE) for character in M.gameworld[x][y].characters: libtcod.console_set_default_foreground (cons.game_console, character.color) libtcod.console_put_char (cons.game_console, x, y, character.char, libtcod.BKGND_NONE) M.gameworld[x][y].explored=True # Draw all the light walls. elif libtcod.map_is_in_fov (P.player.fov, x, y): libtcod.console_set_char_background (cons.game_console, x, y, M.WALL_COLOR, libtcod.BKGND_SET) M.gameworld[x][y].explored=True # Blits the game console to the root console. libtcod.console_blit(cons.game_console,0,0,M.MAP_WIDTH,M.MAP_HEIGHT,0,0,0,1)
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 display(self, con): if self.game.fov_recompute: #recompute FOV if needed (the player moved or something) self.game.fov_recompute = False libtcod.map_compute_fov(self.map.fov_map, self.game.player.x, self.game.player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) for y in range(self.height): for x in range(self.width): map_x, map_y = x + self.x_offset, y + self.y_offset if self.map.in_bounds(map_x, map_y): visible = libtcod.map_is_in_fov(self.map.fov_map, map_x, map_y) tt = self.map.tiles[map_x][map_y].type if visible: libtcod.console_set_char_background(con, x, y, tt.bg_color_lit, libtcod.BKGND_SET) libtcod.console_set_default_foreground(con, tt.fg_color_lit) libtcod.console_put_char(con, x, y, tt.char, libtcod.BKGND_NONE) # TODO: Doing this here bugs it if the player's light radius is not on screen self.map.tiles[map_x][map_y].explored = True elif self.map.tiles[map_x][map_y].explored: libtcod.console_set_char_background(con, x, y, tt.bg_color, libtcod.BKGND_SET) libtcod.console_set_default_foreground(con, tt.fg_color) libtcod.console_put_char(con, x, y, tt.char, libtcod.BKGND_NONE) else: libtcod.console_set_char_background(con, x, y, void_color, libtcod.BKGND_SET) libtcod.console_put_char(con, x, y, ' ', libtcod.BKGND_NONE) else: libtcod.console_set_char_background(con, x, y, void_color, libtcod.BKGND_SET) libtcod.console_put_char(con, x, y, ' ', libtcod.BKGND_NONE) for object in self.map.objects: object.draw(con, self.x_offset, self.y_offset) libtcod.console_blit(con, 0, 0, self.width, self.height, 0, 0, 0) libtcod.console_flush()
def draw(self, con, x_off, y_off): if libtcod.map_is_in_fov(self.map.fov_map, self.x, self.y): libtcod.console_set_default_foreground(con, self.color) libtcod.console_put_char(con, self.x - x_off, self.y - y_off, self.char, libtcod.BKGND_NONE)
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 __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)
def draw_foreground(self, char, color, x, y): libtcod.console_set_default_foreground(self.con, color) libtcod.console_put_char(self.con, x, y, char, libtcod.BKGND_NONE)