def render_all(): global fov_map, color_dark_wall, color_light_wall global color_dark_ground, color_light_ground global fov_recompute if fov_recompute: fov_recompute = False libtcod.map_compute_fov(fov_map,player.x,player.y,TORCH_RADIUS,FOV_LIGHT_WALLS,FOV_ALGO) for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): visible = libtcod.map_is_in_fov(fov_map,x,y) wall = map[x][y].block_sight if not visible: if map[x][y].explored: if wall: libtcod.console_set_char_background(con,x,y,color_dark_wall,libtcod.BKGND_SET) else: libtcod.console_set_char_background(con,x,y,color_dark_ground,libtcod.BKGND_SET) #else, it's visible else: if wall: libtcod.console_set_char_background(con,x,y,color_light_wall,libtcod.BKGND_SET) else: libtcod.console_set_char_background(con,x,y,color_light_ground,libtcod.BKGND_SET) map[x][y].explored = True #draw all objects, with the player last for object in objects: if object != player: object.draw() player.draw() ################### ## GUI ELEMENTS #preparing to render to panel libtcod.console_set_default_background(panel, libtcod.black) libtcod.console_clear(panel) #display the message box y=1 for (line,color) in game_msgs: libtcod.console_set_default_foreground(panel,color) libtcod.console_print_ex(panel,MSG_X,y,libtcod.BKGND_NONE,libtcod.LEFT,line) y+=1 #show some stats #render_bar(1,1,BAR_WIDTH,'HP',player.body.hp,player.body.max_hp,libtcod.light_red,libtcod.darker_red) libtcod.console_set_default_foreground(panel,libtcod.light_gray) libtcod.console_print_ex(panel,1,0,libtcod.BKGND_NONE,libtcod.LEFT,get_names_under_mouse()) #blit the panel to the screen libtcod.console_blit(panel,0,0,SCREEN_WIDTH,PANEL_HEIGHT,0,0,PANEL_Y) #blit our off-screen console libtcod.console_blit(con,0,0,MAP_WIDTH,MAP_HEIGHT,0,0,0)
def show_menu(self, options, header, hide_options=False): """ Show menu with header and options in the screen. """ #calculate total height for the header (after auto-wrap) header_height = libtcod.console_get_height_rect(self.inv_window, 0, 0, MAP_WIDTH, MAP_HEIGHT, header) #print the header, with auto-wrap libtcod.console_set_default_foreground(self.inv_window, libtcod.white) libtcod.console_print_rect_ex(self.inv_window, 0, 0, MAP_WIDTH, MAP_HEIGHT, libtcod.BKGND_NONE, libtcod.LEFT, header) #print all the options y = header_height for (option_key, option_text) in options: if hide_options is True: text = option_text else: text = '(' + option_key + ') ' + option_text libtcod.console_print_ex(self.inv_window, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, text) y += 1 #blit the contents of "self.inv_window" to the root console x, y, _ = SCREEN_RECT.top_left.coords libtcod.console_blit(self.inv_window, 0, 0, MAP_WIDTH, MAP_HEIGHT, 0, x, y, 1.0, 0.7) libtcod.console_flush() libtcod.console_clear(self.inv_window)
def render_all(): # Go through all Tiles, and set their character and color. for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): # Draw it. libtcod.console_put_char_ex(con, x, y, map[x][y].char, map[x][y].color, libtcod.black) # Draw all objects in the list. for object in objects: object.draw() # Blit the contents of "con" to the root console. libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0) # Prepare to render the GUI panel. libtcod.console_set_default_background(panel, libtcod.black) libtcod.console_clear(panel) # Show the player's stats. libtcod.console_set_default_foreground(panel, libtcod.red) libtcod.console_print_ex(panel, 1, 1, libtcod.BKGND_NONE, libtcod.LEFT, 'Lives: ' + str(player.lives) + '/' + str(player.max_lives)) libtcod.console_set_default_foreground(panel, libtcod.white) libtcod.console_print_ex(panel, 1, 3, libtcod.BKGND_NONE, libtcod.LEFT, 'Level ' + str(level_number)) # Blit the contents of "panel" to the root console. libtcod.console_blit(panel, 0, 0, SCREEN_WIDTH, PANEL_HEIGHT, 0, 0, PANEL_Y)
def player_card(): #create an off-screen console that represents the card's window window = libtcod.console_new(30, 20) #print player stats libtcod.console_set_foreground_color(window, libtcod.white) libtcod.console_print_left(window, 1, 1, libtcod.BKGND_NONE, 'Player') libtcod.console_print_left(window, 1, 2, libtcod.BKGND_NONE, 'Class: ' + player.stats.plclass) libtcod.console_print_left(window, 1, 3, libtcod.BKGND_NONE, 'STR:' + str(player.stats.str)) libtcod.console_print_left(window, 1, 4, libtcod.BKGND_NONE, 'DEX:' + str(player.stats.dex)) libtcod.console_print_left(window, 1, 5, libtcod.BKGND_NONE, 'CON:' + str(player.stats.con)) libtcod.console_print_left(window, 1, 6, libtcod.BKGND_NONE, 'INT:' + str(player.stats.int)) libtcod.console_print_left(window, 1, 7, libtcod.BKGND_NONE, 'FTH:' + str(player.stats.fth)) libtcod.console_print_left(window, 1, 8, libtcod.BKGND_NONE, 'PER:' + str(player.stats.per)) libtcod.console_print_left(window, 1, 10, libtcod.BKGND_NONE, 'AC: ' + str(player.stats.ac)) libtcod.console_print_left(window, 1, 11, libtcod.BKGND_NONE, 'Encumbrance: ') libtcod.console_print_left(window, 1, 13, libtcod.BKGND_NONE, 'Hit %: ' + str((20-player.stats.hitdie)*5)) libtcod.console_print_left(window, 1, 14, libtcod.BKGND_NONE, 'Damage: ' + str(player.stats.mindmg) + ' - ' + str(player.stats.maxdmg)) #blit the contents of "window" to the root console libtcod.console_blit(window, 0, 0, 30, 20, 0, 1, 1, 1.0, 0.7) #present the root console to the player and wait for a key-press libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) if key.vk == libtcod.KEY_ENTER and key.lalt: #(special case) Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) return None
def menu(header, options, width): if len(options) > 26: raise ValueError("Cannot have a menu with more than 26 options.") #TODO: expand inventory. header_height = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header) height = len(options) + header_height window = libtcod.console_new(width, height) 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") #can be replaced with a list i'll iterate over when i want more positions for option_text in options: text = "({0}) {1}".format(chr(letter_index), option_text) libtcod.console_print_ex(window, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, text) y += 1 letter_index += 1 #center and show menu x = SCREEN_WIDTH/2 - width/2 y = SCREEN_HEIGHT/2 - height/2 libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7) libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) index = key.c - ord("a") if index >= 0 and index < len(options): return index else: return None
def render_all(): global fov_map, color_dark_wall, color_light_wall global color_dark_ground, color_light_ground global fov_recompute if fov_recompute: # recompute FOV if needed (the player moved or something) fov_recompute = False libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) # go through all tiles, and set their background color according to the FOV for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): visible = libtcod.map_is_in_fov(fov_map, x, y) wall = map[x][y].block_sight if not visible: # if it's not visible right now, the player can only see it if it's explored if map[x][y].explored: if wall: libtcod.console_set_char_background(con, x, y, color_dark_wall, libtcod.BKGND_SET) else: libtcod.console_set_char_background(con, x, y, color_dark_ground, libtcod.BKGND_SET) else: # it's visible if wall: libtcod.console_set_char_background(con, x, y, color_light_wall, libtcod.BKGND_SET) else: libtcod.console_set_char_background(con, x, y, color_light_ground, libtcod.BKGND_SET) # since it's visible, explore it map[x][y].explored = True # draw all objects in the list, except the player. we want it to # always appear over all other objects! so it's drawn later. for object in objects: if object != player: object.draw() player.draw() # blit the contents of "con" to the root console libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0) # prepare to render the GUI console libtcod.console_set_default_background(panel, libtcod.black) libtcod.console_clear(panel) # show the player's stats render_bar(1, 1, BAR_WIDTH, "HP", player.fighter.hp, player.fighter.max_hp, libtcod.light_red, libtcod.darker_red) # print the game messages, one line at a time y = 1 for (line, color) in game_msgs: libtcod.console_set_default_foreground(panel, color) libtcod.console_print_ex(panel, MSG_X, y, libtcod.BKGND_NONE, libtcod.LEFT, line) y += 1 libtcod.console_set_default_foreground(panel, libtcod.light_gray) libtcod.console_print_ex(panel, 1, 0, libtcod.BKGND_NONE, libtcod.LEFT, get_names_under_mouse()) # blit GUI to screen libtcod.console_blit(panel, 0, 0, SCREEN_WIDTH, PANEL_HEIGHT, 0, 0, PANEL_Y)
def menu(header, options, width): if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options') # Calc the header height after auto-wrap, one line per option header_height = libtcod.console_get_height_rect(con, 0, 0, width, opt.SCREEN_HEIGHT, header) if header == '': header_height = 0 height = len(options) + header_height # Create new offscreen window window = libtcod.console_new(width, height) # Print header with auto-wrap libtcod.console_set_default_foreground(window, libtcod.Color(230,230,230)) libtcod.console_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, header) # Print options y = header_height letter_index = ord('a') for option_text in options: # Print options in format a) Option text = chr(letter_index) + ') ' + option_text libtcod.console_print_ex(window, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, text) y += 1 letter_index += 1 x = opt.SCREEN_WIDTH/2 - width/2 y = opt.SCREEN_HEIGHT/2 - height/2 libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7) libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) if key.vk == libtcod.KEY_ENTER and key.lalt: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) # If an item was chosen, return it index = key.c - ord('a') if index >= 0 and index < len(options): return index return None
def menu(header, options, width): #add functionality of pages header_height = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header) height = len(options) + header_height window = libtcod.console_new(width, height) 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 x = SCREEN_WIDTH / 2 - width / 2 y = SCREEN_HEIGHT / 2 - height / 2 libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7) libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) index = key.c - ord('a') if index >= 0 and index < len(options): return index return None
def equipment_menu(con, header, player, equipment_menu_width, screen_width, screen_height, colors): """Show what items the player has equipped on the equipment slots.""" header_height = libtcod.console_get_height_rect(con, 0, 0, equipment_menu_width, screen_height, header) height = header_height + 10 window = libtcod.console_new(equipment_menu_width, height) libtcod.console_set_default_foreground(window, colors['text_default']) libtcod.console_set_color_control(libtcod.COLCTRL_1, colors['text_emphasize'], colors['background_default']) libtcod.console_set_color_control(libtcod.COLCTRL_2, colors['text_desaturate'], colors['background_default']) libtcod.console_set_color_control(libtcod.COLCTRL_3, colors['text_info'], colors['background_default']) libtcod.console_set_color_control(libtcod.COLCTRL_4, colors['text_info_alt'], colors['background_default']) slots = [(player.equipment.main_hand, 'Main hand'), (player.equipment.off_hand, 'Off hand'), (player.equipment.torso, 'Torso'), (player.equipment.head, 'Head'), (player.equipment.coat, 'Coat'), (player.equipment.ring_l, 'Ring (left)'), (player.equipment.ring_r, 'Ring (right)'), (player.equipment.special, 'Special')] libtcod.console_print_rect_ex(window, 0, 1, equipment_menu_width, height, libtcod.BKGND_NONE, libtcod.LEFT, header) line = header_height + 1 letter_index = ord('a') for slot, slot_desc in slots: equippable_name = '%cEmpty.%c' % (libtcod.COLCTRL_2, libtcod.COLCTRL_STOP) index_prefix = f'%c({chr(letter_index)})%c' % (libtcod.COLCTRL_1, libtcod.COLCTRL_STOP) if slot is not None: equippable_name = f'%c{slot.name} %c{slot.equippable}%c' % ( libtcod.COLCTRL_3, libtcod.COLCTRL_4, libtcod.COLCTRL_STOP) equippable_stats = str(slot.equippable) libtcod.console_print_rect_ex( window, 0, line, equipment_menu_width, height, libtcod.BKGND_NONE, libtcod.LEFT, '{0}{1}: {2}'.format(index_prefix, slot_desc, equippable_name)) line += 1 letter_index += 1 x = int(screen_width / 2 - equipment_menu_width / 2) y = int(screen_height / 2 - height / 2) libtcod.console_blit(window, 0, 0, equipment_menu_width, height, 0, x, y, 1.0, 0.7)
def character_screen(player, character_screen_width, character_screen_height, screen_width, screen_height): window = libtcod.console_new(character_screen_width, character_screen_height) libtcod.console_set_default_foreground(window, libtcod.white) libtcod.console_print_rect_ex(window, 0, 1, character_screen_width, character_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, 'Character Information') libtcod.console_print_rect_ex(window, 0, 2, character_screen_width, character_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, 'Level: {0}'.format(player.level.current_level)) libtcod.console_print_rect_ex(window, 0, 3, character_screen_width, character_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, 'Experience: {0}'.format(player.level.current_xp)) libtcod.console_print_rect_ex(window, 0, 4, character_screen_width, character_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, 'Experience to Level: {0}'.format(player.level.experience_to_next_level)) libtcod.console_print_rect_ex(window, 0, 6, character_screen_width, character_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, 'Maximum HP: {0}'.format(player.fighter.max_hp)) libtcod.console_print_rect_ex(window, 0, 7, character_screen_width, character_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, 'Attack: {0}'.format(player.fighter.power)) libtcod.console_print_rect_ex(window, 0, 8, character_screen_width, character_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, 'Defense: {0}'.format(player.fighter.defense)) libtcod.console_print_rect_ex(window, 0, 9, character_screen_width, character_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, 'Race: {0}'.format(player.fighter.race)) libtcod.console_print_rect_ex(window, 0, 9, character_screen_width, character_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, 'Gender: {0}'.format(Gender.male)) x = screen_width // 2 - character_screen_width // 2 y = screen_height // 2 - character_screen_height // 2 libtcod.console_blit(window, 0, 0, character_screen_width, character_screen_height, 0, x, y, 1.0, 0.7)
def menu(header, options, width): if len(options) > 26: raise ValueError('Cannot have menu with more than 26 options.') header_height = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header) if header == '': header_height = 0 height = len(options) + header_height window = libtcod.console_new(width, height) 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 x = SCREEN_WIDTH / 2 - width / 2 y = SCREEN_HEIGHT / 2 - height / 2 libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7) libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) if key.vk == libtcod.KEY_ENTER and key.lalt: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) index = key.c - ord('a') if index >= 0 and index < len(options): return index return None
def target_tile(max_range=None): box = libtcod.console_new(1, 1) x = Game.player.x y = Game.player.y libtcod.console_set_default_background(box, libtcod.orange) libtcod.console_clear(box) key = Game.key while (x, y) != (0, 0): Game.render_all() libtcod.console_blit(box, 0, 0, 1, 1, 0, x, y, 1.0, 0.5) #0.7 is the transparency libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) key = libtcod.console_wait_for_keypress(True) if key.vk == libtcod.KEY_ESCAPE: return (None, None) direction = Game.getDirection(key) if direction is not None: x += direction[0] y += direction[1] if direction == (0, 0): if Game.map.is_tile_in_fov( x, y) and (max_range is None or Game.player.distance(x, y) <= max_range): return (x, y) else: Game.message('That is out of range.', libtcod.red)
def render_all(): global color_light_wall global color_light_ground global fov_recompute move_camera(player.x, player.y) if fov_recompute == True: fov_recompute = False libtcod.console_clear(con) for y in range(CAMERA_HEIGHT): for x in range(CAMERA_WIDTH): (map_x, map_y) = (camera_x + x, camera_y + y) libtcod.console_put_char(con, x, y, map[x][y].variation, libtcod.BKGND_NONE) #go through all tiles, and set their variation # for y in range(MAP_HEIGHT): # for x in range(MAP_WIDTH): #draw all objects in the list for object in objects: object.draw() #blit the contents of "con" to the root console libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0)
def menu(header, options, width): if len(options) > 26: raise ValueError('More than 26 menu items not currently supported') header_height = libtcodpy.console_get_height_rect(_console, 0, 0, width, config.SCREEN_HEIGHT, header) height = len(options) + header_height window = libtcodpy.console_new(width, height) libtcodpy.console_set_default_foreground(window, libtcodpy.white) libtcodpy.console_print_rect_ex(window, 0, 0, window, height, libtcodpy.BKGND_NONE, libtcodpy.LEFT, header) y = header_height letter_index = ord('a') for option_text in options: text = '(' + chr(letter_index) + ') ' + option_text libtcodpy.console_print_ex(window, 0, y, libtcodpy.BKGND_NONE, libtcodpy.LEFT, text) y += 1 letter_index += 1 x = config.SCREEN_WIDTH / 2 - width / 2 y = config.SCREEN_HEIGHT / 2 - height / 2 libtcodpy.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7) libtcodpy.console_flush() key = libtcodpy.console_wait_for_keypress(True) index = key.c - ord('a') if 0 <= index < len(options): return index return None
def render_all(): global fov_map, color_dark_wall, color_light_wall global color_dark_ground, color_light_ground global fov_recompute if fov_recompute: fov_recompute = False libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): visible = libtcod.map_is_in_fov(fov_map, x, y) tile = map[x][y].sort if not visible: if map[x][y].explored: if tile == 'wall': libtcod.console_set_back(con, x, y, color_dark_wall, libtcod.BKGND_SET) elif tile == 'metal1': libtcod.console_set_back(con, x, y, color_dark_metal1, libtcod.BKGND_SET) elif tile == 'metal2': libtcod.console_set_back(con, x, y, color_dark_metal2, libtcod.BKGND_SET) else: libtcod.console_set_back(con, x, y, color_dark_ground, libtcod.BKGND_SET) else: if tile == 'wall': libtcod.console_set_back(con, x, y, color_light_wall, libtcod.BKGND_SET) elif tile == 'metal1': libtcod.console_set_back(con, x, y, color_light_metal1, libtcod.BKGND_SET) elif tile == 'metal2': libtcod.console_set_back(con, x, y, color_light_metal2, libtcod.BKGND_SET) else: libtcod.console_set_back(con, x, y, color_light_ground, libtcod.BKGND_SET) map[x][y].explored = True for object in objects: if object != player: object.draw() player.draw() libtcod.console_blit(con, 0, 0, MAP_WIDTH, MAP_HEIGHT, 0, 0, 0) libtcod.console_set_background_color(panel, libtcod.black) libtcod.console_clear(panel) y = 1 for (line, color) in game_msgs: libtcod.console_set_foreground_color(panel, color) libtcod.console_print_left(panel, MSG_X, y, libtcod.BKGND_NONE, line) y += 1 render_bar(1, 1, BAR_WIDTH, 'John\'s Oxygen', player.spaceman.oxygen, player.spaceman.max_oxygen, libtcod.light_red, libtcod.darker_red) render_bar(1, 3, BAR_WIDTH, 'Adam\'s Oxygen', npc.spaceman.oxygen, npc.spaceman.max_oxygen, libtcod.light_magenta, libtcod.darker_magenta) libtcod.console_set_foreground_color(panel, libtcod.light_gray) libtcod.console_print_left(panel, 1, 0, libtcod.BKGND_NONE, get_names_under_mouse()) libtcod.console_blit(panel, 0, 0, SCREEN_WIDTH, PANEL_HEIGHT, 0, 0, PANEL_Y) for object in objects: object.clear()
def menu(con, header, options, width, screenWidth, screenHeight): if len(options) > 28: raise ValueError('Cannot have a menu with more than 26 options.') # calculate height of header (post auto-wrap) and one line per option headerHeight = libtcod.console_get_height_rect(con,0,0,width, screenHeight, header) height = len(options) + headerHeight # create off-screen console that reps the menu's window window = libtcod.console_new(width,height) # print header w/ 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) # print options y = headerHeight letterIndex = ord('a') for optionText in options: text = '(' + chr(letterIndex) + ') ' + optionText libtcod.console_print_ex(window,0,y,libtcod.BKGND_NONE, libtcod.LEFT, text) y += 1 letterIndex += 1 # blit contents of window to root console x = int(screenWidth/2 - width/2) y = int(screenHeight/2 - height/2) libtcod.console_blit(window,0,0,width,height,0,x,y,1.0,0.7)
def main_menu(): libtcod.console_flush() while not libtcod.console_is_window_closed(): libtcod.console_clear(con) libtcod.console_blit(con,0,0,game.screen_width,game.screen_height,0,0,0) # Show the title, credits etc libtcod.console_set_default_foreground(0, libtcod.light_yellow) libtcod.console_print_ex(0,game.screen_width/2,game.screen_height/2-4, libtcod.BKGND_NONE, libtcod.CENTER, game.name) libtcod.console_print_ex(0,game.screen_width/2, game.screen_height-2,libtcod.BKGND_NONE, libtcod.CENTER, game.author + '2013') # Show options and wait for player's choice choice = menu('',['Play a new game', 'Continue from last game', 'Quit'], 30) if choice == 0: # new game new_game() play_game() elif choice == 1: # load game try: load_game() except: msgbox('\n No saved game to load.\n', 30) continue play_game() elif choice == 2: # quit break
def build(self): #print 'built!' libtcod.console_set_default_foreground(self.console, self.get_palette().LBLUE) libtcod.console_set_default_background(self.console, self.get_palette().DGREY) libtcod.console_print_frame(self.console, 0, 0, self.width, self.height, True, libtcod.BKGND_SET, 'fenetre1') temp = libtcod.console_new(self.width - 2, self.height - 2) libtcod.console_set_default_foreground(temp, self.get_palette().LBLUE) libtcod.console_set_default_background(temp, self.get_palette().DGREY) y = 1 for elem in self.content: libtcod.console_clear(temp) height = elem.build(temp) libtcod.console_blit(temp, 0, 0, elem.width, elem.height, self.console, 1, y) y += height
def blit(self, src, x=0, y=0, w=False, h=False, xdst=0, ydst=0, ffade=1.0, bfade=1.0): if not w: w = self.screenwidth if not h: h = self.screenheight libtcodpy.console_blit(src, x, y, w, h, 0, xdst, ydst, ffade, bfade)
def blit(self): for console in self.consoles: if console.visible: libtcod.console_blit(console.console, 0,0,0,0,0, console.x, console.y) # End of display : flush the console libtcod.console_flush()
def splashScreen(console): libtcod.console_set_default_background(console, SPLASH_BACKGROUND_COLOUR) libtcod.console_clear(console) done = False counter = 0 blinkOn = True blinkSpeed = 10 while not done and not libtcod.console_is_window_closed(): key = libtcod.console_check_for_keypress(True) if not key.vk == libtcod.KEY_NONE: done = True #print the splash messages libtcod.console_set_default_foreground(console, SPLASH_FOREGROUND_COLOUR1) libtcod.console_print_rect(console, 4, 5, SCREEN_WIDTH, SCREEN_HEIGHT, "SWORD SHOP") libtcod.console_set_default_foreground(console, SPLASH_FOREGROUND_COLOUR2) libtcod.console_print_rect(console, 4, 8, SCREEN_WIDTH, SCREEN_HEIGHT, "by luke david\n fitzpatrick") if(blinkOn): libtcod.console_set_default_foreground(console, SPLASH_FOREGROUND_COLOUR3) libtcod.console_print_rect(console, 16, 13, SCREEN_WIDTH, SCREEN_HEIGHT, "press any key") # blit the panel to the screen libtcod.console_blit(console, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-(2*6), 0, 0, 6) libtcod.console_flush() counter += 1 if counter >= blinkSpeed: counter = 0 blinkOn = not blinkOn libtcod.console_clear(console)
def draw(self): libtcod.console_clear(self.console) player = filter(lambda ent: ent[1].get_attribute(AttributeTag.Player), self.entity_manager.entities.iteritems())[0][1] player_position = player.get_attribute( AttributeTag.WorldPosition).data['value'] position_delta = Vec2d(0, 0) cur_action_number = 1 for queued_action in self.actions: if queued_action.type == ActionTag.ProgramMovement: position_delta += queued_action.data['value'] draw_info = player.get_attribute(AttributeTag.DrawInfo) target_position = player_position + position_delta libtcod.console_put_char_ex(self.console, target_position.x, target_position.y, str(cur_action_number), libtcod.blue, libtcod.black) elif queued_action.type == ActionTag.DamagePosition: target_position = None if 'relative' in queued_action.data: target_position = player_position + position_delta + queued_action.data[ 'relative'] else: #assume there's an absolute position listed. proibably should be generating a nice error here but that can be done soon(TM) target_position = queued_action.data['absolute'] libtcod.console_put_char_ex(self.console, target_position.x, target_position.y, str(cur_action_number), libtcod.dark_red, libtcod.black) cur_action_number += 1 if cur_action_number != 0 and cur_action_number == 10: cur_action_number = 0 libtcod.console_blit(self.console, 0, 0, self.width, self.height, 0, self.world_x_start, self.world_y_start)
def draw_panel(player, pointer_location): """ Refreshes the UI display and blits it to the window. """ libtcod.console_set_default_background(_panel, libtcod.black) libtcod.console_clear(_panel) # Only display the (log.MSG_HEIGHT) most recent write_log(log.game_msgs[-log.MSG_HEIGHT:], _panel, MSG_X, 1) _render_bar(1, 1, config.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 ' + str(player.current_map.dungeon_level)) # _debug_positions(player, mouse) # _debug_room(player) # _debug_danger(player) _debug_fps() libtcod.console_set_default_foreground(_panel, libtcod.light_gray) libtcod.console_print_ex( _panel, 1, 0, libtcod.BKGND_NONE, libtcod.LEFT, _get_names_under_mouse(player, pointer_location)) # Done with "_panel", blit it to the root console. libtcod.console_blit(_panel, 0, 0, config.SCREEN_WIDTH, config.PANEL_HEIGHT, 0, 0, PANEL_Y)
def render(self): c = self.console libtcod.console_clear(c) wx = cfg.SCREEN_WIDTH wy = cfg.SCREEN_HEIGHT self.playerReveal() offset_x = self.player.x - cfg.WID2 if offset_x + wx > self.width: offset_x = self.width - wx if offset_x < 0: offset_x = 0 offset_y = self.player.y - cfg.HGT2 if offset_y + wy > self.height: offset_y = self.height - wy if offset_y < 0: offset_y = 0 for x in xrange(wx): for y in xrange(wy): self.level[offset_x + x][offset_y + y].draw( c, offset_x, offset_y) for entity in self.tile_entity: pos = entity.position() #if self.level[pos[0]][pos[1]].seen: entity.draw(c,offset_x,offset_y) entity.draw(c, offset_x, offset_y) self.player.draw(c, offset_x, offset_y) libtcod.console_blit(self.console, 0, 0, wx, wy, 0, 0, 0)
def render_all(): global fov_map, color_dark_wall, color_light_wall, color_dark_ground, color_light_ground global fov_recompute, hunger, msg_index x_range = range(camera.x, camera.x + CAMERA_WIDTH) y_range = range(camera.y, camera.y + CAMERA_HEIGHT) if fov_recompute: # recompute FOV if need be (player movement or whatever) fov_recompute = False if 'fog' in map[player.x][player.y].mod_set: libtcod.map_compute_fov(fov_map, player.x, player.y, FOG_TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) else: libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) for y in y_range: for x in x_range: # uncomment this to return to exploration mode #visible = libtcod.map_is_in_fov(fov_map, x, y) visible = True wall = map[x][y].block_sight mods = map[x][y].mod_set if not visible: if map[x][y].explored: if wall: libtcod.console_set_char_background(con, x-camera.x, y-camera.y, color_dark_wall, libtcod.BKGND_SET) else: libtcod.console_set_char_background(con, x-camera.x, y-camera.y, color_dark_ground, libtcod.BKGND_SET) else: libtcod.console_set_char_background(con, x-camera.x, y-camera.y, libtcod.black, libtcod.BKGND_SET) else: if wall: #libtcod.console_set_default_foreground(con, libtcod.white) libtcod.console_set_char_background(con, x-camera.x, y-camera.y, color_light_wall, libtcod.BKGND_SET) else: # THIS ELIF IS FOR A DEBUG COLOR if map[x][y].h == 1: libtcod.console_set_char_background(con, x-camera.x, y-camera.y, color_blood, libtcod.BKGND_SET) else: libtcod.console_set_char_background(con, x-camera.x, y-camera.y, color_light_ground, libtcod.BKGND_SET) map[x][y].explored = True for obj in objects: # prevents drawing over the player if obj != player and (obj.x in x_range) and (obj.y in y_range): libtcod.console_set_default_foreground(con, obj.color) libtcod.console_put_char(con, obj.x-camera.x, obj.y-camera.y, obj.char, libtcod.BKGND_NONE) libtcod.console_set_default_foreground(con, player.color) libtcod.console_put_char(con, player.x-camera.x, player.y-camera.y, player.char, libtcod.BKGND_NONE) libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0)
def draw(self): libtcod.console_clear(self.console) xp_loader.load_layer_to_console(self.console, self.bg_data['layer_data'][0]) libtcod.console_set_alignment(self.console, libtcod.LEFT) libtcod.console_print( self.console, self.remaining_actions_display_position[0], self.remaining_actions_display_position[1], str(self.max_actions - self.current_action_count)) libtcod.console_print(self.console, self.max_actions_display_position[0], self.max_actions_display_position[1], str(self.max_actions)) for x in range(self.queued_actions_bar_width + 1): if x <= self.highlighted_tile_count and self.highlighted_tile_count > 0: libtcod.console_put_char( self.console, self.queued_actions_display_start[0] + x, self.queued_actions_display_start[1], chr(178)) else: libtcod.console_put_char( self.console, self.queued_actions_display_start[0] + x, self.queued_actions_display_start[1], chr(176)) libtcod.console_blit(self.console, 0, 0, self.width, self.height, 0, 0, self.y_blit_offset)
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, game_state): # Draw all the tiles in the game map if fov_recompute: 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 wall: libtcod.console_set_char_background(con, x, y, colors.get('light_wall'), libtcod.BKGND_SET) else: libtcod.console_set_char_background(con, x, y, colors.get('light_ground'), libtcod.BKGND_SET) game_map.tiles[x][y].explored = True elif game_map.tiles[x][y].explored: if wall: libtcod.console_set_char_background(con, x, y, colors.get('dark_wall'), libtcod.BKGND_SET) else: libtcod.console_set_char_background(con, x, y, colors.get('dark_ground'), libtcod.BKGND_SET) 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) 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) #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_set_default_foreground(panel, libtcod.light_gray) libtcod.console_print_ex(panel, 1, 0, libtcod.BKGND_NONE, libtcod.LEFT, get_names_under_mouse(mouse, entities, fov_map)) libtcod.console_blit(panel, 0, 0, screen_width, panel_height, 0, 0, panel_y) if game_state in (GameStates.SHOW_INVENTORY, GameStates.DROP_INVENTORY): if game_state == GameStates.SHOW_INVENTORY: inventory_title = 'Press the key next to the 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.inventory, 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 victory_screen(character_screen_width, character_screen_height, screen_width, screen_height): window = libtcod.console_new(character_screen_width, character_screen_height) libtcod.console_set_default_foreground(window, libtcod.white) window = libtcod.console_new(character_screen_width, character_screen_height) libtcod.console_set_default_foreground(window, libtcod.white) libtcod.console_print_rect_ex(window, 0, 4, character_screen_width, character_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, 'VICTORY !!') libtcod.console_print_rect_ex(window, 0, 5, character_screen_width, character_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, 'The Ancient King of the Horde is dead!') libtcod.console_print_rect_ex(window, 0, 9, character_screen_width, character_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, 'Press ESC to quit this game.') x = screen_width // 2 - character_screen_width // 2 y = screen_height // 2 - character_screen_height // 2 libtcod.console_blit(window, 0, 0, character_screen_width, character_screen_height, 0, x, y, 1.0, 0.7)
def panel2_display(): str_s = 'Str:' + str(player.fighter.status.Str) con_s = 'Con:' + str(player.fighter.status.Con) dex_s = 'Dex:' + str(player.fighter.status.Dex) int_s = 'Int:' + str(player.fighter.status.Int) car_s = player.fighter.status.career panel2_msgs = [player.name, str_s, con_s, dex_s, int_s, ' ', 'career', car_s, ' '] skill_msgs = ['Skills:'] #print skills s = player.fighter.total_skills for index in s: skill_msgs.append(str(index) + ')' + s[index][0] + ' [' + s[index][1] + ']') passive_msgs = ['','Passive:'] #print passive for index in player.fighter.status.passives: passive_msgs.append(player.fighter.status.passives[index]) panel2_msgs.extend(skill_msgs) panel2_msgs.extend(passive_msgs) libtcod.console_set_default_background(panel2, libtcod.black) libtcod.console_clear(panel2) y = 1 for lines in panel2_msgs: libtcod.console_set_default_foreground(panel2, libtcod.white) libtcod.console_print_ex(panel2, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, lines) y += 1 libtcod.console_blit(panel2, 0 , 0, PANEL2_WIDTH, PANEL2_HEIGHT, 0 ,PANEL2_X, 0)
def menu(con, header, options, width, screen_width, screen_height): if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options.') # calculate total height for the header header_height = libtcod.console_get_height_rect(con, 0, 0, width, screen_height, header) height = len(options) + header_height # create a off screen console for menu s window window = libtcod.console_new(width, height) # print t 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) # print all options 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 x = int(screen_width / 2 - width / 2) y = int(screen_height / 2 - height / 2) libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7)
def menu(con, header, options, width, screen_width, screen_height): 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(con, 0, 0, width, screen_height, header) height = len(options) + header_height # create an off-screen console that represents the menu's window window = libtcod.console_new(width, height) # 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) # print all the options 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 the contents of "window" to the root console x = int(screen_width / 2 - width / 2) y = int(screen_height / 2 - height / 2) libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7)
def menu(header, options, width): if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options.') #menu cannot have 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(con, 0, 0, width, SCREEN_HEIGHT, header) if header == "": header_height = 0 height = len(options) + header_height #creating new window to draw menu window = libtcod.console_new(width, height) #print header libtcod.console_set_default_foreground(window, libtcod.white) libtcod.console_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, header) #print options 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 to main screen x = SCREEN_WIDTH/2 - width/2 y = SCREEN_HEIGHT/2 - height/2 libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7) #last two parameters represent foreground and background transparency, respectively #flush and wait for keypress libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) if key.vk == libtcod.KEY_ENTER and key.lalt: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) #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(con, header, options, width, screen_width, screen_height): """Displays a generic menu of selectable 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 header_height = libtcod.console_get_height_rect(con, 0, 0, width, screen_height, header) height = len(options) + header_height # create an off-screen console that represents the menu's window window = libtcod.console_new(width, height) # 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) # print all the options 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 the contents of "window" to the root console x = int(screen_width / 2 - width / 2) y = int(screen_height / 2 - height / 2) libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7)
def job_screen(player, character_screen_width, character_screen_height, screen_width, screen_height): window = libtcod.console_new(character_screen_width, character_screen_height) libtcod.console_set_default_foreground(window, libtcod.white) libtcod.console_print_rect_ex( window, 0, 2, character_screen_width, character_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, 'Level in Fighter: {0}'.format(player.fighter.job.fighter_level)) libtcod.console_print_rect_ex( window, 0, 3, character_screen_width, character_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, 'Levels in Magician: {0}'.format(player.fighter.job.magician_level)) libtcod.console_print_rect_ex( window, 0, 4, character_screen_width, character_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, 'Levels in Cleric: {0}'.format(player.fighter.job.cleric_level)) libtcod.console_print_rect_ex( window, 0, 5, character_screen_width, character_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, 'Levels in Thief: {0}'.format(player.fighter.job.thief_level)) x = screen_width // 2 - character_screen_width // 2 y = screen_height // 2 - character_screen_height // 2 libtcod.console_blit(window, 0, 0, character_screen_width, character_screen_height, 0, x, y, 1.0, 0.7)
def render_all(self, target): if target is not None: global color_light_wall global color_light_ground for y in xrange(self.map.height): for x in xrange(self.map.width): visible = libtcod.map_is_in_fov(self.hero.fov_map, x, y) wall = self.map.tiles[x][y].block_sight if visible: if wall: libtcod.console_set_char_background( target, x, y, color_light_wall, libtcod.BKGND_SET) else: libtcod.console_set_char_background( target, x, y, color_light_ground, libtcod.BKGND_SET) self.map.tiles[x][y].explored = True else: if self.map.tiles[x][y].explored: if wall: libtcod.console_set_char_background( target, x, y, color_dark_wall, libtcod.BKGND_SET) else: libtcod.console_set_char_background( target, x, y, color_dark_ground, libtcod.BKGND_SET) for o in self.npcs: if libtcod.map_is_in_fov(self.hero.fov_map, o.x, o.y): o.draw(target) self.hero.draw(target) libtcod.console_blit(target, 0, 0, SCREEN_WIDE, SCREEN_HIGH, 0, 0, 0)
def menu(con, header, options, width, screen_width, screen_height): global key, mouse # calculate total height for the header (after auto-wrap) and one line per option header_height = libtcod.console_get_height_rect(con, 0, 0, width, screen_height, header) height = len(options) + header_height # create an off-screen console that represents the menu's window window = libtcod.console_new(width, height) # 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) # print all the options 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 the contents of "window" to the root console x = int(screen_width / 2 - width / 2) y = int(screen_height / 2 - height / 2) libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7)
def display(self): libtcod.console_set_default_foreground(self.console, libtcod.white) hptext = "Dead." if self.player.properties['combat'] is not None: hptext = 'HP: ' + str(self.player.properties['combat'].hp) + '/' + str(self.player.properties['combat'].max_hp) libtcod.console_print_ex(self.console, 1, 0, libtcod.BKGND_NONE, libtcod.LEFT, ('{0: <' + str(self.width) + '}').format(hptext)) libtcod.console_blit(self.console, 0, 0, self.width, self.height, 0, self.x, self.y)
def draw_console(player): """ Refreshes the map display and blits to the window. Sets or clears player.endangered. """ global _con current_map = player.current_map if current_map.is_outdoors and current_map.fov_elevation_changed: current_map.set_fov_elevation(player) current_map.fov_elevation_changed = False if current_map.fov_needs_recompute: libtcod.map_compute_fov(player.current_map.fov_map, player.x, player.y, config.TORCH_RADIUS, config.FOV_LIGHT_WALLS, config.FOV_ALGO) # Redraw if FOV (could have) changed. if current_map.is_outdoors: _draw_outdoors(player) else: _draw_indoors(player) # Draw all objects in the list, except the player. We want it to # always appear over all other objects, so it's drawn later. # (Could also achieve this by guaranteeing the player is always # the last object in current_map.objects.) for object in player.visible_objects: _draw_object(player, object) _draw_object(player, player) libtcod.console_blit(_con, 0, 0, config.MAP_PANEL_WIDTH, config.MAP_PANEL_HEIGHT, 0, 0, 0)
def menu(header, options, width): if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options') # get geometry header_height = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header) height = len(options) + header_height # create an off screen window window = libtcod.console_new(width, height) libtcod.console_set_default_foreground(window, libtcod.white) libtcod.console_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, header) #print all the options 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 x = SCREEN_WIDTH/2 - width/2 y = SCREEN_HEIGHT/2 - height/2 libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7) libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) index = key.c - ord('a') if index >= 0 and index < len(options): return index return None
def menu(header, options, width): if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options') #calculate total height for the header, and one line per option header_height = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header) height = len(options) + header_height #create off-screen console that represeents menu's window window = libtcod.console_new(width, height) #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) #print each option 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 the contents of window to the root console x = SCREEN_WIDTH/2 - width/2 y = SCREEN_HEIGHT/2 - height/2 libtcod.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 libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) index = key.c - ord('a') if index >= 0 and index < len(options): return index return None
def menu(header, options, width): if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options.') #calculate total height for the header after autowrap and one line per option header_height = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header) height = len(options) + header_height #create another offscreen console for the menu's window window = libtcod.console_new(width, height) #print the header, with autowrap 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') #start the list of inventory items with ordinal letter a for option_text in options: text = '(' + chr(letter_index) + ') ' + option_text #converts ordinal to a string for selection libtcod.console_print_ex(window, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, text) y += 1 letter_index += 1 #blit the contents of the inventory window to the root console in the middle of the screen x = SCREEN_WIDTH/2 - width/2 y = SCREEN_HEIGHT/2 - height/2 libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7) #last two values transparency% #present to the root console to the player and wait for key-press libtcod.console_flush() key = libtcod.console_wait_for_keypress(True)
def render_all(con, entities, player, game_map, fov_map, fov_recompute, screen_width, screen_height, colors): if fov_recompute: 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 wall: libtcod.console_set_char_background(con,x,y,colors.get("light_wall"),libtcod.BKGND_SET) else: libtcod.console_set_char_background(con, x, y, colors.get('light_ground'), libtcod.BKGND_SET) game_map.tiles[x][y].explored=True elif game_map.tiles[x][y].explored: if wall: libtcod.console_set_char_background(con, x, y, colors.get('dark_wall'), libtcod.BKGND_SET) else: libtcod.console_set_char_background(con, x, y, colors.get('dark_ground'), libtcod.BKGND_SET) 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) libtcod.console_set_default_foreground(con,libtcod.white) libtcod.console_print_ex(con,1,screen_height -2,libtcod.BKGND_NONE,libtcod.LEFT,"HP: {0:02}/{1:02}".format(player.fighter.hp,player.fighter.max_hp)) libtcod.console_blit(con, 0, 0, screen_width, screen_height, 0, 0, 0)
def storyScreen(console): libtcod.console_set_default_background(console, STORY_BACKGROUND_COLOUR1) libtcod.console_clear(console) done = False counter = 0 blinkOn = True blinkSpeed = 10 while not done and not libtcod.console_is_window_closed(): key = libtcod.console_check_for_keypress(True) if not key.vk == libtcod.KEY_NONE: done = True #print the story message libtcod.console_set_default_foreground(console, STORY_FOREGROUND_COLOUR1) libtcod.console_print_rect(console, 1, 3, SCREEN_WIDTH, SCREEN_HEIGHT, STORY_TEXT1) if(blinkOn): libtcod.console_set_default_foreground(console, SPLASH_FOREGROUND_COLOUR3) libtcod.console_print_rect(console, 16, 22, SCREEN_WIDTH, SCREEN_HEIGHT, "press any key") # blit the panel to the screen libtcod.console_blit(console, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-(2*1), 0, 0, 1) libtcod.console_flush() counter += 1 if counter >= blinkSpeed: counter = 0 blinkOn = not blinkOn libtcod.console_clear(console) libtcod.console_clear(console)
def dbox(x, y, w, h, text, wrap=True, border=0, margin=0, con=0, disp='poly'): con_box = libtcod.console_new(w, h) pad = 0 if border == None else 1 offset = margin + pad fulltext = textwrap.fill(text, w - 2 * (margin + pad)) if wrap else text boxes = word.split_stanza( fulltext, h - 2 * (margin + pad)) if disp == 'poly' else [fulltext] length = len(boxes) i = 0 for box in boxes: i += 1 libtcod.console_clear(con_box) # print if border is not None: rectangle(con_box, 0, 0, w, h, border) libtcod.console_print(con_box, offset, offset, box) put_text_special_colors(con_box, box, offset) libtcod.console_blit( con_box, 0, 0, w, h, # Source con, x, y) # Destination # wait for user input to continue... if i < length: rog.blit_to_final(con, 0, 0) rog.refresh() while True: reply = rog.Input(x + w - 1, y + h - 1, mode="wait") if (reply == ' ' or reply == ''): break libtcod.console_delete(con_box)
def draw_console(player): """ Refreshes the map display and blits to the window. Sets or clears player.endangered. """ global _con current_map = player.current_map if current_map.fov_needs_recompute: # Recompute FOV if needed (the player moved or something in # the dungeon changed). libtcod.map_compute_fov( current_map.fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) _draw_fov_using_terrain(player) # Draw all objects in the list, except the player. We want it to # always appear over all other objects, so it's drawn later. # (Could also achieve this by guaranteeing the player is always # the last object in current_map.objects.) for object in player.visible_objects: _draw_object(player, object) _draw_object(player, player) libtcod.console_blit(_con, 0, 0, config.MAP_PANEL_WIDTH, config.MAP_PANEL_HEIGHT, 0, 0, 0)
def get_targeting_radius(con, panel, x, y, entities, player, game_map, fov_map, fov_recompute, message_log, screen_width, screen_height, bar_width, panel_height, panel_y, mouse, colors, game_state, camera, left_click, right_click, animation_console): #여기서부터 커서 libtcod.console_set_char_background(animation_console, x, y, libtcod.dark_yellow) libtcod.console_blit(animation_console, 0, 0, screen_width, screen_height, 0, 0, 0) libtcod.console_flush() #마우스 위치 변경 감지시 클리어 if mouse.dcx != 0 or mouse.dcy != 0 or mouse.dcx != 0 and mouse.dcy != 0: libtcod.console_clear(animation_console) #왼클릭 올 오른클릭 감지시 좌표 리턴 if handle_mouse(mouse).get('left_click'): left_click = (x, y) return left_click elif handle_mouse(mouse).get('right_click'): right_click = (x, y) return right_click #다음 좌표 변경까지 0.1초 대기(깜빡임 방지) sleep(0.1) libtcod.console_clear(animation_console)
def draw(self): self.map.draw(self.con) self.player.draw(self.con) x = camera.Camera.X - (conf.GAME_WIDTH / 2) y = camera.Camera.Y - (conf.SCREEN_HEIGHT / 2) libtcod.console_blit(self.con, x, y, conf.SCREEN_WIDTH, conf.SCREEN_HEIGHT, 0, 0, 0)
def render_top_down(): for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): z = -1 for zDepth in xrange(MAP_DEPTH - 1, -1, -1): #Drop a line down on each square, compute if we can see that based on previously processed squares if map[x][y][zDepth].block_sight: z = zDepth break print str((x, y, z)) if z != -1: tile = map[x][y][z] r, g, b = tile.light_color #If player can see more than 1 adjacent? tile in that z-depth, it's a wall r += z g += z b += z libtcod.console_set_char_background(con, x, y, libtcod.Color(r, g, b), libtcod.BKGND_SET) else: libtcod.console_set_char_background(con, x, y, libtcod.Color(0, 0, 0), libtcod.BKGND_SET) #Draw player for object in objects: object.draw() #blit the contents of "con" to the root console libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0)
def menu(header, options, width): #First, make sure the menu has 26 or fewer items (This is due to an alphabetical selection limitation) if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options!') #implicitly calculate the height of the window, based on the header height (after word wrap) and the number # of options header_height = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header) if header == '': header_height = 0 height = len(options) + header_height #Create an offscreen console that represents the menus window, and a slightly larger one to nest the menu inside of #This will create a border effect for the inner menu, strictly asthetic outer_window = libtcod.console_new(width + 2, height + 2) window = libtcod.console_new(width, height) #Print the header to our offscreen console libtcod.console_set_default_foreground(window, libtcod.white) libtcod.console_set_default_background(window, libtcod.darker_sepia) libtcod.console_clear(window) libtcod.console_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, header) #Print all the options, with a corresponding ASCII character y = header_height #Get the ASCII value of the letter 'a' 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 the contents of the window to the main game screen, centered x = SCREEN_WIDTH / 2 - width / 2 y = SCREEN_HEIGHT /2 - height / 2 #Set up the outer window (which only acts as a border for the inner window, strictly graphical) libtcod.console_set_default_background(outer_window, libtcod.brass) libtcod.console_clear(outer_window) #Blit the actual message window onto the outer window, centered and one off from the borders libtcod.console_blit(window, 0, 0, width, height, outer_window, 1, 1) #Blit the outer window onto the screen, centered libtcod.console_blit(outer_window, 0, 0, width + 2, height + 2, 0, x, y) #Now that the console is presented to the player, wait for them to make a choice before doing anything else libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) #Clear the main console, so no artifacts from the menu appear libtcod.console_clear(0) #Check for fullscreen keys if key.vk == libtcod.KEY_ENTER and key.lalt: #ALT + Enter, toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) #Convert the ASCII code to an index; if it corresponds to a valid menu item, return it index = key.c - ord('a') if index >= 0 and index < len(options): return index return None
def menu(header, options, width): if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options.') #calculate the total height for the header after auto-wrap, and one line per option header_height = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header) height = len(options) + header_height #print an off-screen console that represents the menu's window window = libtcod.console_new(width, height) #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) #print all the options 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 the contents of 'window' to the root console x = SCREEN_WIDTH / 2 - width / 2 y = SCREEN_HEIGHT / 2 - height / 2 libtcod.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 libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) #Bugged, try the waitForEvent API
def render(self): libtcod.console_set_default_background(0, libtcod.black) libtcod.console_set_default_background(self.panel, libtcod.black) libtcod.console_clear(0) libtcod.console_clear(self.panel) self.screens[-1].render() libtcod.console_blit(self.panel, 0, 0, self.screen_width, self.screen_height, 0, 0, 0, 1, 1)
def render_all(con, entities, game_map, fov_map, fov_recompute, screen_width, screen_height, colors): # Draw all entities in the list for entity in entities: draw_entity(con, entity, fov_map) if fov_recompute: 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 wall: libtcod.console_set_char_background( con, x, y, colors.get('light_wall'), libtcod.BKGND_SET) else: libtcod.console_set_char_background( con, x, y, colors.get('light_ground'), libtcod.BKGND_SET) game_map.tiles[x][y].explored = True elif game_map.tiles[x][y].explored: if wall: libtcod.console_set_char_background( con, x, y, colors.get('dark_wall'), libtcod.BKGND_SET) else: libtcod.console_set_char_background( con, x, y, colors.get('dark_ground'), libtcod.BKGND_SET) libtcod.console_blit(con, 0, 0, screen_width, screen_height, 0, 0, 0)
def RenderEverything(): player = None for ent in ActiveEntityList: if 'ControlledByPlayer' and 'CanSee' in ent.flags: player = ent break #player = TestDummy for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): terraintile = Map[x][y] if player != None: canseetile = player.Vision.CanSee(x,y) if canseetile: libtcod.console_set_char_background(MainConsole,x,y,terraintile.bgcolour,libtcod.BKGND_SET) libtcod.console_set_default_foreground(MainConsole,terraintile.fgcolour) libtcod.console_put_char(MainConsole, x, y, terraintile.char, libtcod.BKGND_NONE) else: libtcod.console_set_char_background(MainConsole,x,y,terraintile.bgcolour*UNSEEN_TERRAIN_DARKNESS,libtcod.BKGND_SET) libtcod.console_set_default_foreground(MainConsole,terraintile.fgcolour*UNSEEN_TERRAIN_DARKNESS) libtcod.console_put_char(MainConsole, x, y, terraintile.char, libtcod.BKGND_NONE) for ent in ActiveEntityList: ent.Draw() DrawAllPaths(MainConsole) RenderGui() libtcod.console_blit(MainConsole, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0) libtcod.console_blit(GUIBottomConsole, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, GUI_BOTTOM_Y)
def render_all(self, target): if target is not None: global color_light_wall global color_light_ground for y in xrange(self.map.height): for x in xrange(self.map.width): visible = libtcod.map_is_in_fov(self.hero.fov_map, x, y) wall = self.map.tiles[x][y].block_sight if visible: if wall: libtcod.console_set_char_background(target, x, y, color_light_wall, libtcod.BKGND_SET) else: libtcod.console_set_char_background(target, x, y, color_light_ground, libtcod.BKGND_SET) self.map.tiles[x][y].explored = True else: if self.map.tiles[x][y].explored: if wall: libtcod.console_set_char_background(target, x, y, color_dark_wall, libtcod.BKGND_SET) else: libtcod.console_set_char_background(target, x, y, color_dark_ground, libtcod.BKGND_SET ) for o in self.npcs: if libtcod.map_is_in_fov(self.hero.fov_map, o.x, o.y): o.draw(target) self.hero.draw(target) libtcod.console_blit(target, 0, 0, SCREEN_WIDE, SCREEN_HIGH, 0, 0, 0)
def menu(header, options, width): if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options.') header_height = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header) if header == '': header_height = 0 height = len(options) + header_height window = libtcod.console_new(width, height) 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 x = SCREEN_WIDTH / 2 - width/2 y = SCREEN_HEIGHT / 2 - height/2 libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7) libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) if key.vk == libtcod.KEY_ENTER and key.lalt: #(special case) Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) index = key.c - ord('a') if index >= 0 and index < len(options): return index return None
def build(self,con): #build the menu itself libtcod.console_print_ex(con,self.width/2,0,libtcod.BKGND_NONE,libtcod.CENTER,self.name) i=1 temp=libtcod.console_new(self.width,self.height) for elem in self.content: #print elem #print "built" dh=elem.build(temp) dh=elem.height #updated from dh libtcod.console_blit(temp,0,0,elem.width,dh,con,1,i) elem.set_pos(1,i) # print 'elem placed at', elem.pos libtcod.console_clear(temp) libtcod.console_put_char_ex(con,0,i,chr(26),libtcod.white,libtcod.black) # libtcod.console_set_char_background(con, 0, i, libtcod.blue) i+=dh #---------------7Drl---------------------------------- if self.height<i+1: #Bricolage, to change later on self.height=i+1
def showDebugScreen(self): # store the current view behind_window = libtcod.console_new(SCREEN_WIDTH, SCREEN_HEIGHT) libtcod.console_blit(0, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, behind_window, 0, 0, 1.0, 1.0) # show the background image, at twice the regular console resolution img = libtcod.image_load("./media/menu_debug.png") libtcod.image_blit_2x(img, 0, 0, 0) while not libtcod.console_is_window_closed(): # show options and wait for the player's choice choice = self.showMenu( "Select debug option:", ["Run some test code!", "Show me some game stuff!", "Back"], # Choice 0 # Choice 1 # Choice 2 36, ) # interpret choice if choice is None: continue if choice == 0: print "Running some test code!" self.runTestCode() self.showMessage("Test code complete!", "There might be some output in the console...", 36) continue elif choice == 1: print "Showing some game stuff!" self.newGame() self.showGameScreen() elif choice == 2: # quit print "Back" break # Clean up (restore whatever was behind this window) libtcod.console_blit(behind_window, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0, 1.0, 1.0) libtcod.console_flush()
def render_scene(): for layer in VIEW_SCENE.values(): for view in layer: tcod.console_blit(view['console'], 0, 0, view['draw_size'][0], view['draw_size'][1], 0, view['position'][0], view['position'][1], view['fade'][0], view['fade'][1])
def menu(header, options, width): if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options.') #calculate the total height for the header and one line per option header_height = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header) if header == '': header_height = 0 height = len(options) + header_height #create an off-screen console that represent the menu's window window = libtcod.console_new(width, height) #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) #print all the option 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 the contents of 'window' to the root console x = SCREEN_WIDTH / 2 - width / 2 y = SCREEN_HEIGHT / 2 - height / 2 libtcod.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 libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) #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 draw(self): def sorter(val): k,v = val #if (ord(k) < (65+26) and ord(k) >= 65): #this code makes capital and lowercase appear in inconsistent orders in the list... # k=chr(ord(k)+32) #offset CAPS chars to make capital and lowercase equal (with capital coming second) return k y=0 # draw title and box misc.rectangle(self.con, 0,0, self.w,self.h, 0) title="<{}>".format(self.name) tx=math.floor( (self.w - len(title)) /2) libtcod.console_print(self.con, tx, 0, title) # draw options lis=list(self.keysItems.items()) lis.sort(key=sorter) for key,item in lis: name=self.get_name(item) libtcod.console_print( self.con, 1, y + 1, '({i}) {nm}'.format(i=key, nm=name) ) y += 1 libtcod.console_blit( self.con, 0,0, self.w, self.h, 0, self.x, self.y ) libtcod.console_flush()