def blit_dialogues(): """ Draw dialogues onto the screen. """ if len(player.dialogues) > 0: libtcod.console_clear(0) dlg = player.dialogues[-1] if dlg.npc_picture: icon = libtcod.image_load(os.path.join('data', 'images', dlg.npc_picture)) else: icon = libtcod.image_load(os.path.join('data', 'images', 'icon-%s.png' % (dlg.npc_name))) frame = libtcod.image_load(os.path.join('data', 'images', 'dialogue-frame.png')) libtcod.image_blit_rect(frame, 0, 0, 0, -1, -1, libtcod.BKGND_SET) libtcod.image_blit_rect(icon, 0, C.MAP_LEFT, C.MAP_TOP, -1, -1, libtcod.BKGND_SET) # title libtcod.console_print_ex(0, 2 + (C.MAP_WIDTH / 2), 2, libtcod.BKGND_NONE, libtcod.CENTER, "%c%s says:%c" % (C.COL4, dlg.npc_name, C.COLS)) # # the message # libtcod.console_print_ex(0, 2 + (C.MAP_WIDTH / 2), C.MAP_TOP + 4, # libtcod.BKGND_NONE, libtcod.CENTER, # "\"%c%s%c\"" % (C.COL5, dlg.dialogue, C.COLS)) try: libtcod.console_print_rect( 0, 4, 6, C.MAP_WIDTH - 4, C.MAP_HEIGHT - 2, "%s" % (dlg.dialogue)) except e: print('dialogue string format error: %s' % (e)) # press space libtcod.console_print_ex(0, 2 + (C.MAP_WIDTH / 2), C.SCREEN_HEIGHT - 1, libtcod.BKGND_NONE, libtcod.CENTER, "(spacebar or enter...)")
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 blit_about(): libtcod.console_clear(0) icon = libtcod.image_load(os.path.join('data', 'images', 'about-frame.png')) libtcod.image_blit_rect(icon, 0, 0, 0, -1, -1, libtcod.BKGND_SET) try: readme = file('README', 'r') except IOError, e: libtcod.console_print_ex(0, 2, 2, libtcod.BKGND_NONE, libtcod.LEFT, "Error: about file not found :'(") return None
def blit_lost(): libtcod.console_clear(0) frame = libtcod.image_load(os.path.join('data', 'images', 'dialogue-frame.png')) libtcod.image_blit_rect(frame, 0, 0, 0, -1, -1, libtcod.BKGND_SET) icon = libtcod.image_load(os.path.join('data', 'images', 'icon-paw.png')) libtcod.image_blit_rect(icon, 0, C.MAP_LEFT, C.MAP_TOP, -1, -1, libtcod.BKGND_SET) libtcod.console_print_ex(0, C.SCREEN_WIDTH / 2, 4, libtcod.BKGND_NONE, libtcod.CENTER, \ "Ouch! You lost all your health.\n\n" \ "You rest to retry this level.\n\n" \ "(press space to try again)")
def draw_messages(): """ Display the last x messages in-game. """ if player.messages: # move messages with our player y = 9 if player.y <= 18: y = C.MAP_HEIGHT - 6 libtcod.console_print_ex(0 , 3, y ,libtcod.BKGND_NONE, libtcod.LEFT ,"\n".join(player.messages))
def blit_victory(): libtcod.console_clear(0) frame = libtcod.image_load(os.path.join('data', 'images', 'about-frame.png')) libtcod.image_blit_rect(frame, 0, 0, 0, -1, -1, libtcod.BKGND_SET) results = ["You won, Top Dog!"] results.append("You moved %s times." % (player.moves)) results.append("You drank %s puddles." % (player.quenches)) results.append("You took %s bites." % (player.bites_taken)) results.append("You ate %s treats." % (player.treats_eaten)) results.append("You piddled %s times." % (player.piddles_taken)) results.append("Your score is %s!" % (player.score)) results.append("Well Done ^_^") libtcod.console_print_ex(0, C.SCREEN_WIDTH / 2, 4, libtcod.BKGND_NONE, libtcod.CENTER, "\n\n".join(results))
def blit_menu(): libtcod.console_clear(0) icon = libtcod.image_load(os.path.join('data', 'images', 'intro.png')) libtcod.image_blit_rect(icon, 0, 0, 0, -1, -1, libtcod.BKGND_SET) text = [ "version %s" % (C.VERSION) ,"%cA%cbout" % (C.COL1, C.COLS) ,"%cspace%c to continue" % (C.COL4, C.COLS) ] libtcod.console_print_ex(0, 2, 45, libtcod.BKGND_NONE, libtcod.LEFT, "\n".join(text)) libtcod.console_print_ex(0, C.SCREEN_WIDTH / 2, 24, libtcod.BKGND_NONE, libtcod.CENTER, "in 'The Lost Puppy'")
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 blit_help(): """ Show help. """ libtcod.console_clear(0) icon = libtcod.image_load(os.path.join('data', 'images', 'stats-frame.png')) libtcod.image_blit_rect(icon, 0, C.MAP_LEFT, C.MAP_TOP, -1, -1, libtcod.BKGND_SET) libtcod.console_print_ex(0, C.SCREEN_WIDTH / 2, 2, libtcod.BKGND_NONE, libtcod.CENTER, "%cTop Dog%c\nv%s\n^_^" % (C.COL5, C.COLS, C.VERSION)) # helptext = ["%c%s%s" % (C.COL5, C.COLS, C.VERSION)] helptext = ["The %cPuppy%c has been kidnapped by the %cFat Cat Mafioso%c. You travel from yard to yard, searching for the crafty Cats!" % (C.COL4, C.COLS, C.COL1, C.COLS)] helptext.append("\nYou are the %c@%c sign. Walk into other animals to interact with them." % (C.COL3, C.COLS)) helptext.append("\n%cKEYPAD%c" % (C.COL5, C.COLS)) helptext.append("\nUse the %cKeypad%c to move, this is preferred as \ diagonals are the dog's bark. Keypad 5 shows your stats, as does [i]nfo. The %cARROW%c keys also move you." \ % (C.COL4, C.COLS, C.COL4, C.COLS)) helptext.append("\n%cACTIONS%c" % (C.COL5, C.COLS)) helptext.append("\n[%cd%c]rink water" % (C.COL5, C.COLS)) helptext.append("[%ce%c]at food" % (C.COL5, C.COLS)) helptext.append("[%cp%c]piddle to relieve yourself" % (C.COL5, C.COLS)) helptext.append("[%ci%c]nfo screen: stats and quests" % (C.COL5, C.COLS)) helptext.append("\nThe keypad also map to actions, use this mnemonic to remember:") helptext.append("\n%cD%crink and %cD%civide\n%cE%cat and %cM%cultiply\n%cP%ciddling %cS%coothes ;)" % (C.COL1, C.COLS, C.COL1, C.COLS , C.COL2, C.COLS, C.COL2, C.COLS , C.COL3, C.COLS, C.COL3, C.COLS)) helptext.append("\nNow go find that %cPuppy!%c" % (C.COL5, C.COLS)) helptext.append("\nWOOF!") libtcod.console_print_rect(0, 4, 10, C.MAP_WIDTH - 4, C.MAP_HEIGHT - 2, "\n".join(helptext))
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)
def draw_player_stats(): """ Print player info and stats in the side panel. """ tile = object_at(player.x, player.y) if not tile: tile = game_map[player.x][player.y] # the object/tile name player is standing on libtcod.console_print_ex(0, 2 + (C.MAP_WIDTH / 2), C.SCREEN_HEIGHT - 2, libtcod.BKGND_NONE, libtcod.CENTER, "%c%s%c" % (C.COL5, tile.name, C.COLS)) # player hearts if player.weak: heart_colors = [libtcod.red]* 10 else: heart_colors = (libtcod.red, libtcod.red, libtcod.orange, libtcod.orange , libtcod.amber, libtcod.amber, libtcod.lime, libtcod.lime , libtcod.chartreuse, libtcod.chartreuse) for heart in range(player.get_hearts()): libtcod.console_put_char_ex( 0, heart + C.STAT_HEART_LEFT, C.STAT_HEART_TOP ,chr(3), heart_colors[heart], None) texts = [ "level: %s" % (player.level) ,"score: %s" % (player.score) ,player.inventory_name(prefix="carrying: ") ] if len(player.quests) > 0: texts.append("+ %s" % (player.quests[-1].title)) # level, score, inventory libtcod.console_print_ex( 0, C.STAT_HEART_LEFT, 1 ,libtcod.BKGND_NONE, libtcod.LEFT ,"\n".join(texts) ) # player health status if player.weak: libtcod.console_print_ex(0, C.MAP_WIDTH, C.STAT_HEART_TOP ,libtcod.BKGND_NONE, libtcod.RIGHT ,"%c*weakness*%c" % (C.COL1, C.COLS)) elif player.thirsty: libtcod.console_print_ex(0, C.MAP_WIDTH, C.STAT_HEART_TOP ,libtcod.BKGND_NONE, libtcod.RIGHT ,"%c*thirstys*%c" % (C.COL2, C.COLS)) elif player.hungry: libtcod.console_print_ex(0, C.MAP_WIDTH, C.STAT_HEART_TOP ,libtcod.BKGND_NONE, libtcod.RIGHT ,"%c*hungrys*%c" % (C.COL2, C.COLS)) elif player.mustpiddle: libtcod.console_print_ex(0, C.MAP_WIDTH, C.STAT_HEART_TOP ,libtcod.BKGND_NONE, libtcod.RIGHT ,"%c*piddles*%c" % (C.COL2, C.COLS))
def blit_player_stats(): """ Draw player stats and quests screen. """ libtcod.console_clear(0) icon = libtcod.image_load(os.path.join('data', 'images', 'stats-frame.png')) libtcod.image_blit_rect(icon, 0, C.MAP_LEFT, C.MAP_TOP, -1, -1, libtcod.BKGND_SET) if player.carrying: if player.carrying.quest_id: inv_item = "%c%s%c\n%c*quest item*%c" % \ (C.COL3, player.carrying.name, C.COLS, C.COL4, C.COLS) else: inv_item = "%c%s%c" % (C.COL3, player.carrying.name, C.COLS) else: inv_item = "" labels = ( "" ,"" ,"%clevel%c:" % (C.COL5, C.COLS) ,"%cscore%c:" % (C.COL5, C.COLS) ,"%cmoves%c:" % (C.COL5, C.COLS) ,"%cinventory%c:" % (C.COL5, C.COLS) ) values = [ "%cTop Dog%c" % (C.COL5, C.COLS) ,"" ,str(player.level) ,str(player.score) ,str(player.moves) ,inv_item ] # name, score, inventory libtcod.console_print_ex(0, C.STATS_SCREEN_LEFT, C.STATS_SCREEN_TOP, libtcod.BKGND_NONE, libtcod.RIGHT, "\n".join(labels)) libtcod.console_print_ex(0, C.STATS_SCREEN_LEFT + 2, C.STATS_SCREEN_TOP, libtcod.BKGND_NONE, libtcod.LEFT, "\n".join(values)) # quests values = [] if len(player.quests) > 0: values = ["%cQUESTS%c\n" % (C.COL5, C.COLS)] for q in player.quests: values.append("+ %s" % (q.title)) # hungry, thirsty, piddle, inventory if player.weak: values.append("+ %cweak%c, [e]at food" % (C.COL1, C.COLS)) if player.hungry: values.append("+ %chungry%c, [e]at *food*" % (C.COL2, C.COLS)) if player.thirsty: values.append("+ %cthirsty%c, [d]rink water" % (C.COL2, C.COLS)) libtcod.console_print_ex(0, 4, C.SCREEN_HEIGHT / 2, libtcod.BKGND_NONE, libtcod.LEFT, "\n".join(values)) # player hearts if player.weak: heart_colors = [libtcod.red]* 10 else: heart_colors = (libtcod.red, libtcod.red, libtcod.orange, libtcod.orange , libtcod.amber, libtcod.amber, libtcod.lime, libtcod.lime , libtcod.chartreuse, libtcod.chartreuse) for heart in range(player.get_hearts()): libtcod.console_put_char_ex( 0, heart + C.STAT_HEART_LEFT, C.STAT_HEART_TOP ,chr(3), heart_colors[heart], None)
icon = libtcod.image_load(os.path.join('data', 'images', 'about-frame.png')) libtcod.image_blit_rect(icon, 0, 0, 0, -1, -1, libtcod.BKGND_SET) try: readme = file('README', 'r') except IOError, e: libtcod.console_print_ex(0, 2, 2, libtcod.BKGND_NONE, libtcod.LEFT, "Error: about file not found :'(") return None readme_text = readme.read() readme.close() libtcod.console_print_rect(0, 2, 2, C.MAP_WIDTH - 3, C.MAP_HEIGHT - 4, readme_text) libtcod.console_print_ex(0, 2, 47, libtcod.BKGND_NONE, libtcod.LEFT, "%cspace%c to return" % (C.COL4, C.COLS)) def draw_map(): """ Draw the map tiles onto the canvas. """ for y in range(C.MAP_HEIGHT - 0): for x in range(C.MAP_WIDTH - 0): tile = game_map[x][y] if player.wizard or libtcod.map_is_in_fov(fov_map, x, y): tile.seen = True libtcod.console_put_char_ex(canvas, x, y, tile.char, tile.fgcolor, tile.bgcolor) elif tile.seen: