def main_menu(): while not libtcod.console_is_window_closed(): libtcod.console_disable_keyboard_repeat() libtcod.console_set_default_background(0, libtcod.black) libtcod.console_set_default_foreground(0, libtcod.black) libtcod.console_clear(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, 'BALL LABYRINTH') libtcod.console_print_ex(0, SCREEN_WIDTH / 2, SCREEN_HEIGHT - 2, libtcod.BKGND_NONE, libtcod.CENTER, 'By Eric Williams') # Show options and wait for the player's choice. choice = menu('', ['Play a new game', 'Load saved game', 'Play custom level', 'Quit'], 24) if choice == 0: # New game. new_game() libtcod.console_set_keyboard_repeat(5, 5) play_game() elif choice == 1: # Load saved game. loaded = load_game() if loaded: libtcod.console_set_keyboard_repeat(5, 5) play_game() elif choice == 2: # Open the test arena level. custom = new_custom_game() if custom: libtcod.console_set_keyboard_repeat(5, 5) play_game() elif choice == 3: # Quit. break
def get_filename(append=None): libtcod.console_disable_keyboard_repeat() height = 3 width = 50 filename = '' window = libtcod.console_new(width, height) while True: # Don't leave the menu until a valid filename has been written. # Print a nice border around the menu. libtcod.console_clear(window) frame = '#' * width * height back = ' ' * (width - 1) * (height - 1) libtcod.console_set_default_foreground(window, libtcod.white) libtcod.console_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, frame) libtcod.console_print_rect_ex(window, 1, 1, width - 2, height - 2, libtcod.BKGND_NONE, libtcod.LEFT, back) # Print the filename. libtcod.console_set_default_foreground(window, libtcod.white) if append: libtcod.console_print(window, 1, 1, filename + append) else: libtcod.console_print(window, 1, 1, filename) # 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() # Wait for a keypress. key = libtcod.console_check_for_keypress(libtcod.KEY_PRESSED) key_char = chr(key.c) key_letter_index = key.c - ord('a') key_number_index = key.c - ord('0') # Handle keys. if key.vk == libtcod.KEY_ENTER and key.lalt: # Special case: Alt + Enter toggles fullscreen. libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) if (key_letter_index >= 0 and key_letter_index < 27) or (key_number_index >= 0 and key_number_index < 10): filename += key_char elif key.vk == libtcod.KEY_BACKSPACE or key.vk == libtcod.KEY_DELETE: filename = filename[:len(filename)-1] elif key.vk == libtcod.KEY_ENTER: if append: filename += append return filename elif key.vk == libtcod.KEY_ESCAPE: return None
def menu(header, options, width): if len(options) > 26: raise ValueError("Cannot have a menu with more than 26 options.") # Disable repeat to avoid selecting wrong chocies. Re-enable it when leaving. libtcod.console_disable_keyboard_repeat() #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 if len(options) > 0: opt_height = len(options) else: opt_height = 1 height = opt_height + header_height + 2 # Create an off-screen console that represents the menu's window. window = libtcod.console_new(width, height) while True: # Don't leave menu until a valid choice has been made. # Print a nice border around the menu. frame = '#' * width * height back = ' ' * (width - 1) * (height - 1) libtcod.console_set_default_foreground(window, libtcod.white) libtcod.console_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, frame) libtcod.console_print_rect_ex(window, 1, 1, width - 2, height - 2, libtcod.BKGND_NONE, libtcod.LEFT, back) # Print the header with auto-wrap. libtcod.console_print_rect_ex(window, 1, 1, width - 2, height - 2, libtcod.BKGND_NONE, libtcod.LEFT, header) # Print all of the options. y = header_height + 1 letter_index = ord('a') for option_text in options: text = '(' + chr(letter_index) + ')' + option_text libtcod.console_print_ex(window, 1, 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. libtcod.console_flush() # Wait for a keypress. key = libtcod.console_check_for_keypress(libtcod.KEY_PRESSED) # Convert the ASCII code to an index. index = key.c - ord('a') # Handle keys. if key.vk == libtcod.KEY_ENTER and key.lalt: # Special case: Alt + Enter toggles fullscreen. libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) if len(options) > 0: if key.vk == libtcod.KEY_ESCAPE: # Cancel and exit. libtcod.console_set_keyboard_repeat(5, 5) return None elif index >= 0 and index < len(options): # Return the option. libtcod.console_set_keyboard_repeat(5, 5) return index elif key.vk == libtcod.KEY_ENTER or key.vk == libtcod.KEY_ESCAPE: libtcod.console_set_keyboard_repeat(5, 5) return None
def select_target(mode, x, y): # Set the loop. key = libtcod.Key() libtcod.console_disable_keyboard_repeat() while True: # Function loop. # Render the screen. render_all() # Print the 'look' character. libtcod.console_set_default_foreground(0, libtcod.yellow) libtcod.console_put_char(0, x, y, 'X', libtcod.BKGND_NONE) # Update the screen. libtcod.console_flush() # Process key input. key = libtcod.console_check_for_keypress(libtcod.KEY_PRESSED) if key.vk == libtcod.KEY_ENTER and key.lalt: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif key.vk == libtcod.KEY_ENTER: # Select the target tile and leave the loop. libtcod.console_set_keyboard_repeat(5, 5) return mode, x, y elif key.vk == libtcod.KEY_ESCAPE: # Cancel and leave the loop. libtcod.console_set_keyboard_repeat(5, 5) mode = False return mode, x, y # Move the cursor around. elif key.vk in [libtcod.KEY_UP, libtcod.KEY_KP8]: if y > 0: y -= 1 elif key.vk in [libtcod.KEY_DOWN, libtcod.KEY_KP2]: if y < MAP_HEIGHT - 1: y += 1 elif key.vk in [libtcod.KEY_LEFT, libtcod.KEY_KP4]: if x > 0: x -=1 elif key.vk in [libtcod.KEY_RIGHT, libtcod.KEY_KP6]: if x < MAP_WIDTH - 1: x += 1 elif key.vk == libtcod.KEY_KP7: if x > 0 and y > 0: x -= 1 y -= 1 elif key.vk == libtcod.KEY_KP9: if x < MAP_WIDTH - 1 and y > 0: x +=1 y -=1 elif key.vk == libtcod.KEY_KP1: if x > 0 and y < MAP_HEIGHT - 1: x -= 1 y += 1 elif key.vk == libtcod.KEY_KP3: if x < MAP_WIDTH - 1 and y < MAP_HEIGHT - 1: x += 1 y += 1
import libtcodpy as libtcod from logic.menu_main import MenuMain from logic.menu_manager import MenuManager screen_width = 75 screen_height = 75 limit_fps = 30 menu_manager = MenuManager(MenuMain(screen_width, screen_height)) libtcod.console_disable_keyboard_repeat() libtcod.console_set_custom_font('cp437_10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_ASCII_INROW) libtcod.console_init_root(screen_width, screen_height, 'RogueP pre-dev', False) libtcod.sys_set_fps(limit_fps) while not libtcod.console_is_window_closed(): #behavior_manager.update_behaviors(entities) if not menu_manager.loop(round(libtcod.sys_get_last_frame_length()*1000)): break