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("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 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): #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.') #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(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(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 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.') # 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 print_lyrics(panel, lyrics, active_character): primary_color = tcod.sea primary_color_dark = tcod.darkest_sea tcod.console_set_default_foreground(panel, primary_color) tcod.console_set_default_background(panel, tcod.black) tcod.console_set_color_control(tcod.COLCTRL_1, primary_color_dark, tcod.black) tcod.console_set_color_control(tcod.COLCTRL_2, tcod.black, primary_color) lyrics_pre_active = '%c{}%c'.format( lyrics[:active_character]) % (tcod.COLCTRL_1, tcod.COLCTRL_STOP) lyrics_active = '%c{}%c'.format( lyrics[active_character]) % (tcod.COLCTRL_2, tcod.COLCTRL_STOP) lyrics_post_active = lyrics[active_character + 1:] formatted_lyrics = lyrics_pre_active + lyrics_active + lyrics_post_active x = int(tcod.console_get_width(panel) / 2) y = 1 w = tcod.console_get_width(panel) - 8 h = tcod.console_get_height(panel) tcod.console_print_rect_ex(panel, x, y, w, h, tcod.BKGND_SET, tcod.CENTER, formatted_lyrics)
def display(self, mouse): ''' Display the button, and highlight if necessary ''' color = self.color # Highlight if self.mouse_is_inside(mouse): color = self.hcolor if self.hover_text: #hover_info(interface=interface, header=['t'], text=self.hover_text, xb=self.x, yb=self.y-10, transp=0) self.gui_panel.interface.add_hover_info(interface=self.gui_panel.interface, header=self.hover_header, text=self.hover_text, cx=mouse.cx + self.hover_text_offset[0], cy=mouse.cy + self.hover_text_offset[1], transp=1, do_hover=0) # Handle clicks if mouse.lbutton_pressed and not self.clicked: mouse.lbutton_pressed = 0 self.click() if self.do_draw_box: self.gui_panel.draw_box(self.x, self.x + self.width, self.y, self.y + self.height, color) libtcod.console_set_default_foreground(self.con, color) libtcod.console_print_rect_ex(con=self.con, x=self.center_x, y=self.text_y, w=self.width, h=self.height, flag=libtcod.BKGND_NONE, alignment=libtcod.CENTER, fmt=self.text) libtcod.console_set_default_foreground(self.gui_panel.con, self.gui_panel.frontcolor)
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') # 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(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 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('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 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 input_text(width, height, input_prompt, end_prompt): #width and height determine the dimensions of the prompt console. #input prompt is the message before the entry line #end_prompt is the message after the entry line text = '' while True: libtcod.console_flush() 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, input_prompt + text + end_prompt) (x, y) = (defn.SCREEN_WIDTH / 2 - width / 2, defn.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 0 <= index <= 25 or -32 <= index <= -7 or index == -65: #lowercase letters, capital letters, and spaces are allowed text += chr(key.c) elif len(text) > 0: if key.c == 8: text = text[:-1] if key.c == 13: break gui.clear_screen() return text
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('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): 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): #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.') #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 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 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 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(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 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 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 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 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 main(): # Setup player global player_x, player_y, player_cor, snake_len, fruit_x, fruit_y, high_score player_x = SCREEN_WIDTH // 2 player_y = SCREEN_HEIGHT // 2 player_cor = [] snake_len = 0 high_score = 0 fruit_x = randint(0, 80) fruit_y = randint(0, 50) game_over = "u lost" # Setup Font font_filename = 'arial10x10.png' tcod.console_set_custom_font( font_filename, tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD) # Initialize screen title = 'Snake' root = tcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, title, FULLSCREEN) # Set FPS tcod.sys_set_fps(LIMIT_FPS) exit_game = False while not tcod.console_is_window_closed() and not exit_game: tcod.console_set_default_foreground(0, tcod.white) tcod.console_put_char(0, player_x, player_y, '@', tcod.BKGND_NONE) tcod.console_flush() if player_x == fruit_x and player_y == fruit_y: snake_len += 1 high_score += 1 place_fruit() snake() handle_keys() if tcod.console_get_char(0, player_x, player_y) == 49: sleep(1) tcod.console_clear(root) tcod.console_flush() sleep(0.5) tcod.console_print_rect_ex(root, (SCREEN_WIDTH // 2) - len(game_over) // 2, SCREEN_HEIGHT // 2, 0, 0, 0, 0, "{}".format(game_over)) tcod.console_print_rect_ex( root, (SCREEN_WIDTH // 2) - len("Highscore: {}".format(high_score)) // 2, (SCREEN_HEIGHT // 2) + 2, 0, 0, 0, 0, "Highscore: {}".format(high_score)) tcod.console_flush() sleep(5) sys.exit()
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 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 city_production_menu(): width, height = R.MAP_VIEW_WIDTH - 4, R.MAP_VIEW_HEIGHT - 4 city_select_pop = libtcod.console_new(width, height) selected_city = None limit = len(cities) - 1 pos_x = R.MAP_VIEW_WIDTH / 2 - width / 2 pos_y = R.MAP_VIEW_HEIGHT / 2 - height / 2 for a in range(R.MAP_VIEW_WIDTH - 4): #clear screen, colour dark grey, every cycle for b in range(R.MAP_VIEW_HEIGHT - 4): #libtcod.console_print_rect(window, a, b, libtcod.console_print_rect_ex(city_select_pop, a, b, R.MAP_VIEW_WIDTH - 4, R.MAP_VIEW_HEIGHT - 4, libtcod.BKGND_NONE, libtcod.LEFT, " ") libtcod.console_blit(city_select_pop, 0, 0, width, height, 0, pos_x, pos_y, 1.0, 0.9) libtcod.console_flush() offset = 0 while True: libtcod.console_clear(city_select_pop) libtcod.console_set_default_foreground(city_select_pop, libtcod.yellow) libtcod.console_set_default_foreground(city_select_pop, libtcod.light_yellow) #city_length = len(cities) for city in range(len(cities)): # picks the smaller of the two. location = cities[city] resources = "\n" for resource in location.producing: supply_demand = location.trade_house.supply_demand[resource] resources += resource + " " + str( location.producing[resource][1]) + " \t " + str( supply_demand[0]) + " " + str( supply_demand[1]) + ", " + str( supply_demand[2]) + "\n" libtcod.console_print_ex(city_select_pop, 1, 1 + offset, libtcod.BKGND_NONE, libtcod.LEFT, location.name + " " + resources) offset += 6 libtcod.console_blit(city_select_pop, 0, 0, width, height, 0, pos_x, pos_y, 1.0, 0.9) libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) if key.vk == libtcod.KEY_ENTER or key.vk == libtcod.KEY_BACKSPACE: break
def text_menu(header, text): libtcod.console_set_default_foreground(var.MenuPanel, var.TextColor) libtcod.console_set_default_background(var.MenuPanel, libtcod.black) # Clear and print header: libtcod.console_clear(var.MenuPanel) libtcod.console_print_rect_ex(var.MenuPanel, 1, 1, var.MenuWidth, var.MenuHeight, libtcod.BKGND_SET, libtcod.LEFT, header + " [Space for next; Esc to exit]") # Text should always be a list of lines. line = -1 y = 28 while abs(line) <= len(text): (toPrint, color, turn) = text[line] try: libtcod.console_set_default_foreground(var.MenuPanel, color) except: libtcod.console_set_default_foreground(var.MenuPanel, var.TextColor) libtcod.console_print_ex(var.MenuPanel, 2, y, libtcod.BKGND_SET, libtcod.LEFT, "[" + str(turn) + "] " + toPrint) line -= 1 y -= 1 if y < 3 or abs(line) > len(text): # Draw it and wait for input: libtcod.console_blit(var.MenuPanel, 0, 0, var.MenuWidth, var.MenuHeight, 0, 5, 5) libtcod.console_flush() while True: Key = libtcod.console_wait_for_keypress(True) if Key.vk == libtcod.KEY_ESCAPE: return None if Key.vk == libtcod.KEY_SPACE: libtcod.console_clear(var.MenuPanel) libtcod.console_set_default_foreground( var.MenuPanel, var.TextColor) libtcod.console_print_rect_ex( var.MenuPanel, 1, 1, var.MenuWidth, var.MenuHeight, libtcod.BKGND_SET, libtcod.LEFT, header + " [Space for next page; Esc to exit]") y = 28 break
def examine_screen(entities, mouse, fov_map, character_screen_width, character_screen_height, screen_width, screen_height): window = libtcod.console_new(character_screen_width, character_screen_height) libtcod.console_set_default_foreground(window, libtcod.white) (x, y) = (mouse.cx, mouse.cy) for entity in entities: if entity.x == x and entity.y == y and libtcod.map_is_in_fov( fov_map, entity.x, entity.y): libtcod.console_print_rect_ex(window, 0, 1, character_screen_width, character_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, '{0}'.format(entity.name)) libtcod.console_print_rect_ex(window, 0, 2, character_screen_width, character_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, 'Resistances') idx = 3 if entity.fighter: for res in entity.fighter.resistance: libtcod.console_print_rect_ex( window, 0, idx, character_screen_width, character_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, '{0}: {1}'.format(res, entity.fighter.resistance[res])) idx += 1 libtcod.console_print_rect_ex(window, 0, idx, character_screen_width, character_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, 'Conditions') idx += 1 if entity.fighter: for con in entity.fighter.conditions: libtcod.console_print_rect_ex( window, 0, idx, character_screen_width, character_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, '{0}: {1}'.format(con, entity.fighter.conditions[con])) idx += 1 x = screen_width // 2 - character_screen_width // 2 y = screen_height // 2 - character_screen_height // 2 libtcod.console_blit(window, 0, 0, character_screen_width, character_screen_height, 0, x, y, 1.0, 0.7)
def render(self): libtcod.console_set_default_foreground(self.panel, self.text_color) libtcod.console_print_rect_ex(self.panel, self.x, self.y, self.width, self.height, libtcod.BKGND_NONE, libtcod.LEFT, self.header) y = self.header_height for option_text in self.options: if (option_text == self.options[self.selected_index]): libtcod.console_set_default_background(self.panel, self.selected_bkgnd) else: libtcod.console_set_default_background(self.panel, self.unselected_bkgnd) libtcod.console_print_ex(self.panel, self.x, y + self.y, libtcod.BKGND_SET, libtcod.LEFT, str(option_text)) y += 1
def job_screen(player, character_screen_width, character_screen_height, screen_width, screen_height): window = libtcod.console_new(character_screen_width, character_screen_height) libtcod.console_set_default_foreground(window, libtcod.white) libtcod.console_print_rect_ex( window, 0, 2, character_screen_width, character_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, 'Level in Fighter: {0}'.format(player.fighter.job.fighter_level)) libtcod.console_print_rect_ex( window, 0, 3, character_screen_width, character_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, 'Levels in Magician: {0}'.format(player.fighter.job.magician_level)) libtcod.console_print_rect_ex( window, 0, 4, character_screen_width, character_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, 'Levels in Cleric: {0}'.format(player.fighter.job.cleric_level)) libtcod.console_print_rect_ex( window, 0, 5, character_screen_width, character_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, 'Levels in Thief: {0}'.format(player.fighter.job.thief_level)) x = screen_width // 2 - character_screen_width // 2 y = screen_height // 2 - character_screen_height // 2 libtcod.console_blit(window, 0, 0, character_screen_width, character_screen_height, 0, x, y, 1.0, 0.7)
def leave_game(self): window = lt.console_new(30, 10) lt.console_set_default_foreground(window, lt.white) lt.console_set_default_background(window, lt.black) lt.console_print_rect_ex(window, 15, 3, 20, 10, lt.BKGND_NONE, lt.CENTER, 'Do you really want to quit?\nY/N') lt.console_blit(window, 0, 0, 0, 0, 0, MAP_WIDTH/2 - 15, MAP_HEIGHT/2 - 5, 1.0, 0.8) lt.console_flush() while True: key = lt.console_wait_for_keypress(True) if key.c == ord('y') or key.c == ord('Y'): return "exit" elif key.c != 27: return "no turn"
def help_menu(header, lines): libtcod.console_set_default_background(var.MenuPanel, libtcod.black) libtcod.console_set_default_foreground(var.MenuPanel, var.TextColor) libtcod.console_clear(var.MenuPanel) libtcod.console_print_rect_ex(var.MenuPanel, 1, 1, var.MenuWidth, var.MenuHeight, libtcod.BKGND_SET, libtcod.LEFT, header) libtcod.console_print_ex(var.MenuPanel, 1, 28, libtcod.BKGND_SET, libtcod.LEFT, "[Space for next; Esc to exit]") line = 0 y = 3 while line < len(lines): libtcod.console_print_ex(var.MenuPanel, 2, y, libtcod.BKGND_SET, libtcod.LEFT, lines[line]) line += 1 y += 1 if y == 27 or line >= len(lines): # Draw it and wait for input: libtcod.console_blit(var.MenuPanel, 0, 0, var.MenuWidth, var.MenuHeight, 0, 5, 5) libtcod.console_flush() while True: Key = libtcod.console_wait_for_keypress(True) if Key.vk == libtcod.KEY_ESCAPE: return None if Key.vk == libtcod.KEY_SPACE: # This unfortunately must be repeated here: libtcod.console_clear(var.MenuPanel) libtcod.console_print_rect_ex(var.MenuPanel, 1, 1, var.MenuWidth, var.MenuHeight, libtcod.BKGND_SET, libtcod.LEFT, header) libtcod.console_print_ex(var.MenuPanel, 1, 28, libtcod.BKGND_SET, libtcod.LEFT, "[Space for next; Esc to exit]") y = 3 break
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 dialogue_screen(player, npc, dialogue_screen_width, dialogue_screen_height, screen_width, screen_height): # X = 50 Y = 30 window = libtcod.console_new(dialogue_screen_width, dialogue_screen_height) libtcod.console_set_default_background(window, libtcod.darker_grey) libtcod.console_set_default_foreground(window, libtcod.white) libtcod.console_clear(window) libtcod.console_print_rect_ex(window, 0, 0, dialogue_screen_width, dialogue_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, '{0}'.format(npc.dialogue.dialogue)) libtcod.console_print_rect_ex(window, 0, 29, dialogue_screen_width, dialogue_screen_height, libtcod.BKGND_NONE, libtcod.LEFT, 'Press ESC to close this window.') x = screen_width // 2 - dialogue_screen_width // 2 y = screen_height // 2 - dialogue_screen_height // 2 libtcod.console_blit(window, 0, 0, dialogue_screen_width, dialogue_screen_height, 0, x, y, 1.0, 1.0)
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 loading_bar(con,header,progress,screen_width,screen_height): x=screen_width//4 #Starts a quarter of the way across the screen y=screen_height//4 #Quarter of the way down the screen width=screen_width//2 #Half the width of the screen height=4 window=lc.console_new(width,4) # Create loading bar window lc.console_set_default_foreground(window,lc.white) lc.console_print_rect_ex(window,0,1,screen_width//2,4,lc.BKGND_NONE,lc.LEFT,header) for pos in range(0,int(width*progress)): # Set achieved progress to white lc.console_set_char_background(window,pos,2,lc.white) for pos in range(int(width*progress),width): # Set remaining space to grey lc.console_set_char_background(window,pos,2,lc.light_grey) lc.console_blit(window,0,0,width,height,0,x,y,1.0,1.0) #Blit changes
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(header, options, width): if len(options) > 26: raise ValueError("Cannot have a menu with more than twenty-six options.") #Calculate total height for the header (after auto-wrap) and one line per option. headerHeight = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header) if header == "": headerHeight = 0 height = len(options) + headerHeight #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, one by one. ORD and CHR are built-in Python functions. chr(i) returns a string #of one character whose ASCII code is in the integer i - for example, chr(97) returns "a". ord(c) is #the opposite - given a string of length one, it returns an integer representing the Unicode code #point of the character - for example, ord("a") returns 97. 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 the contents of window to the root console. The last two parameters passed to console_blit #define the foreground and background transparency, respectively. 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 keypress. libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) if key.vk == libtcod.KEY_ENTER and key.lalt: #Alt-Enter toggles 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, otherwise return None index = key.c - ord("a") if index >= 0 and index < len(options): return index return None
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 print_rect_ex(self, x=0, y=0, width=None, height=None, effect=libtcod.BKGND_NONE, align=libtcod.LEFT, text=''): if width is None: width = self.width if height is None: height = self.height return libtcod.console_print_rect_ex(self.console_id, x, y, width, height, effect, align, text)
def menu(self, header, options, width): #check to see if there's more than 26 options 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 if header == '': header_height = 0 else: header_height = libtcod.console_get_height_rect(self.con, 0, 0, width, Screen.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 the 'window' to the root console x = Screen.SCREEN_WIDTH / 2 - width / 2 y = Screen.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) #check if they user wants to go full screen 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 show_highscores(): #Load them from a file try: file = shelve.open('highscores', 'r') scores = file['scores'] file.close() except: print "No highscore file found." scores = [] #Format them into an array of lines texts = [] for i in range(0, min(25, len(scores))): #(name, score) in scores[0 : min(25, len(scores))]: (name, score) = scores[i] spacecount = max(20 - len(name) - len(str(i)), 0) texts.append(str(i) + ". " + name + (' ' * spacecount) + str(score)) #menu("High Scores", texts, SCORE_WIDTH) #calculate total height for the header (after auto-wrap) and one line per option header = "High Scores" width = SCORE_WIDTH header_height = libtcod.console_get_height_rect(g.con, 0, 0, width, SCREEN_HEIGHT, header) if header == '': header_height = 0 height = len(texts) + 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 = 1 for option_text in texts: text = option_text libtcod.console_print_ex(window, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, text) y += 1 letter_index += 1 return window, width, height
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, Game): if len(options) > data.MAX_NUM_ITEMS: message('Cannot have a menu with more than ' + str(data.MAX_NUM_ITEMS) + ' options.', Game) #calculate total height of the header (after auto-wrap) and one line per option header_height = libtcod.console_get_height_rect(Game.con, 0, 0, width, data.SCREEN_HEIGHT, header) if header == '': header_height = 0 height = len(options) + header_height #create 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 contents of window to root console x = data.SCREEN_WIDTH / 2 - width / 2 y = data.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 keypress libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) if key.vk == libtcod.KEY_ENTER and key.lalt: # full screen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) #convert ASCII code to an index. if it's valid, return it index = key.c - ord('a') if index >= 0 and index < len(options): return index else: return None
def render(self, draw_frame=False, draw_index=False, align='left', fgalpha=.9, bgalpha=.3): if len(self.opts) > MAX_OPTS: raise ValueError('No more than %d options allowed.' % MAX_OPTS) # calculate height, set options window, print header _w, _h = self.width+2, self.h+2 #total width and height window = tl.console_new(_w, _h) tl.console_clear(window) tl.console_set_default_background(window, tl.violet) tl.console_set_default_foreground(window, tl.lightest_grey) if draw_frame: tl.console_print_frame(window, 0, 0, _w, _h) tl.console_print_rect_ex(window, 1,1, self.width,self.h, tl.BKGND_NONE,tl.LEFT, self.header) if self.opts != []: # draw a separator line if options are not empty tl.console_hline(window, 1, self.headerHeight+1, self.width) if draw_frame: tl.console_put_char(window, 0, self.headerHeight+1, tl.CHAR_TEEE) tl.console_put_char(window, self.width+1, self.headerHeight+1, tl.CHAR_TEEW) # print all options n,y = 0,self.headerHeight+2 sel = self.highlightedItemIdx if self.highlightedItemIdx >= 0 else MAX_OPTS+self.highlightedItemIdx for txt in self.opts: txt = txt.rjust(self.width) if align=='right'\ else txt.center(self.width) if align=='center'\ else ' '+txt.ljust(self.width)[:-3] if draw_index\ else txt.ljust(self.width) if draw_index: txt = ' %d %s' % (n, txt) txt = '{'+txt[1]+'}'+txt[6:] if sel == n else txt[3:] scolor = 'amber' if sel == n else 'light_grey' if self.isAnimated and sel == n: # hicky-hacky color animation scolor = tl.amber*(self.phase/PHASES) tl.console_set_color_control(tl.COLCTRL_1, scolor, tl.black) txt = '%c%s%c'%(tl.COLCTRL_1,txt.ljust(self.width),tl.COLCTRL_STOP) else: txt = tc.parse('{{%s}}%s{{stop}}'%(scolor,txt.ljust(self.width))) tl.console_print(window, 1, y, txt) n += 1; y += 1 # calculate window position, blit window to screen & flush x = SCREEN_W/2 - self.width/2 y = SCREEN_H/2 - self.h/2 tl.console_blit(window, 0, 0, _w, _h, 0, x, y, fgalpha, bgalpha)