def draw_obj(self, con, x, y, obj): # set the color and then draw the character that represents this object at its position if x is not None: # set the color and then draw the character that represents this object at its position libtcod.console_set_default_foreground(con, obj.color) libtcod.console_put_char(con, x, y, obj.char, libtcod.BKGND_NONE)
def main_menu(): img = libtcod.image_load('menu_background.png') while not libtcod.console_is_window_closed(): # show the background image, at twice the regular console # resolution libtcod.image_blit_2x(img, 0, 0, 0) # show the game's title, and some credits! libtcod.console_set_default_foreground(0, libtcod.light_yellow) libtcod.console_print_ex(0, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 - 4, libtcod.BKGND_NONE, libtcod.CENTER, 'TOMBS OF THE ANCIENT KINGS') libtcod.console_print_ex(0, SCREEN_WIDTH / 2, SCREEN_HEIGHT - 2, libtcod.BKGND_NONE, libtcod.CENTER, 'By Jotaf') # show options and wait for the player's choice choice = menu('', ['Play a new game', 'Continue last game', 'Quit'], 24) if choice == 0: # new game new_game() play_game() if choice == 1: # load last game try: load_game() except: msgbox('\n No saved game to load.\n', 24) continue play_game() elif choice == 2: # quit break
def draw_tile(self, x, y, tile, in_fov): con = self.map_console tile_disp = TILE_DISPS[tile.tile_type] if in_fov: tile.explored = True color_mult = 1.0 if in_fov else 0.5 bg = tile_disp.bg * color_mult if tile_disp.bg else None fg = tile_disp.fg * color_mult if tile_disp.fg else None if tile.explored: if tile_disp.char == ' ': libtcod.console_set_char_background(con, x, y, bg, libtcod.BKGND_SET) libtcod.console_set_char(con, x, y, ' ') else: libtcod.console_set_char_background(con, x, y, bg, libtcod.BKGND_SET) libtcod.console_set_default_foreground(con, fg) libtcod.console_put_char(con, x, y, tile_disp.char, libtcod.BKGND_NONE) else: libtcod.console_set_char_background(con, x, y, libtcod.black, libtcod.BKGND_SET) libtcod.console_set_char(con, x, y, ' ')
def setUp(self, screenWidth, screenHeight, FPS, fgcol): libtcod.console_set_custom_font(self.fontImageFileName, libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_ASCII_INROW) libtcod.console_init_root(screenWidth, screenHeight, 'List-editor', False) libtcod.sys_set_fps(FPS) libtcod.console_set_background_flag(0, libtcod.BKGND_SET) libtcod.console_set_default_foreground(0, fgcol)
def draw(self): # only show if it's visible to the player; or it's set to # "always visible" and on an explored tile if (libtcod.map_is_in_fov(fov_map, self.x, self.y) or (self.always_visible and map[self.x][self.y].explored)): # set the color and then draw the character that represents # this object at its position libtcod.console_set_default_foreground(con, self.color) libtcod.console_put_char(con, self.x, self.y, self.char, libtcod.BKGND_NONE)
def draw_tile(self, tile, x, y, in_fov): bk_color = tile.bk_color fg_color = tile.fg_color if in_fov: bk_color = bk_color * 2 fg_color = fg_color * 2 libtcod.console_set_char_background( self.con, x, y, bk_color, libtcod.BKGND_SET) libtcod.console_set_default_foreground(self.con, fg_color) libtcod.console_put_char( self.con, x, y, tile.char, libtcod.BKGND_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 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 # 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 = 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) # (special case) Alt+Enter: toggle fullscreen 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 render_bar(x, y, total_width, name, value, maximum, bar_color, back_color): # render a bar (HP, experience, etc). first calculate the width # of the bar bar_width = int(float(value) / maximum * total_width) # render the background first libtcod.console_set_default_background(panel, back_color) libtcod.console_rect(panel, x, y, total_width, 1, False, libtcod.BKGND_SCREEN) # now render the bar on top libtcod.console_set_default_background(panel, bar_color) if bar_width > 0: libtcod.console_rect(panel, x, y, bar_width, 1, False, libtcod.BKGND_SCREEN) # finally, some centered text with the values libtcod.console_set_default_foreground(panel, libtcod.white) libtcod.console_print_ex(panel, x + total_width / 2, y, libtcod.BKGND_NONE, libtcod.CENTER, name + ': ' + str(value) + '/' + str(maximum))
def init_libtcod(): startup_msg = ("analyzing air quality...", "calculating primordial soup..." ,"reading the future...", "carbon dating your hard drive..." ,"finding prime numbers...") print(random.choice(startup_msg)) libtcod.console_set_custom_font('data/fonts/terminal12x12_gs_ro.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_ASCII_INROW) libtcod.console_init_root(C.SCREEN_WIDTH, C.SCREEN_HEIGHT, 'top dog -- v%s' % (C.VERSION), C.FULLSCREEN) libtcod.sys_set_fps(C.LIMIT_FPS) # default font color libtcod.console_set_default_foreground(0, libtcod.white) # set color control codes for inline string formatting # listed by priority: think defcon levels # high alert, priority one libtcod.console_set_color_control(libtcod.COLCTRL_1 ,libtcod.light_red ,libtcod.black) # warning, danger will robinson libtcod.console_set_color_control(libtcod.COLCTRL_2 ,libtcod.light_yellow ,libtcod.black) # informational, you got a quest item libtcod.console_set_color_control(libtcod.COLCTRL_3 ,libtcod.light_green ,libtcod.black) # tile and npc names libtcod.console_set_color_control(C.COL4 ,libtcod.light_azure ,libtcod.black) # all other words libtcod.console_set_color_control(libtcod.COLCTRL_5 ,libtcod.white ,libtcod.black) return libtcod.console_new(C.MAP_WIDTH, C.MAP_HEIGHT)
def draw(self): if not self.is_in_fov(): return libtcod.console_set_default_foreground(self.__con, self.color) libtcod.console_put_char(self.__con, self.__x, self.__y, self.char, libtcod.BKGND_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, MAP_WIDTH, MAP_HEIGHT, 0, 0, 0) # prepare to render the GUI panel libtcod.console_set_default_background(panel, libtcod.black) libtcod.console_clear(panel) # 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 # 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) libtcod.console_print_ex(panel, 1, 3, libtcod.BKGND_NONE, libtcod.LEFT, 'Dungeon level ' + str(dungeon_level)) # display names of objects under the mouse 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 contents of "panel" to the root console libtcod.console_blit(panel, 0, 0, SCREEN_WIDTH, PANEL_HEIGHT, 0, 0, PANEL_Y)
libtcod.line(rect.x1, rect.y1, rect.x1, rect.y2, listener) libtcod.line(rect.x2, rect.y1, rect.x2, rect.y2, listener) libtcod.line(rect.x1, rect.y2, rect.x2, rect.y2, listener) if tree.left is not None: trace_sections(tree.left) if tree.right is not None: trace_sections(tree.right) trace_sections(tree) libtcod.console_set_custom_font( 'terminal12x12_gs_ro.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_ASCII_INROW) libtcod.console_init_root( SCREEN_WIDTH, SCREEN_HEIGHT, 'rogue-one', False, libtcod.RENDERER_GLSL) for x in range(SCREEN_WIDTH): for y in range(SCREEN_HEIGHT): col = libtcod.Color(60 * map[x][y], 60 * map[x][y], 60 * map[x][y]) libtcod.console_set_char_background(0, x, y, col) libtcod.console_set_default_foreground(0, libtcod.dark_grey) libtcod.console_put_char(0, x, y, ".") libtcod.console_flush() libtcod.console_wait_for_keypress(True) exit()
tiler = TileGenerator() tiler.run(world, map) player = Entity(27, 22, "@", libtcod.white, con, world) npc = Entity(56, 27, "J", libtcod.yellow, con, world) objects = [npc, player] fov_map = libtcod.map_new(MAP_WIDTH, MAP_HEIGHT) for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): libtcod.map_set_properties(fov_map, x, y, not world[x][y].block_sight, not world[x][y].blocked) fov_recompute = True while not libtcod.console_is_window_closed(): libtcod.console_set_default_foreground(0, libtcod.white) if fov_recompute: fov_recompute = False libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) render_all(fov_map) libtcod.console_flush() for object in objects: object.clear() if handle_keys(): break
def draw(self): libtcod.console_set_default_foreground(self.con, self.color) libtcod.console_put_char(self.con, self.x, self.y, self.char, libtcod.BKGND_NONE)