def menu(header, options, width): if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options.') #calc height of header header_height = libtcod.console_height_left_rect(con, 0, 0, width, SCREEN_HEIGHT, header) if header == '': header_height = 0 height = len(options) + header_height #offscreen conolse window = libtcod.console_new(width, height) #print the header libtcod.console_set_foreground_color(window, libtcod.white) libtcod.console_print_left_rect(window, 0, 0, width, height, libtcod.BKGND_NONE, header) y = header_height letter_index = ord('a') for option_text in options: text = '(' + chr(letter_index) + ') ' + option_text libtcod.console_print_left(window, 0, y, libtcod.BKGND_NONE, text) y += 1 letter_index += 1 #blit the contents to 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) #wait for keypress libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) #convert ascii to index 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) and one line per option header_height = libtcod.console_height_left_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_foreground_color(window, libtcod.white) libtcod.console_print_left_rect(window, 0, 0, width, height, libtcod.BKGND_NONE, 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_left(window, 0, y, libtcod.BKGND_NONE, 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): if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options.') #calc height of header header_height = libtcod.console_height_left_rect(con, 0, 0, width, SCREEN_HEIGHT, header) if header == '': header_height = 0 height = len(options) + header_height #offscreen conolse window = libtcod.console_new(width, height) #print the header libtcod.console_set_foreground_color(window, libtcod.white) libtcod.console_print_left_rect(window, 0, 0, width, height, libtcod.BKGND_NONE, header) y = header_height letter_index= ord('a') for option_text in options: text = '(' + chr(letter_index) + ') ' + option_text libtcod.console_print_left(window, 0, y, libtcod.BKGND_NONE, text) y += 1 letter_index += 1 #blit the contents to 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) #wait for keypress libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) #convert ascii to index 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) and one line per option header_height = libtcod.console_height_left_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_foreground_color(window, libtcod.white) libtcod.console_print_left_rect(window, 0, 0, width, height, libtcod.BKGND_NONE, 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_left(window, 0, y, libtcod.BKGND_NONE, 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