def assertConsolesEqual(a, b): for y in range(libtcodpy.console_get_height(a)): for x in range(libtcodpy.console_get_width(a)): assert libtcodpy.console_get_char(a, x, y) == \ libtcodpy.console_get_char(b, x, y) assert libtcodpy.console_get_char_foreground(a, x, y) == \ libtcodpy.console_get_char_foreground(b, x, y) assert libtcodpy.console_get_char_background(a, x, y) == \ libtcodpy.console_get_char_background(b, x, y)
def test_array_read_write(): console = tcod.console.Console(width=12, height=10) FG = (255, 254, 253) BG = (1, 2, 3) CH = ord('&') tcod.console_put_char_ex(console, 0, 0, CH, FG, BG) assert console.ch[0, 0] == CH assert tuple(console.fg[0, 0]) == FG assert tuple(console.bg[0, 0]) == BG tcod.console_put_char_ex(console, 1, 2, CH, FG, BG) assert console.ch[2, 1] == CH assert tuple(console.fg[2, 1]) == FG assert tuple(console.bg[2, 1]) == BG console.clear() assert console.ch[1, 1] == ord(' ') assert tuple(console.fg[1, 1]) == (255, 255, 255) assert tuple(console.bg[1, 1]) == (0, 0, 0) ch_slice = console.ch[1, :] ch_slice[2] = CH console.fg[1, ::2] = FG console.bg[...] = BG assert tcod.console_get_char(console, 2, 1) == CH assert tuple(tcod.console_get_char_foreground(console, 2, 1)) == FG assert tuple(tcod.console_get_char_background(console, 2, 1)) == BG
def assert_char(console, x, y, ch=None, fg=None, bg=None): if ch is not None: try: ch = ord(ch) except TypeError: pass assert libtcodpy.console_get_char(console, x, y) == ch if fg is not None: assert libtcodpy.console_get_char_foreground(console, x, y) == fg if bg is not None: assert libtcodpy.console_get_char_background(console, x, y) == bg
def __getitem__(self, index): if isinstance(index, slice): raise TypeError('Console objects do not support slices. Yet.') x, y = index if x > self.width or x < 0 or y > self.height or y < 0: raise IndexError( 'Attempt to access cell ({0}, {1}), which is out of range. Console size is ({2}, {3}).' .format(x, y, self.width, self.height)) return ConsoleCell(chr(tcod.console_get_char(self._c, x, y)), tcod.console_get_char_foreground(self._c, x, y), tcod.console_get_char_background(self._c, x, y))
def __getitem__(self, index): if isinstance(index, slice): raise TypeError('Console objects do not support slices. Yet.') x, y = index if x > self.width or x < 0 or y > self.height or y < 0: raise IndexError( 'Attempt to access cell ({0}, {1}), which is out of range. Console size is ({2}, {3}).'.format(x, y, self.width, self.height)) return ConsoleCell(chr(tcod.console_get_char(self._c, x, y)), tcod.console_get_char_foreground(self._c, x, y), tcod.console_get_char_background(self._c, x, y))
def test_console_fill(console): width = libtcodpy.console_get_width(console) height = libtcodpy.console_get_height(console) fill = [i % 256 for i in range(width * height)] libtcodpy.console_fill_background(console, fill, fill, fill) libtcodpy.console_fill_foreground(console, fill, fill, fill) libtcodpy.console_fill_char(console, fill) # verify fill bg, fg, ch = [], [], [] for y in range(height): for x in range(width): bg.append(libtcodpy.console_get_char_background(console, x, y)[0]) fg.append(libtcodpy.console_get_char_foreground(console, x, y)[0]) ch.append(libtcodpy.console_get_char(console, x, y)) assert fill == bg assert fill == fg assert fill == ch
def look_mode(): global MESSAGES from game import decode_key x, y = GAME.player.x, GAME.player.y _messages = MESSAGES MESSAGES = [] message('Look mode - use movement keys, ESC/q to exit.', T.green) new_ui_turn() _draw_messages() redraw = True while True: if redraw: T.console_blit(CON_MAP, 0, 0, MAP_W, MAP_H, None, 1, 1) c = T.console_get_char(CON_MAP, x, y) color = T.console_get_char_foreground(CON_MAP, x, y) T.console_put_char_ex(None, x+1, y+1, c, T.black, color) describe_tile(x, y) _draw_messages() T.console_flush() # now clear the message buffer of last messages while MESSAGES and MESSAGES[-1][0]: MESSAGES.pop() redraw = False cmd = decode_key(readkey()) if cmd == 'quit': break elif isinstance(cmd, tuple): name, args = cmd if name == 'walk': dx, dy = args if in_map(x+dx, y+dy): x, y = x+dx, y+dy redraw = True MESSAGES = _messages
def draw_mouse(con, x, y, mouse_char, color): global old_mouse_pos, old_mouse_char, old_mouse_char_color # re draw the char that the mouse was at previously if old_mouse_pos[0] is not None: libtcod.console_set_default_foreground(con, old_mouse_char_color) libtcod.console_put_char(con, old_mouse_pos[0], old_mouse_pos[1], old_mouse_char, libtcod.BKGND_NONE) # get old variables for next time this function is called old_mouse_pos[0] = x old_mouse_pos[1] = y if libtcod.console_get_char(con, x, y) != mouse_char: old_mouse_char = libtcod.console_get_char(con, x, y) old_mouse_char_color = libtcod.console_get_char_foreground(con, x, y) # draw where the mouse is now libtcod.console_set_default_foreground(con, color) libtcod.console_put_char(con, x, y, mouse_char, libtcod.BKGND_NONE) libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0)
def test_console_fill_numpy(console): width = libtcodpy.console_get_width(console) height = libtcodpy.console_get_height(console) fill = numpy.zeros((height, width), dtype=numpy.intc) for y in range(height): fill[y, :] = y % 256 libtcodpy.console_fill_background(console, fill, fill, fill) libtcodpy.console_fill_foreground(console, fill, fill, fill) libtcodpy.console_fill_char(console, fill) # verify fill bg = numpy.zeros((height, width), dtype=numpy.intc) fg = numpy.zeros((height, width), dtype=numpy.intc) ch = numpy.zeros((height, width), dtype=numpy.intc) for y in range(height): for x in range(width): bg[y, x] = libtcodpy.console_get_char_background(console, x, y)[0] fg[y, x] = libtcodpy.console_get_char_foreground(console, x, y)[0] ch[y, x] = libtcodpy.console_get_char(console, x, y) fill = fill.tolist() assert fill == bg.tolist() assert fill == fg.tolist() assert fill == ch.tolist()
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, options_tutorial_enabled, game_state, names_list, colors_list, tick, tick_speed): 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) door = game_map.tiles[x][y].door wall = game_map.tiles[x][y].block_sight if door: libtcod.console_set_char_foreground( con, x, y, (255, 255, 255)) libtcod.console_set_char_background( con, x, y, colors.get('light_ground'), libtcod.BKGND_SET) libtcod.console_set_char(con, x, y, game_map.tiles[x][y].door.char) elif wall: #assign characters based on blitmap if game_map.blitmap[x][y] in [10]: #east-west walls if game_map.tiles[x][ y - 1].block_sight == False and game_map.tiles[x][ y + 1].empty_space: #floor above, empty space below libtcod.console_set_char_background( con, x, y, (0, 0, 0), libtcod.BKGND_SET) libtcod.console_set_char_foreground( con, x, y, colors.get('light_wall')) elif (game_map.tiles[x][y - 1].empty_space and game_map.tiles[x][y + 1].block_sight == False) or ( game_map.tiles[x][y - 1].block_sight == False and game_map.tiles[x][y + 1].block_sight == False): #empty space above, and floor below or floor above, floor below libtcod.console_set_char_background( con, x, y, colors.get('dark_ground'), libtcod.BKGND_SET) libtcod.console_set_char_foreground( con, x, y, colors.get('light_wall')) libtcod.console_set_char(con, x, y, 223) elif game_map.blitmap[x][y] in [0, 1, 2, 8, 11]: if game_map.tiles[x][y + 1].empty_space: libtcod.console_set_char_background( con, x, y, (0, 0, 0), libtcod.BKGND_SET) libtcod.console_set_char_foreground( con, x, y, colors.get('light_wall')) else: libtcod.console_set_char_background( con, x, y, colors.get('dark_ground'), libtcod.BKGND_SET) libtcod.console_set_char_foreground( con, x, y, colors.get('light_wall')) libtcod.console_set_char(con, x, y, 223) elif game_map.blitmap[x][y] in [3, 9]: #bottom left, bottom right corners if game_map.tiles[x][y + 1].empty_space: libtcod.console_set_char_background( con, x, y, (0, 0, 0), libtcod.BKGND_SET) libtcod.console_set_char_foreground( con, x, y, colors.get('light_wall')) else: libtcod.console_set_char_background( con, x, y, colors.get('dark_ground'), libtcod.BKGND_SET) libtcod.console_set_char_foreground( con, x, y, colors.get('light_wall')) libtcod.console_set_char(con, x, y, 223) elif game_map.blitmap[x][y] in [ 4, 5, 6, 7, 12, 13, 14, 15 ]: libtcod.console_set_char_background( con, x, y, colors.get('light_wall'), libtcod.BKGND_SET) libtcod.console_set_char_foreground( con, x, y, colors.get('light_wall')) libtcod.console_set_char(con, x, y, 218) else: #floor libtcod.console_set_char_background( con, x, y, colors.get('light_ground'), libtcod.BKGND_SET) if visible: game_map.tiles[x][y].explored = True else: if game_map.tiles[x][y].explored: #explored, not visible bgcolor = ( libtcod.console_get_char_background(con, x, y) * .33) fgcolor = ( libtcod.console_get_char_foreground(con, x, y) * .33) libtcod.console_set_char_background( con, x, y, bgcolor, libtcod.BKGND_SET) libtcod.console_set_char_foreground(con, x, y, fgcolor) else: libtcod.console_set_char_background( con, x, y, (0, 0, 0), libtcod.BKGND_SET) libtcod.console_set_char_foreground( con, x, y, (0, 0, 0)) 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, tick, tick_speed) 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) libtcod.console_set_default_foreground(panel, libtcod.white) if game_state == GameStates.KEYTARGETING: if player.y >= game_map.height / 2: #player on bottom half of the screen (tx, ty) = (58, 2) else: #player on top half of the map (tx, ty) = (58, 32) libtcod.console_set_default_background(0, libtcod.lighter_blue) libtcod.console_set_default_foreground(0, libtcod.black) libtcod.console_print_ex(0, tx, ty, libtcod.BKGND_SET, libtcod.RIGHT, "Press [esc] to exit targeting.") #print mouse x/y #libtcod.console_print_ex(panel, 1, 0, libtcod.BKGND_NONE, libtcod.LEFT, (str(mouse.cx) + "," + str(mouse.cy))) if options_tutorial_enabled: MAP_HEIGHT = game_map.height MAP_WIDTH = game_map.width if game_map.dungeon_level < 3: libtcod.console_set_default_background(0, libtcod.lighter_yellow) libtcod.console_set_default_foreground(0, libtcod.black) if player.y >= MAP_HEIGHT / 2: #player on bottom half of the screen (tx, ty) = (58, 2) else: #player on top half of the map (tx, ty) = (58, MAP_HEIGHT - 2) if game_state != GameStates.KEYTARGETING: if player.turn_count < 4: libtcod.console_print_ex( 0, tx, ty, libtcod.BKGND_SET, libtcod.RIGHT, "Use the numpad or arrow keys to move.") elif player.turn_count < 9: libtcod.console_print_ex( 0, tx, ty, libtcod.BKGND_SET, libtcod.RIGHT, "You can move into creatures to attack with a melee ") libtcod.console_print_ex( 0, tx, ty + 1, libtcod.BKGND_SET, libtcod.RIGHT, "weapon, or press [f] to fire a ranged weapon.") elif player.turn_count < 14: libtcod.console_print_ex( 0, tx, ty, libtcod.BKGND_SET, libtcod.RIGHT, "Press [x] to examine creatures or items on the ground." ) else: for ent in entities: if not ent.name == "Player": if ent.x == player.x and ent.y == player.y: if ent.name == "stairs": libtcod.console_print_ex( 0, tx, ty, libtcod.BKGND_SET, libtcod.RIGHT, "Press [Enter] to go down the stairs.") elif ent.item: if not game_map.tiles[ent.x][ent.y].door: libtcod.console_print_ex( 0, tx, ty, libtcod.BKGND_SET, libtcod.RIGHT, "Press [g] to grab an item.") myneighbors = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)] for dx, dy in myneighbors: tdx, tdy = player.x + dx, player.y + dy if game_map.tiles[tdx][tdy].door: if game_map.tiles[tdx][tdy].block_sight: libtcod.console_print_ex( 0, tx, ty, libtcod.BKGND_SET, libtcod.RIGHT, "Move into a closed door to open it.") else: libtcod.console_print_ex( 0, tx, ty, libtcod.BKGND_SET, libtcod.RIGHT, "Press [c] and then a direction to close an open door." ) # Print the game messages, one line at a time # 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', 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_print_ex(panel, 1, 4, libtcod.BKGND_NONE, libtcod.LEFT, # 'Turn : {0}'.format(player.turn_count)) #print condition icons if len(player.conditions) > 0: ind = 0 for condition in player.conditions: if condition.active: libtcod.console_print_ex(panel, 1 + ind, 2, libtcod.BKGND_SET, libtcod.LEFT, condition.char) libtcod.console_set_char_background(panel, 1 + ind, 2, condition.bgcolor, libtcod.BKGND_SET) libtcod.console_set_char_foreground(panel, 1 + ind, 2, condition.fgcolor) ind += 1 #timeline libtcod.console_set_default_foreground(panel, libtcod.dark_gray) libtcod.console_print_ex(panel, 22, 1, libtcod.BKGND_NONE, libtcod.LEFT, chr(195)) #195 > libtcod.console_print_ex(panel, 58, 1, libtcod.BKGND_NONE, libtcod.LEFT, chr(180)) #180 < for x in range(23, 58): libtcod.console_print_ex(panel, x, 1, libtcod.BKGND_NONE, libtcod.LEFT, chr(196)) #196 - libtcod.console_set_default_foreground(panel, libtcod.white) libtcod.console_print_ex(panel, 23, 1, libtcod.BKGND_NONE, libtcod.LEFT, chr(player.char)) #player char turn_order = [] for ent in entities: if not ent.name == player.name and ent.ai and libtcod.map_is_in_fov( fov_map, ent.x, ent.y): temp_enemy_timer = ent.fighter.timer + ent.fighter.speed while temp_enemy_timer >= player.fighter.speed: turn_order.append(ent) temp_enemy_timer -= player.fighter.speed x = 25 for ent in turn_order: if x < 58: libtcod.console_print_ex(panel, x, 1, libtcod.BKGND_NONE, libtcod.LEFT, chr(ent.char + 1)) x = x + 2 #check for timeline highlighting if mouse.cy == 37: if mouse.cx >= 25 and mouse.cx <= 57: timeline_index = (mouse.cx - 25) if mouse.cx % 2 == 0: timeline_index -= 1 timeline_index = int(timeline_index / 2) else: timeline_index = 99 if timeline_index <= len(turn_order) - 1 and tick % tick_speed < 2: ent = turn_order[timeline_index] libtcod.console_set_default_foreground(0, libtcod.lighter_yellow) libtcod.console_print_ex(0, ent.x, ent.y, libtcod.BKGND_NONE, libtcod.LEFT, chr(ent.char)) # TODO :: Look at this... this code was an attempt to highlight each occurance of a highlighted creature in the timeline #so if you highlight a rat, and he gets two turns, I wanted both of his sprites on the timeline to highlight. #didn't work, but it did manage to highlight the specific sprite you were hovering over on the timeline. # for e in turn_order: # if e == turn_order[timeline_index]: # ex = 25 + (2*timeline_index) # libtcod.console_set_default_foreground(panel, libtcod.lighter_yellow) # libtcod.console_print_ex(panel, ex, 1, libtcod.BKGND_NONE, libtcod.LEFT, chr(ent.char)) for ent in entities: if libtcod.map_is_in_fov( fov_map, ent.x, ent.y ) and ent.x == mouse.cx and ent.y == mouse.cy and ent.name != "Targeter" and ent.name != "Player": if ent.fighter: #print a context menu for the lil guy context_menu(game_map.width, game_map.height, ent, names_list) break else: render_tooltip(ent.x + 1, ent.y - 1, ent.name) if game_state == GameStates.KEYTARGETING: targeter = None for ent in entities: if ent.name == 'Targeter': targeter = ent if not targeter == None: #libtcod.console_set_default_foreground(panel, libtcod.light_gray) libtcod.console_print_ex( panel, 1, 0, libtcod.BKGND_NONE, libtcod.LEFT, get_all_at(targeter.x, targeter.y, entities, fov_map, game_map, names_list)) for ent in entities: if libtcod.map_is_in_fov( fov_map, ent.x, ent.y ) and ent.x == targeter.x and ent.y == targeter.y and ent.name != "Targeter" and ent.name != "Player": if ent.fighter: #print a context menu for the lil guy context_menu(game_map.width, game_map.height, ent, names_list) break else: render_tooltip(ent.x + 1, ent.y - 1, ent.name) libtcod.console_blit(panel, 0, 0, screen_width, panel_height, 0, 0, panel_y) if game_state == GameStates.PLAYERS_TURN: if len(player.conditions) > 0: if mouse.cy == 35: #the row where status icons are displayed if mouse.cx > 0 and mouse.cx <= len(player.conditions): render_tooltip(mouse.cx + 1, 36, player.conditions[mouse.cx - 1].tooltip) if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY): if game_state == GameStates.SHOW_INVENTORY: inventory_title = 'Press the key next to an item to use it, or Esc to cancel.\n' 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 get_char_foreground(self, x, y): return tcod.console_get_char_foreground(self._c, x, y)
def console_invert_color(con, x, y): col1 = libtcod.console_get_char_foreground(con, x, y) col2 = libtcod.console_get_char_background(con, x, y) libtcod.console_set_char_foreground(con, x, y, color_invert(col1)) libtcod.console_set_char_background(con, x, y, color_invert(col2))