def start_new_round(self, winner_of_last_round=None): """starts a new round of pong says who won, shows a countdown, moves the ball to the center, resets the players at the end, wipes the conway sim and resumes play """ # lets do stuff in an off-screen console, so we can use transparency # so the player can see the map con = tcod.console_new(self.width, self.height) time_elapsed = 0.0 # in seconds x = self.width / 2 tcod.console_set_alignment(con, tcod.CENTER) while self.alive and not tcod.console_is_window_closed() and \ time_elapsed < 3.0: y = self.height / 2 if winner_of_last_round: tcod.console_set_default_foreground(con, winner_of_last_round.color) player_num = self.players.index(winner_of_last_round) + 1 string = "Player %d scores!" % player_num height = tcod.console_get_height_rect(con, x, y, self.width, self.height, string) tcod.console_print_rect(con, x, y, self.width, height, string) y += height tcod.console_set_default_foreground(con, tcod.white) string = "New round starting in %d seconds..." % int(3 - time_elapsed) height = tcod.console_get_height_rect(con, x, y, self.width, self.height, string) tcod.console_print_rect(con, x, y, self.width, height, string) self.handle_input() self.update_conway() self.render_all() tcod.console_blit(con, 0, 0, 0, 0, 0, 0, 0, 1.0, 0.75) tcod.console_flush() time_elapsed += tcod.sys_get_last_frame_length() # delete tcod console we created tcod.console_delete(con) #reset the ball self.ball = Ball(1, 1) #reset the player positions for player in self.players: player.top = self.height / 2 # wipe the conway simulation (by creating a new one) self.init_map(self.conway.size, self.conway.color)
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 menu(header, options, width): #generic menu function. has automatic height calculation if len(options) > CARRY_LIMIT: raise ValueError('Cannot have a menu with more than' + str(CARRY_LIMIT) + 'options.') #calculates total height, one line per option header_height = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header) height = len(options) + header_height #creates an off-screen console, menu window window = libtcod.console_new(width, height) #prints header, words wrapped libtcod.console_set_default_foreground(window, libtcod.white) libtcod.console_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, header) #prints options y = header_height letter_index = ord('a') #gets ascii code from a character 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 #blits content of window console to root console/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) #shows root console 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.') 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 options_text in options: text = '(' + chr(letter_index) + ') ' + options_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) #wait for keypress before continuing libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) #convert ASCII code to index and return if it corresponds to an option 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 auto-wrap) WITH 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) if key.vk == libtcod.KEY_ENTER and key.lalt: #(special case) Alt+Enter: toggle fullscreen 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): max_menu_options = 26 if len(options) > max_menu_options: raise ValueError( 'Cannot have a menu with more than {0} options'.format( max_menu_options) ) #this limits the number of total options in a given menu 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 = 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') # 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 get_height_rect(self, x=0, y=0, width=None, height=None, text=''): if width is None: width = self.width if height is None: height = self.height return libtcod.console_get_height_rect(self.console_id, x, y, width, height, text)
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 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 menu(con, header, options, width, screen_width, screen_height): if len(options) > 26: raise ValueError('cannot have a screen with more than 26 options') # calculate total header height 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) # 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 contents to 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 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 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 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 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 test_console_printing(console, fg, bg): libtcodpy.console_set_background_flag(console, libtcodpy.BKGND_SET) assert (libtcodpy.console_get_background_flag(console) == libtcodpy.BKGND_SET) libtcodpy.console_set_alignment(console, libtcodpy.LEFT) assert (libtcodpy.console_get_alignment(console) == libtcodpy.LEFT) libtcodpy.console_print(console, 0, 0, 'print') libtcodpy.console_print_ex(console, 0, 0, libtcodpy.BKGND_SET, libtcodpy.LEFT, 'print ex') assert (libtcodpy.console_print_rect( console, 0, 0, 8, 8, 'print rect') > 0 ) assert (libtcodpy.console_print_rect_ex( console, 0, 0, 8, 8, libtcodpy.BKGND_SET, libtcodpy.LEFT, 'print rect ex') > 0 ) assert (libtcodpy.console_get_height_rect( console, 0, 0, 8, 8, 'get height') > 0 ) libtcodpy.console_set_color_control(libtcodpy.COLCTRL_1, fg, bg)
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): #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 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 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): #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("Can't have a menu with more than 26 options.") #Calculate the word wrapped height of the header and the total window height header_height = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header) height = len(options) + header_height #Create a new window for the menu 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 to the middle of the main 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)
def menu(header, options, width): if len(options) > 26: raise ValueError('Cannot have a menu with over 26 options.') header_height = libtcod.console_get_height_rect(view, 0, 0, width, const.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 = const.SCREEN_WIDTH / 2 - width / 2 y = const.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') header_height = libtcodpy.console_get_height_rect(0, 0, 0, config.window_width, config.window_height, header) if header == '': header_height = 0 height = len(options) + header_height menupanel = Panel(0, 0, width, height) menupanel.write_wrap_ex(0, 0, width, height, header, libtcodpy.LEFT) y = header_height letter_index = ord('a') for option_text in options: text = '(' + chr(letter_index) + ') ' + option_text menupanel.write(0, y, text) y += 1 letter_index += 1 x = config.window_width / 2 - width / 2 y = config.window_height / 2 - height / 2 menupanel.blit(xdst=x, ydst=y, bfade=0.7) config.gamewindow.flush key = libtcodpy.console_wait_for_keypress(True) if key.vk == libtcodpy.KEY_ENTER and key.lalt: libtcodpy.console_set_fullscreen(not libtcodpy.console_is_fullscreen()) 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 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 choice_menu(header, options, width): if len(options) > 26: raise ValueError(u"Cannot have a menu with more than 26 options.") header_hight = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header) height = len(options) + header_hight menu_console = libtcod.console_new(width, height) libtcod.console_set_default_foreground(menu_console, libtcod.white) libtcod.console_print_rect_ex(menu_console, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, header) # Print options. y = header_hight letter_index = ord(u"a") for option_text in options: text = "(%s) %s" % (chr(letter_index), option_text) libtcod.console_print(menu_console, 0, y, text) y += 1 letter_index += 1 x = SCREEN_WIDTH / 2 - width / 2 y = SCREEN_HEIGHT / 2 - height / 2 libtcod.console_blit(menu_console, 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(u"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 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 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) 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)
def get_height_rect(self, x=0, y=0, width=None, height=None, text=''): if text is None: raise ValueError("Trying to console.get_height_rect of a None!") if width is None: width = self.width - x if height is None: height = self.height - y return libtcod.console_get_height_rect(self.console_id, x, y, width, height, text)
def build(self, con): libtcod.console_set_default_background(con, self.bkg_color) libtcod.console_set_default_foreground(con, self.color) libtcod.console_print_rect_ex(con, 0, 0, self.width, 0, libtcod.BKGND_NONE, libtcod.LEFT, self.entry) self.height = libtcod.console_get_height_rect(con, 0, 0, self.width, 0, self.entry)
def menu(header, options, width): """ Generic menu builder. Defines a list of options to the player. """ global con global key if len(options) > 26: # Make sure we don't get carried away with the menu. 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 not len(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 = '({}) {}'.format(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 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) # Present the mains screen to the player and wait for a key-press libtcod.console_flush() # Watch out for key presses and return the options index. 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 pop_up(width=None, height=None, title=None, text=None): mouse = Input.mouse key = Input.key # calculate total height for the header (after auto-wrap) and one line per option if width is None: width = Constants.MAP_CONSOLE_WIDTH - 30 if height is None: height = libtcod.console_get_height_rect( 0, 0, 0, width, Constants.SCREEN_HEIGHT, text) + 7 pop = libtcod.console_new(width, height) # print the header, with auto-wrap libtcod.console_set_default_foreground(pop, Constants.UI_PopFore) libtcod.console_set_default_background(pop, Constants.UI_PopBack) libtcod.console_print_frame(pop, 0, 0, width, height, clear=True, flag=libtcod.BKGND_SET, fmt=title) Render.print_rect(pop, 3, 3, width - 6, height, text) # blit the contents of "window" to the root console x = Constants.MAP_CONSOLE_WIDTH / 2 - width / 2 y = Constants.MAP_CONSOLE_HEIGHT / 2 - height / 2 button_text = 'Click to Continue' button = Button(button_text, width / 2, height - 3, function=close_window, target=pop, length=len(button_text)) libtcod.console_blit(pop, 0, 0, width, height, 0, x, y, 1.0, .85) while True: libtcod.console_blit(pop, 0, 0, width, height, 0, x, y, 1.0, 0.0) libtcod.console_flush() libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if button.draw(x, y) == 'close': return if key.vk == libtcod.KEY_ENTER and key.lalt: # Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) if key.vk == libtcod.KEY_ESCAPE or key.vk == libtcod.KEY_ESCAPE or key.vk == libtcod.KEY_SPACE: return
def menu(header, options, width, options_params=None): 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(window, 0, 0, width, height, header) # print all the options y = header_height letter_index = ord('a') longest_text = 0 for option_text in options: text = '(' + chr(letter_index) + ') ' + option_text libtcod.console_print(window, 0, y, text) y += 1 letter_index += 1 if len(text) > longest_text: longest_text = len(text) # draw a line between the letter and the option libtcod.console_vline(window, 3, header_height, height, libtcod.BKGND_LIGHTEN) if options_params: y = header_height last_char_x = longest_text # draw a line between the letter and the option libtcod.console_vline(window, last_char_x, header_height, height, libtcod.BKGND_LIGHTEN) for param in options_params: libtcod.console_print(window, last_char_x+1, y, str(param)) y += 1 if len(str(param)) > longest_text: longest_text = len(str(param)) # 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 libtcod.console_flush()
def multi_objects_menu(header, options, width): #The player is presented with some options and makes a choice based on graphics choice = 0 new_choice = 0 selection = [] #Calculate total height for header (after auto-wrap) and one line per option header_height = libtcod.console_get_height_rect(render.mapcon, 0, 0, width, SCREEN_HEIGHT, header) height = len(options) + header_height #Create the virtual console to write the menu on window = libtcod.console_new(width, height) while True: #Clear the console ready to draw libtcod.console_clear(window) #Draw the 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) #Iterate through and print the options, highlighting the current selection. y = header_height for index, option in enumerate(options): libtcod.console_set_default_foreground(window, libtcod.white) if index == choice: libtcod.console_set_default_foreground(window, MENU_HILIGHT) libtcod.console_print_ex(window, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, '>') if option in selection: libtcod.console_set_default_foreground(window, MENU_SELECTED) libtcod.console_print_ex(window, 1, y, libtcod.BKGND_NONE, libtcod.LEFT, option.name) y += 1 #Blit the window to the root and flush to render everything. libtcod.console_blit(window, 0, 0, width, height, 0, SCREEN_WIDTH/2 - width/2, SCREEN_HEIGHT/2 - height/2) libtcod.console_flush() libtcod.sys_wait_for_event(libtcod.EVENT_KEY_PRESS, key, mouse, True) if key.vk == libtcod.KEY_ENTER: return selection if key.vk == libtcod.KEY_SPACE: if options[choice] in selection: selection.remove(options[choice]) else: selection.append(options[choice]) if key.vk == libtcod.KEY_ESCAPE: return None #Up and down arrows change selection elif key.vk == libtcod.KEY_UP or key.vk == libtcod.KEY_KP8: new_choice = choice - 1 elif key.vk == libtcod.KEY_DOWN or key.vk == libtcod.KEY_KP2: new_choice = choice + 1 #Check that we're not selecting outside the boundary if 0 <= new_choice < len(options): choice = new_choice
def __init__(self, width=None, height=None, title='', text=''): self.width = width self.height = height self.title = title self.text = text if width is None: self.width = Constants.MAP_CONSOLE_WIDTH - 10 if height is None: self.height = libtcod.console_get_height_rect(0, 0, 0, width, Constants.SCREEN_HEIGHT, text) + 10 self.opened = False
def menu(header, options, width): """ Creates a menu with a header as the title at the top of the window, options is the list of strings to display, and height is formed from the header + the length of the word-wrapped options. """ #global end_credits 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 then one line per option header_height = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header) if header == '': header_height = 0 #otherwise there is a blank line on top of the menu if there's no 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 #ord() and chr() work together to convert between letters and ASCII codes 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) #Display the libtcod credits. # TODO: Put this in a separate console so that it can just run on its own without blocking # access to the menu. Make sure to kill that console if the user chooses an option so that # it doesnt keep running on top of whatever is next. # while not end_credits: # end_credits = libtcod.console_credits_render(5, 5, False) # libtcod.console_flush() # key = libtcod.console_check_for_keypress() # if key.vk is not libtcod.KEY_NONE: break #present the root console to the player and wait for a key-press libtcod.console_flush() sleep(0.4) # Need to debounce otherwise the menus are super irritating key = libtcod.console_wait_for_keypress(True) 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 an option, return the index. index = key.c - ord('a') # key.c is the ASCII code of the character that was pressed if index >= 0 and index < len(options): return index return None
def __init__(self, width=None, height=None, title='', text=''): self.width = width self.height = height self.title = title self.text = text if width is None: self.width = Constants.MAP_CONSOLE_WIDTH - 10 if height is None: self.height = libtcod.console_get_height_rect( 0, 0, 0, width, Constants.SCREEN_HEIGHT, text) + 10 self.opened = False
def text_input(header): width = len(header) header_height = libtcod.console_get_height_rect(g.con, 0, 0, width, SCREEN_HEIGHT, header) if header == '': header_height = 0 height = 1 + 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) #blit the contents of "window" to the root console x = SCREEN_WIDTH/2 - width/2 y = SCREEN_HEIGHT/6 - height/2 libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7) #Now, get key input repeatedly text = "" libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) while key.vk != libtcod.KEY_ENTER and key.vk != libtcod.KEY_ESCAPE: if key.vk == libtcod.KEY_BACKSPACE: if len(text) > 0: text = text[:-1] #Cut off the last one elif key.c >= 32 and key.c <= 127: key_char = chr(key.c) text += key_char #Redraw #render_all() 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) #libtcod.console_print_ex(window, 0, 1, libtcod.BKGND_NONE, libtcod.LEFT, 'Ravi') libtcod.console_print_ex(window, 0, 1, libtcod.BKGND_NONE, libtcod.LEFT, text) #libtcod.console_print_ex(panel, MSG_X, y, libtcod.BKGND_NONE, libtcod.LEFT,line) #blit the contents of "window" to the root console 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) return text
def display_inventory(source): # Set up indent space and header indent_space = ' ' header = 'Inventory vol: %s/%s weight:%s/%s' % (str( source.volume()), str(source.max_volume()), str( source.carry_weight()), str(source.max_carry_weight())) header_height = libtcod.console_get_height_rect(c.con, 0, 0, c.screen_width, c.screen_height, header) height = source.inventory_size() + header_height + len(c.order) + 1 width = 50 inventory_window = libtcod.console_new(width, height) libtcod.console_set_default_foreground(inventory_window, libtcod.white) libtcod.console_print_rect_ex(inventory_window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, header) libtcod.console_hline(inventory_window, 0, header_height, width) y = header_height + 1 category_index = 0 inventory = source.stats.inventory selector_val = 1 for category in inventory: if len(inventory[category_index]) > 0: libtcod.console_print_ex(inventory_window, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, c.order[category_index]) y += 1 for item in inventory[category_index]: current_item, amount = item.items()[0] name = item.name if amount > 1: name += " (%s)" % str(amount) selector = token_to_index(selector_val) option = '%s%s%s' % (selector, indent_space, name) libtcod.console_print_ex(inventory_window, 2, y, libtcod.BKGND_NONE, libtcod.LEFT, option) y += 1 selector_val += 1 category_index += 1 xdst = int((1.0 / 10) * c.screen_width) ydst = int((1.0 / 20) * c.screen_height) libtcod.console_blit(inventory_window, 0, 0, c.screen_width, c.screen_height, 0, xdst, ydst, 1.0, 0.7) libtcod.console_flush()
def menu(header, options, width): global key global mouse 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_set_alignment(window, libtcod.LEFT) libtcod.console_set_default_background(window, libtcod.BKGND_NONE) libtcod.console_print_rect(window, 0, 0, width, height, header) # Print all the options y = header_height letter_index = ord('a') for option_text in options: text = '({}) {}'.format(chr(letter_index), option_text) libtcod.console_print(window, 0, y, text) y += 1 letter_index += 1 # Blit the contents of "window" to the root console x = int(round(SCREEN_WIDTH / 2 - width / 2)) y = int(round(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() libtcod.sys_wait_for_event(libtcod.EVENT_KEY_PRESS, key, mouse, False) if key.vk == libtcod.KEY_ENTER and key.lalt: #(special case) Alt+Enter: toggle fullscreen 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(header, options, width, highlighted=[]): """Basic, general-purpose menu. Allows the user to choose from up to 26 text 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 if header == "": header_height = 0 else: header_height = tcod.console_get_height_rect(0, 0, 0, width, SCREEN_HEIGHT, header) height = len(options) + header_height # create an off-screen console that represents the menu's window window = tcod.console_new(width, height) # print the header, with auto-wrap tcod.console_set_default_foreground(window, tcod.white) tcod.console_print_rect(window, 0, 0, width, height, header) # print all the options y = header_height letter_index = ord("a") for option_text in options: text = "(" + chr(letter_index) + ") " + option_text tcod.console_print(window, 0, y, text) y += 1 letter_index += 1 for index in highlighted: w = len(options[index]) + 4 tcod.console_set_default_background(window, tcod.grey) y = index + header_height tcod.console_rect(window, 0, y, w, 1, False, flag=tcod.BKGND_SET) # blit the contents of "window" to the root console x = SCREEN_WIDTH // 2 - width // 2 y = SCREEN_HEIGHT // 2 - height // 2 tcod.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 tcod.console_flush() tcod.sys_wait_for_event(tcod.EVENT_KEY_PRESS, key, mouse, True) # special case: changing to/from fullscreen if key.vk == tcod.KEY_F11: tcod.console_set_fullscreen(not tcod.console_is_fullscreen()) elif key.vk == tcod.KEY_ESCAPE: return "escape" else: # 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(header, options, width, screen_width, screen_height, position=None, type=None): 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(0, 0, 0, width, screen_height, header) height = len(options) + header_height + 2 # create an offscreen console that represents the menu's window terminal.layer(2) terminal.clear_area(0, 0, screen_width, screen_height) if type == 'main': x = int(screen_width / 2 - 9) y = int(screen_height / 2 + 5 + height) elif type == 'inventory': x = int(screen_width / 2 + 25) y = int(screen_height / 2) else: x = int(screen_width / 2) y = int(screen_height / 2) #while True: terminal.print_(x + 1, y, '[color=white]' + header) # print all the options h = header_height letter_index = ord('a') run = 0 for option_text in options: text = option_text if position is not None: if run == position: terminal.print_(x + 1, h + y + 1, '[color=yellow]' + text) else: terminal.print_(x + 1, h + y + 1, '[color=white]' + text) else: terminal.print_(x + 1, h + y + 1, '[color=white]' + text) h += 1 letter_index += 1 run += 1 # present the root console to the player and wait for keypress terminal.refresh()
def pop_up(width=None, height=None, title=None, text=None): mouse = Input.mouse key = Input.key # calculate total height for the header (after auto-wrap) and one line per option if width is None: width = Constants.MAP_CONSOLE_WIDTH - 30 if height is None: height = libtcod.console_get_height_rect(0, 0, 0, width, Constants.SCREEN_HEIGHT, text) + 7 pop = libtcod.console_new(width, height) # print the header, with auto-wrap libtcod.console_set_default_foreground(pop, Constants.UI_PopFore) libtcod.console_set_default_background(pop, Constants.UI_PopBack) libtcod.console_print_frame(pop, 0, 0, width, height, clear=True, flag=libtcod.BKGND_SET, fmt=title) Render.print_rect(pop, 3, 3, width - 6, height, text) # blit the contents of "window" to the root console x = Constants.MAP_CONSOLE_WIDTH / 2 - width / 2 y = Constants.MAP_CONSOLE_HEIGHT / 2 - height / 2 button_text = 'Click to Continue' button = Button(button_text, width / 2, height - 3, function=close_window, target=pop, length=len(button_text)) libtcod.console_blit(pop, 0, 0, width, height, 0, x, y, 1.0, .85) while True: libtcod.console_blit(pop, 0, 0, width, height, 0, x, y, 1.0, 0.0) libtcod.console_flush() libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse) if button.draw(x, y) == 'close': return if key.vk == libtcod.KEY_ENTER and key.lalt: # Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) if key.vk == libtcod.KEY_ESCAPE or key.vk == libtcod.KEY_ESCAPE or key.vk == libtcod.KEY_SPACE: return
def __init__(self, header, options, x, y, width, parent, text_color = libtcod.white, selected_bkgnd = libtcod.red, unselected_bkgnd = libtcod.black): self.header = header self.parent = parent self.options = options self.selected_index = 0 self.x = x self.y = y self.width = width self.header_height = libtcod.console_get_height_rect(self.parent.panel, 0, 0, self.width, self.parent.screen_height, header) self.height = len(self.options) + self.header_height self.panel = self.parent.panel self.text_color = text_color self.selected_bkgnd = selected_bkgnd self.unselected_bkgnd = unselected_bkgnd
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) header_height = libtcod.console_get_height_rect(console.body, 0, 0, width, SCREEN_HEIGHT, header) if header == '': header_height = 0 height = len(options) + header_height # window = libtcod.console_new(width, height) window = Panel(0, 0, 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) window.set_default_foreground(libtcod.white) window.write_wrap_ex(0, 0, width, height, header, libtcod.LEFT) 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) window.write(0, y, 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) window.x = x window.y = y window.blit(bfade=0.7) # libtcod.console_flush() root.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 description_box(con, description, width, screen_width, screen_height, x, y, colors): """Show box at specific location with description of entity.""" height = libtcod.console_get_height_rect(con, 0, 0, width, screen_height, description) window = libtcod.console_new(width, height) libtcod.console_set_default_foreground(window, colors['text_default']) if description: libtcod.console_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, description) libtcod.console_blit(window, 0, 0, width, height, 0, x, y - height, 1.0, 0.7)
def menu(header, options, width): global key if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options.') #calculate total height for 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 #calculate 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.capitalize() libtcod.console_print_ex(window, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, text) y += 1 letter_index += 1 #blit the contents of window to 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) libtcod.console_flush() while True: #check for input in each iteration libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS|libtcod.EVENT_MOUSE, key, mouse) index = key.c - ord('a') if key.vk == libtcod.KEY_NONE: continue #if nothing is pressed keep looping elif key.vk == libtcod.KEY_ENTER and key.lalt: #Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif index >= 0 and index < len(options): return index #if an option is chosen return it's index in the options list elif index < 0 or index >= len(options): return None #if any other key is pressed close the menu
def menu(header, options, width): if len(options) > MAX_OPTIONS: 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, g.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 = g.SCREEN_WIDTH / 2 - width / 2 y = g.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() g.key = libtcod.console_wait_for_keypress(True) if g.key.vk == libtcod.KEY_ENTER and g.key.lalt: # (special case) Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) index = g.key.c - ord('a') if 0 <= index < len(options): return index return None
def instructions(header, options, width): global con 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 + 3 #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, 1, width, height, libtcod.BKGND_NONE, libtcod.LEFT, header) #print all the options y = header_height for option_text in options: text = option_text libtcod.console_print_ex(window, 1, y, libtcod.BKGND_NONE, libtcod.LEFT, text) y += 1 libtcod.console_print_ex(window, 1, y, libtcod.BKGND_NONE, libtcod.LEFT, '') libtcod.console_print_ex(window, 1, y + 1, libtcod.BKGND_NONE, libtcod.LEFT, 'Press enter to close the menu.') libtcod.console_print_ex(window, 1, y + 2, libtcod.BKGND_NONE, libtcod.LEFT, '') #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.9) #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): return else: key = libtcod.console_wait_for_keypress(True)
def menu(header, options, width): """ Display a menu of options headed by letters; return (the key pressed, the index [0, 25] of the selection, or None). """ global _con 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, config.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) 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 = config.SCREEN_WIDTH / 2 - width / 2 y = config.SCREEN_HEIGHT / 2 - height / 2 libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7) libtcod.console_flush() while True: key = block_for_key() if not (key.vk == libtcod.KEY_ALT or key.vk == libtcod.KEY_CONTROL or key.vk == libtcod.KEY_SHIFT): break index = key.c - ord('a') if index >= 0 and index < len(options): return (key.c, index) return (key.c, None)
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) # 打印 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) 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 # 打印所有项目 # 原理 : 打印一个循环,第一个选项的 Y 坐标位于页眉的正下方;我们打印该选项的文本, 并增加它。 # 然后从字母 A 开始, 每次递增, 以在选项的文本旁边显示它。函数返回字母 A 的 ASCII 码; # 然后, 可以用来增加它来获取其余字母的代码。 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) # 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 menu(con, header, options, width, screen_width, screen_height, colors, in_game=True): """Show menu window in center of the screen.""" 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 more 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, colors['text_default']) libtcod.console_set_color_control(libtcod.COLCTRL_1, colors['text_emphasize'], colors['background_default']) 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 = f'%c({chr(letter_index)})%c{option_text}' % ( libtcod.COLCTRL_1, libtcod.COLCTRL_STOP) 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 if not in_game: x = int(screen_width / 2 - width / 2) else: 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(self, header, options, width): global keys if len(options) > 26: raise ValueError("cannot have a menu with more than 26 options") header_height = libtcod.console_get_height_rect( self.con, 0, 0, width, R.SCREEN_HEIGHT, header) if header_height == "": header_height = 0 height = len(options) + header_height #now creates an off screen console that represents the menu windwo. 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) 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 = R.SCREEN_WIDTH / 2 - width / 2 y = R.SCREEN_HEIGHT / 2 - height / 2 libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7) while True: libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) if key.vk == libtcod.KEY_ENTER and key.lalt: #Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen( not libtcod.console_is_fullscreen()) if key.vk == libtcod.KEY_ESCAPE or key.vk == libtcod.KEY_BACKSPACE: return None #exit inventory index = key.c - ord("a") if index >= 0 and index < len(options): return index
def draw_menu(console, header, options, width, empty_text=None, keys=None, format_=None): if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options.') if len(options) == 0 and empty_text: options = [empty_text] # Calculate total height for header (after auto-wrap), # and one line per option if header == '': header_height = 0 else: header_height = libtcod.console_get_height_rect(console, 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 if format_: fmt = format_ else: fmt = '(%s) %s' y = header_height letter_index = ord('a') for i in range(len(options)): if keys: key = keys[i] else: key = chr(letter_index) text = fmt % (key, options[i]) libtcod.console_print_ex(window, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, text) y += 1 letter_index += 1 # Blit the contents of the menu 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)
def menu(header, options, width, back_color=libtcod.blue, text_color=libtcod.white): 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_background(window, back_color) libtcod.console_set_default_foreground(window, text_color) libtcod.console_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_SCREEN, 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_SCREEN, 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, 1.0) #opacity of fg, bg = 1.0, 1.0 #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