def draw_cursor(mouse, cursor_radius, game_state, target_fov_map, fov_map_no_walls, screen_width, screen_height): #Checks to see that the item we are using has no radius so that we dont waste time computing a target reticule if cursor_radius == 1 or cursor_radius == None: cursor = libtcod.console_new(1, 1) libtcod.console_set_default_foreground(cursor, libtcod.white) cursor.draw_rect(0, 0, 1, 1, 0, bg=libtcod.white) libtcod.console_blit(cursor, 0, 0, 1, 1, 0, mouse.cx, mouse.cy, 1.0, 0.7) # If we have a radius greater than one then draw a circle with a radius of cursor_radius. Game state needs to be targetting, this makes it so when we cancel targetting our cursor goes back to normal elif game_state == GameStates.TARGETING: #I needed to add a buffer to the screen width otherwise the targeting reticule would wrap to the otehr side of the screen when it was on the left side. cursor = libtcod.console.Console(screen_width + 20, screen_height) libtcod.console_set_default_background(cursor, [245, 245, 245]) libtcod.console_set_key_color(cursor, [245, 245, 245]) cursor.draw_rect(0, 0, screen_width, screen_height, 0, bg=[245, 245, 245]) #Compute FOV from the cursors perspective. This makes it so walls etc. will block our reticule from showing green recompute_fov(target_fov_map, mouse.cx, mouse.cy, cursor_radius, light_walls=False, algorithm=libtcod.FOV_RESTRICTIVE) #Check all coords within the target radius from our cursors for x in range(mouse.cx - cursor_radius, mouse.cx + cursor_radius + 1): for y in range(mouse.cy - cursor_radius, mouse.cy + cursor_radius + 1): if math.sqrt((x - mouse.cx)**2 + (y - mouse.cy)**2) <= cursor_radius: #This FOV is computer from the player perspective, but with walls not lighting. This makes it so that if our cursors is on a wall the reticule will be red. if not libtcod.map_is_in_fov(fov_map_no_walls, x, y): cursor.draw_rect(x, y, 1, 1, 0, bg=libtcod.red) #Check FOV of the cursors so that walls will block our reticule. If coordinate is in FOV we color it green. elif libtcod.map_is_in_fov(target_fov_map, x, y): cursor.draw_rect(x, y, 1, 1, 0, bg=libtcod.light_green) else: cursor.draw_rect(x, y, 1, 1, 0, bg=libtcod.red) libtcod.console_blit(cursor, 0, 0, 0, 0, 0, 0, 0, 0, 0.4)
def test_console_blit(console, offscreen): libtcodpy.console_print(offscreen, 0, 0, 'test') libtcodpy.console_blit(offscreen, 0, 0, 0, 0, console, 0, 0, 1, 1) assertConsolesEqual(console, offscreen) libtcodpy.console_set_key_color(offscreen, libtcodpy.black)
def render_all(con, panel, tooltip, messages_pane, inventory_pane, entities, player, game_map, fov_map, fov_recompute, message_log, screen_width, screen_height, map_width, map_height, panel_width, panel_height, panel_x, mouse, colors, cam_x, cam_y, anim_frame, game_state, targeting_item, log_scroll, log_height, inv_scroll, inv_height, inv_selected): libtcod.console_clear(0) if fov_recompute or game_state == GameStates.TARGETING: # Draw all the tiles in the game map libtcod.console_set_default_foreground(con, libtcod.white) 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 game_state == GameStates.TARGETING and distanceBetween( math.floor((mouse.cx + cam_x) / 3), math.ceil((mouse.cy + cam_y) / 2), x, y) <= targeting_item.item.targeting_radius: backcolor = colors.get('red') else: backcolor = colors.get('light') game_map.tiles[x][y].explored = True else: backcolor = colors.get('dark') libtcod.console_set_char_background(con, x * 3, y * 2, backcolor, libtcod.BKGND_SET) libtcod.console_set_char_background(con, x * 3 + 1, y * 2, backcolor, libtcod.BKGND_SET) libtcod.console_set_char_background(con, x * 3 + 2, y * 2, backcolor, libtcod.BKGND_SET) libtcod.console_set_char_background(con, x * 3, y * 2 + 1, backcolor, libtcod.BKGND_SET) libtcod.console_set_char_background(con, x * 3 + 1, y * 2 + 1, backcolor, libtcod.BKGND_SET) libtcod.console_set_char_background(con, x * 3 + 2, y * 2 + 1, backcolor, libtcod.BKGND_SET) if (game_map.tiles[x][y].explored or visible) and wall: libtcod.console_put_char(con, x * 3, y * 2, tiles.get('wall_tile'), libtcod.BKGND_NONE) libtcod.console_put_char(con, x * 3 + 1, y * 2, tiles.get('wall_tile') + 1, libtcod.BKGND_NONE) libtcod.console_put_char(con, x * 3 + 2, y * 2, tiles.get('wall_tile') + 2, libtcod.BKGND_NONE) libtcod.console_put_char(con, x * 3, y * 2 + 1, tiles.get('wall_tile') + 32, libtcod.BKGND_NONE) libtcod.console_put_char(con, x * 3 + 1, y * 2 + 1, tiles.get('wall_tile') + 33, libtcod.BKGND_NONE) libtcod.console_put_char(con, x * 3 + 2, y * 2 + 1, tiles.get('wall_tile') + 34, libtcod.BKGND_NONE) # Draw all entities in the list entities_in_render_order = sorted(entities, key=lambda x: x.render_order.value) for entity in entities_in_render_order: draw_entity(con, entity, fov_map, anim_frame, game_map, game_state) libtcod.console_blit(con, 0, 0, map_width * 3, map_height * 2, 0, -cam_x, -cam_y) libtcod.console_set_default_background(panel, colors.get('light')) libtcod.console_set_default_foreground(panel, colors.get('dark')) libtcod.console_clear(panel) # Print the game messages, one line at a time libtcod.console_print_ex(panel, int(panel_width / 2), 3, libtcod.BKGND_SET, libtcod.CENTER, "-------- Messages --------") libtcod.console_set_default_background(messages_pane, colors.get('light')) libtcod.console_clear(messages_pane) y = 1 for message in message_log.messages: libtcod.console_set_default_foreground(messages_pane, message.color) libtcod.console_print_ex(messages_pane, message_log.x, y, libtcod.BKGND_NONE, libtcod.LEFT, message.text) y += 1 # if log_scroll > y - log_height: # log_scroll = y - log_height libtcod.console_blit(messages_pane, 0, y - log_height - log_scroll, panel_width - 3, log_height, panel, 2, 4) # libtcod.console_set_default_background(panel, colors.get('dark')) # libtcod.console_set_default_foreground(panel, colors.get('light')) # libtcod.console_put_char(panel, panel_width-1, 5, " ", libtcod.BKGND_SET) libtcod.console_put_char(panel, panel_width - 2, 4, tiles.get('up_tile'), libtcod.BKGND_SET) # libtcod.console_put_char(panel, panel_width-3, 5, " ", libtcod.BKGND_SET) # libtcod.console_put_char(panel, panel_width-1, 5+log_height, " ", libtcod.BKGND_SET) libtcod.console_put_char(panel, panel_width - 2, 4 + log_height, tiles.get('down_tile'), libtcod.BKGND_SET) # libtcod.console_put_char(panel, panel_width-3, 5+log_height, " ", libtcod.BKGND_SET) # Print the inventory items # libtcod.console_set_default_background(panel, colors.get('light')) # libtcod.console_set_default_foreground(panel, colors.get('dark')) libtcod.console_print_ex(panel, int(panel_width / 2), 5 + log_height, libtcod.BKGND_SET, libtcod.CENTER, "-------- Backpack --------") libtcod.console_set_default_foreground(panel, colors.get('green')) libtcod.console_print_ex(panel, int(panel_width / 2), 6 + log_height, libtcod.BKGND_SET, libtcod.CENTER, "<> select | [u]se | [d]rop") libtcod.console_set_default_background(inventory_pane, colors.get('light')) libtcod.console_clear(inventory_pane) y = 1 for item in player.inventory.items: if y == inv_selected + 1: libtcod.console_set_default_background(inventory_pane, colors.get('dark')) libtcod.console_set_default_foreground(inventory_pane, colors.get('light')) else: libtcod.console_set_default_background(inventory_pane, colors.get('light')) libtcod.console_set_default_foreground(inventory_pane, colors.get('dark')) if player.equipment.main_hand == item: libtcod.console_print_ex( inventory_pane, 0, y, libtcod.BKGND_SET, libtcod.LEFT, '{0} ({1}) (main)'.format(item.name, item.number)) elif player.equipment.off_hand == item: libtcod.console_print_ex( inventory_pane, 0, y, libtcod.BKGND_SET, libtcod.LEFT, '{0} ({1}) (off)'.format(item.name, item.number)) else: libtcod.console_print_ex( inventory_pane, 0, y, libtcod.BKGND_SET, libtcod.LEFT, '{0} ({1})'.format(item.name, item.number)) y += 1 libtcod.console_blit(inventory_pane, 0, y - inv_height, panel_width - 3, inv_height, panel, 2, 7 + log_height) render_bar(panel, 2, 1, panel_width - 4, 'Health', player.fighter.hp, player.fighter.max_hp, colors.get('green'), colors.get('dark'), colors.get('light')) libtcod.console_set_default_foreground(panel, colors.get('dark')) # libtcod.console_print_ex(panel, 1, 3, libtcod.BKGND_NONE, libtcod.LEFT, 'Dungeon level: {0}'.format(game_map.dungeon_level)) libtcod.console_set_default_background(panel, colors.get('light')) libtcod.console_set_default_foreground(panel, colors.get('dark')) for y in range(0, screen_height): libtcod.console_put_char(panel, 0, y, tiles.get('gradient_tile'), libtcod.BKGND_SET) libtcod.console_set_default_foreground(panel, colors.get('dark')) libtcod.console_set_default_background(panel, colors.get('light')) libtcod.console_blit(panel, 0, 0, panel_width, panel_height, 0, panel_x, 0) tooltip_text = get_names_under_mouse(cam_x, cam_y, mouse, entities, fov_map) tooltip_len = len(tooltip_text) libtcod.console_set_default_background(tooltip, libtcod.black) libtcod.console_clear(tooltip) libtcod.console_set_default_foreground(tooltip, colors.get('light')) libtcod.console_set_default_background(tooltip, colors.get('dark')) libtcod.console_print_ex(tooltip, 0, 0, libtcod.BKGND_SET, libtcod.LEFT, tooltip_text) libtcod.console_set_key_color(tooltip, libtcod.black) libtcod.console_blit(tooltip, 0, 0, tooltip_len, 1, 0, mouse.cx + 2, mouse.cy + 1) if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY): if game_state == GameStates.SHOW_INVENTORY: inventory_title = 'Use or equip item.\n' else: inventory_title = 'Drop an item.\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)
layer_0_console = libtcod.console_new(xp_data['layer_data'][0]['width'], xp_data['layer_data'][0]['height']) layer_1_console = libtcod.console_new(xp_data['layer_data'][1]['width'], xp_data['layer_data'][1]['height']) xp_loader.load_layer_to_console(layer_0_console, xp_data['layer_data'][0]) xp_loader.load_layer_to_console(layer_1_console, xp_data['layer_data'][1]) #################### # Sets the overlay layer transparency key to REXPaint's background transparency key. For completeness purposes, load_layer_to_console always writes every cell to the console - # REXPaint format note - layer 0 background is written out as 0,0,0. If you're making .xp files for UI overlays or whatever, be absolutely sure to #################### libtcod.console_set_key_color( layer_1_console, libtcod.Color(xp_loader.transparent_cell_back_r, xp_loader.transparent_cell_back_g, xp_loader.transparent_cell_back_b)) #################### # libtcod piping to actually put the console layers on screen. This will probably change quite a bit for your actual usage of libtcod #################### draw_layers = False while not libtcod.console_is_window_closed(): key = libtcod.console_check_for_keypress() if key.vk == libtcod.KEY_ESCAPE: break #exit game