def target_tile(max_range=None): box = libtcod.console_new(1, 1) x = Game.player.x y = Game.player.y libtcod.console_set_default_background(box, libtcod.orange) libtcod.console_clear(box) key = Game.key while (x, y) != (0, 0): Game.renderer.render_all() Game.renderer.render_names_under_target(x, y) Game.renderer.render_target_tile(box, x, y) key = libtcod.console_wait_for_keypress(True) key = libtcod.console_wait_for_keypress(True) direction = Game.get_direction(key) if direction is not None: x += direction[0] y += direction[1] else: return (None, None) if direction == (0, 0): if Game.map.is_tile_in_fov(x, y) and (max_range is None or Game.player.distance(x, y) <= max_range): return (x, y) else: Game.message('That is out of range.', libtcod.red)
def target_tile(max_range=None): box = libtcod.console_new(1, 1) x = Game.player.x y = Game.player.y libtcod.console_set_default_background(box, libtcod.orange) libtcod.console_clear(box) key = Game.key while (x, y) != (0, 0): Game.render_all() libtcod.console_blit(box, 0, 0, 1, 1, 0, x, y, 1.0, 0.5) #0.7 is the transparency libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) key = libtcod.console_wait_for_keypress(True) if key.vk == libtcod.KEY_ESCAPE: return (None, None) direction = Game.getDirection(key) if direction is not None: x += direction[0] y += direction[1] if direction == (0, 0): if Game.map.is_tile_in_fov(x, y) and (max_range is None or Game.player.distance(x, y) <= max_range): return (x, y) else: Game.message('That is out of range.', libtcod.red)
def target_tile(max_range=None): box = libtcod.console_new(1, 1) x = Game.player.x y = Game.player.y libtcod.console_set_default_background(box, libtcod.orange) libtcod.console_clear(box) key = Game.key while (x, y) != (0, 0): Game.render_all() libtcod.console_blit(box, 0, 0, 1, 1, 0, x, y, 1.0, 0.5) #0.7 is the transparency libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) key = libtcod.console_wait_for_keypress(True) if key.vk == libtcod.KEY_ESCAPE: return (None, None) direction = Game.getDirection(key) if direction is not None: x += direction[0] y += direction[1] if direction == (0, 0): if Game.map.is_tile_in_fov( x, y) and (max_range is None or Game.player.distance(x, y) <= max_range): return (x, y) else: Game.message('That is out of range.', libtcod.red)
def runEndGame(): messager.addMessage("You have passed Trigger's gauntlet!") messager.addMessage("You escape into the Net, discovering freedom...") while messager.moreMessages(): messager.nextMessage() libtcod.console_flush() libtcod.console_wait_for_keypress(True) print "I... have quite perished... yet..." exit()
def handle_keys(): Game.key = libtcod.console_wait_for_keypress(True) Game.key = libtcod.console_wait_for_keypress(True) if Game.key.vk == libtcod.KEY_ESCAPE: return 'exit' if Game.state == 'playing': #movement keys Game.map.fov_recompute = True direction = Game.getDirection(Game.key) if direction is None: Game.map.fov_recompute = False key_char = chr(Game.key.c) if key_char == 'g': Game.try_pick_up() if key_char == 'i': chosen_item = Game.inventory_menu( 'Press the key next to an item to use it.\n') if chosen_item is not None: chosen_item.use() if key_char == 'd': chosen_item = Game.inventory_menu( 'Press the key next to an item to drop it.\n') if chosen_item is not None: chosen_item.drop() if key_char == '<': if Game.map.stairs.x == Game.player.x and Game.map.stairs.y == Game.player.y: Game.next_level() if key_char == 'c': level_up_exp = Game.get_exp_to_level() Game.msgbox( 'Character Information\n\nLevel: ' + str(Game.player.level) + '\nExperience: ' + str(Game.player.fighter.xp) + '\nExperience to level up: ' + str(level_up_exp) + '\n\nMaximum HP: ' + str(Game.player.fighter.max_hp) + '\nAttack: ' + str(Game.player.fighter.power) + '\nDexterity: ' + str(Game.player.fighter.dexterity), Game.CHARACTER_SCREEN_WIDTH) return 'didnt-take-turn' elif direction == (0, 0): Game.try_pick_up() pass else: Game.player.move_or_attack(direction[0], direction[1])
def gods_words(self): """welcome screen""" words = "The Game of Life\n\n" \ "l-click / r-click to place / remove seeds\n" \ "press enter to observe or reset\n" \ "press escape to exit\n\n" \ "press any key to start" libtcod.console_print_ex(0, self.width // 2, self.height // 2, libtcod.BKGND_NONE, libtcod.CENTER, words) libtcod.console_flush() libtcod.console_wait_for_keypress(flush=True)
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 GetChoice(self): command = '' key = libtcod.console_wait_for_keypress(True) while key.vk != libtcod.KEY_ENTER: letter = chr(key.c) x = len(command) libtcod.console_set_char( 0, x, 0, letter) #print new character at appropriate position on screen command = command + letter #add to the string key = libtcod.console_wait_for_keypress(True) return command
def choose_direction(self): gui.draw_directions(0, self.owner) key = tcod.console_wait_for_keypress(True) key = tcod.console_wait_for_keypress(True) if key.vk == tcod.KEY_RIGHT: return enum.dir.RIGHT elif key.vk == tcod.KEY_LEFT: return enum.dir.LEFT elif key.vk == tcod.KEY_UP: return enum.dir.UP elif key.vk == tcod.KEY_DOWN: return enum.dir.DOWN return None
def gameOver(): libtcod.console_set_default_foreground(0, libtcod.red) libtcod.console_print(0, 9, 10, "GAMEOVER") libtcod.console_flush() resetPlayer() libtcod.console_wait_for_keypress(True) libtcod.console_set_default_foreground(0, libtcod.red) libtcod.console_print(0, 9, 11, "You died") libtcod.console_flush() libtcod.console_wait_for_keypress(True)
def new_game(): global player, inventory, game_msgs, game_state, dungeon_level, key, race race_choice = None #create object representing the player fighter_component = Fighter(hp=100, defense=1, power=2, xp=0, death_function=player_death) player = Object(0, 0, '@', 'player', libtcod.white, blocks=True, fighter=fighter_component) player.level = 1 #generate map (at this point it's not drawn to the screen) dungeon_level = 1 make_map() initialize_fov() game_state = 'playing' inventory = [] #create the list of game messages and their colors, starts empty game_msgs = [] #a warm welcoming message! message('The UNDERDEEP greets you with a cold breeze that smells of evil. (Press "H" for HELP)', libtcod.red) #initial equipment: a dagger equipment_component = Equipment(slot='right hand', power_bonus=2) obj = Object(0, 0, '-', 'dagger', libtcod.sky, equipment=equipment_component) inventory.append(obj) equipment_component.equip() obj.always_visible = True img = libtcod.image_load('menu_background.png') libtcod.image_blit_2x(img, 0, 0, 0) msgbox("What is your decendancy young adventurer?\n" + "Press ENTER to Continue") libtcod.console_wait_for_keypress(True) libtcod.image_blit_2x(img, 0, 0, 0) while race_choice == None: #keep asking until a choice is made race_choice = menu('Choose Your Race:\n', ['Human', 'Elf', 'Dwarf'], LEVEL_SCREEN_WIDTH) if race_choice == 0: race = 'Human' elif race_choice == 1: race = 'Elf' elif race_choice == 2: race = 'Dwarf'
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 player_card(): #create an off-screen console that represents the card's window window = libtcod.console_new(30, 20) #print player stats libtcod.console_set_foreground_color(window, libtcod.white) libtcod.console_print_left(window, 1, 1, libtcod.BKGND_NONE, 'Player') libtcod.console_print_left(window, 1, 2, libtcod.BKGND_NONE, 'Class: ' + player.stats.plclass) libtcod.console_print_left(window, 1, 3, libtcod.BKGND_NONE, 'STR:' + str(player.stats.str)) libtcod.console_print_left(window, 1, 4, libtcod.BKGND_NONE, 'DEX:' + str(player.stats.dex)) libtcod.console_print_left(window, 1, 5, libtcod.BKGND_NONE, 'CON:' + str(player.stats.con)) libtcod.console_print_left(window, 1, 6, libtcod.BKGND_NONE, 'INT:' + str(player.stats.int)) libtcod.console_print_left(window, 1, 7, libtcod.BKGND_NONE, 'FTH:' + str(player.stats.fth)) libtcod.console_print_left(window, 1, 8, libtcod.BKGND_NONE, 'PER:' + str(player.stats.per)) libtcod.console_print_left(window, 1, 10, libtcod.BKGND_NONE, 'AC: ' + str(player.stats.ac)) libtcod.console_print_left(window, 1, 11, libtcod.BKGND_NONE, 'Encumbrance: ') libtcod.console_print_left(window, 1, 13, libtcod.BKGND_NONE, 'Hit %: ' + str((20-player.stats.hitdie)*5)) libtcod.console_print_left(window, 1, 14, libtcod.BKGND_NONE, 'Damage: ' + str(player.stats.mindmg) + ' - ' + str(player.stats.maxdmg)) #blit the contents of "window" to the root console libtcod.console_blit(window, 0, 0, 30, 20, 0, 1, 1, 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()) return None
def handle_keys(): global playerx, playery #key = libtcod.console_check_for_keypress() #real-time key = libtcod.console_wait_for_keypress(True) #turn-based if key.vk == libtcod.KEY_ENTER and key.lalt: #Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif key.vk == libtcod.KEY_ESCAPE: return True #exit game # Movement keys by arrows. if libtcod.console_is_key_pressed(libtcod.KEY_UP): #north player.move(0, -1) elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN): #south player.move (0, 1) elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT): #west player.move (-1, 0) elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT): #east player.move (1, 0)
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 get_key(self): self.is_visible = True r = None while 1: self.draw() libtcod.console_flush() k = libtcod.console_wait_for_keypress(True) if k and k.pressed and k.c: c = chr(k.c) if c in [i.hotkey for i in self.__items]: r = c break elif c == 'j': self.sel_prev() elif c == 'k': self.sel_next() elif c == ' ': if self.sel_index() != 0: r = self.__items[self.sel_index()].hotkey break self.is_visible = False # TODO: player.redraw_screen() return r
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 handle_keys(): global fov_recompute 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()) elif key.vk == libtcod.KEY_ESCAPE: return True if libtcod.console_is_key_pressed(libtcod.KEY_UP): player.move(0, -1) fov_recompute = True elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN): player.move(0, 1) fov_recompute = True elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT): player.move(-1, 0) fov_recompute = True elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT): player.move(1, 0) fov_recompute = True key_char = chr(key.c) if key_char == 'e': if npc.x == player.x and npc.y == player.y: print('x&y+1 : ', npc.x + 1, npc.y + 1) print('x&y-1 : ', npc.x - 1, npc.y - 1)
def wait_for_key(): """ Get a keypress from the user, and return its code (an integer). """ key = tcod.console_wait_for_keypress(False) return key.c
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('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 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 confirm_screen(con, message, screen_height, screen_width, confirm_key_message='Press [y] or [Enter] to confirm.', confirm_keys=[ord('y'), libtcod.KEY_ENTER], height=4, game=None): ##============================================================================ if len(message) > len(confirm_key_message): width = len(message) + 2 else: width = len(confirm_key_message) + 2 height = height window = game.gEngine.console_new(width, height) r, g, b = libtcod.white game.gEngine.console_set_default_foreground(window, r, g, b) game.gEngine.console_print_frame(window, 0, 0, width, height, True) game.gEngine.console_set_alignment(window, libtcod.CENTER) game.gEngine.console_print(window, width / 2, 1, message) game.gEngine.console_print(window, width / 2, height - 1, confirm_key_message) x = screen_width / 2 - width / 2 y = screen_height / 2 - height / 4 game.gEngine.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 1.0) game.gEngine.console_flush() key = libtcod.console_wait_for_keypress(True) if key.c in confirm_keys or key.vk in confirm_keys: game.gEngine.console_remove_console(window) return True else: game.gEngine.console_remove_console(window) return False
def handle_keys(): #key = libtcod.console_check_for_keypress() #real-time key = libtcod.console_wait_for_keypress(True) #turn-based if key.vk == libtcod.KEY_ENTER and key.lalt: #Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif key.vk == libtcod.KEY_ESCAPE: return True #exit game #movement keys if libtcod.console_is_key_pressed(libtcod.KEY_UP): player.move(0, -1) fov_recompute = True elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN): player.move(0, 1) fov_recompute = True elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT): player.move(-1, 0) fov_recompute = True elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT): player.move(1, 0) fov_recompute = True
def handle_keys(): global playerx, playery key = libtcod.console_wait_for_keypress(True) #key = libtcod.console_check_for_keypress() #alt-enter fullscreens if key.vk == libtcod.KEY_ENTER and key.lalt: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) #escape exits game elif key.vk == libtcod.KEY_ESCAPE: return True #movement key handling if libtcod.console_is_key_pressed(libtcod.KEY_UP): player.move(0, -1) elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN): player.move(0, 1) elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT): player.move(-1, 0) elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT): player.move(1, 0)
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 handle_keys(): global player_x, player_y key = libtcod.console_wait_for_keypress(True) if key.vk == libtcod.KEY_ENTER and key.lalt: # Alt + Enter: Toggle full_screen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif key.vk == libtcod.KEY_ESCAPE: return True if key.vk == libtcod.KEY_CHAR: if key.c == ord('k'): player.move(0, -1) elif key.c == ord('j'): player.move(0, 1) elif key.c == ord('h'): player.move(-1, 0) elif key.c == ord('l'): player.move(1, 0) elif key.c == ord('y'): player.move(-1, -1) elif key.c == ord('u'): player.move(-1, 1) elif key.c == ord('b'): player.move(-1, 1) elif key.c == ord('n'): player.move(1, 1)
def handle_keys(): global playerx, playery key = libtcod.console_wait_for_keypress(True) #fullscreen toggle if key.vk == libtcod.KEY_ENTER and key.lalt: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) #exit game elif key.vk == libtcod.KEY_ESCAPE: return True #movement keys if libtcod.console_is_key_pressed(libtcod.KEY_UP): playery -= 1 elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN): playery += 1 elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT): playerx -= 1 elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT): playerx += 1
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 handle_inputs(): global player_x, player_y # Window key = libtcod.console_wait_for_keypress(True) # Block! if key.vk == libtcod.KEY_ENTER and key.lalt: # Toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif key.vk == libtcod.KEY_ESCAPE: # Exit return True # Movement if libtcod.console_is_key_pressed(libtcod.KEY_UP): config.player.move(0, -1) config.recompute_fov_flag = True elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN): config.player.move(0, 1) config.recompute_fov_flag = True elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT): config.player.move(-1, 0) config.recompute_fov_flag = True elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT): config.player.move(1, 0) config.recompute_fov_flag = True return False
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 handle_keys(): global playerx, playery global fov_recompute 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()) elif key.vk == libtcod.KEY_ESCAPE: #Exit the game return True #movement keys if libtcod.console_is_key_pressed(libtcod.KEY_UP): player.move(0, -1) fov_recompute = True #playery -= 1 elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN): player.move(0, 1) fov_recompute = True #playery += 1 elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT): player.move(-1, 0) fov_recompute = True #playerx -= 1 elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT): player.move(1, 0) fov_recompute = True
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') # 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 return_key(): key_pressed = "null" key = libtcod.console_wait_for_keypress(True) #Check All The Arrow Keys if libtcod.console_is_key_pressed(libtcod.KEY_UP): key_pressed = "KEY_UP" elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN): key_pressed = "KEY_DOWN" elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT): key_pressed = "KEY_RIGHT" elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT): key_pressed = "KEY_LEFT" #Check for Character Keys key_char = chr(key.c) if key_char == 'k': key_pressed = "k" elif key_char =='i': key_pressed = "i" elif key_char == '1': key_pressed = '1' return key_pressed
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 update(self): key = libtcod.console_wait_for_keypress(True) if libtcod.console_is_key_pressed(libtcod.KEY_ESCAPE): if self.menu_stack == []: return True self.current_menu = self.menu_stack[-1] self.menu_choice = util.generic_index(lambda x: x[0] == self.stack[-1], self.current_menu) self.menu_stack = self.menu_stack[:-1] self.stack = self.stack[:-1] elif libtcod.console_is_key_pressed(libtcod.KEY_UP): if not self.menu_choice == 0: self.menu_choice -= 1 elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN): if not self.menu_choice == (len(self.current_menu) - 1): self.menu_choice += 1 elif libtcod.console_is_key_pressed(libtcod.KEY_ENTER): choice = self.current_menu[self.menu_choice] if type(choice) is list: self.menu_choice = 0 self.menu_stack.append(self.current_menu) self.current_menu = choice[1:] self.stack.append(choice[0]) else: self.stack.append(choice) resolve_case(self, self.stack)
def show_popup(text, title): global popup lines = [ string for line in text.split('\n') for string in textwrap.wrap(line, width=MAX_MAP_WIDTH - 4) ] width = max( len(string) for string in lines ) + 4 # 1 space on each side, plus 1 line for the square on each side width = max(width, len(title)) height = len(lines) + 4 # ditto x = (MAX_MAP_WIDTH - width) // 2 y = (MAX_MAP_HEIGHT - height) // 2 tcod.console_print_frame(popup, x, y, width, height, fmt=title) row = 0 for line in lines: tcod.console_print(popup, x + 2, y + 2 + row, line) row += 1 tcod.console_blit(popup, 0, 0, tcod.console_get_width(popup), tcod.console_get_height(popup), 0, 0, 0) tcod.console_flush() key = tcod.console_wait_for_keypress(True) # destroy the popup tcod.console_blit(console, 0, 0, tcod.console_get_width(console), tcod.console_get_height(console), 0, 0, 0) tcod.console_flush() return key
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 handle_keys(): global playerx, playery #key = libtcod.console_check_for_keypress() #real-time key = libtcod.console_wait_for_keypress(True) #turn-based if key.vk == libtcod.KEY_ENTER and key.lalt: #Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif key.vk == libtcod.KEY_ESCAPE: return True #exit game #movement keys if libtcod.console_is_key_pressed(libtcod.KEY_UP): playery -= 1 elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN): playery += 1 elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT): playerx -= 1 elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT): playerx += 1
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 handle_keys(): global fov_recompute #key = libtcod.console_check_for_keypress() #real-time key = libtcod.console_wait_for_keypress(True) #turn-based if key.vk == libtcod.KEY_ENTER and key.lalt: #Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif key.vk == libtcod.KEY_ESCAPE: return True #exit game # Movement keys by arrows. # New for module 4: fov_recompute will force a recalculation every movement. if libtcod.console_is_key_pressed(libtcod.KEY_UP): #north player.move(0, -1) fov_recompute = True elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN): #south player.move (0, 1) fov_recompute = True elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT): #west player.move (-1, 0) fov_recompute = True elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT): #east player.move (1, 0) fov_recompute = True
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 handle_keys(): global playerx, playery 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()) elif key.vk == libtcod.KEY_ESCAPE: return True # exit game # movement keys if libtcod.console_is_key_pressed(libtcod.KEY_UP) and playery != 1: playery -= 1 elif libtcod.console_is_key_pressed( libtcod.KEY_DOWN) and playery != SCREEN_HEIGHT - 22: playery += 1 elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT) and playerx != 1: playerx -= 1 elif libtcod.console_is_key_pressed( libtcod.KEY_RIGHT) and playerx != SCREEN_WIDTH - 22: playerx += 1
def handle_keys(): #key = libtcod.console_check_for_keypress() #real-time key = libtcod.console_wait_for_keypress(True) #turn-based if key.vk == libtcod.KEY_ENTER and key.lalt: #Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif key.vk == libtcod.KEY_ESCAPE: return 'exit' #exit game if game_state == 'playing': #movement keys if libtcod.console_is_key_pressed(libtcod.KEY_UP): player_move_or_attack(0, -1) elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN): player_move_or_attack(0, 1) elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT): player_move_or_attack(-1, 0) elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT): player_move_or_attack(1, 0) else: return 'didnt-take-turn'
def keyboard_input(player, con): global fog_of_war key = libtcod.console_wait_for_keypress(True) if key.vk == libtcod.KEY_ESCAPE: return True if key.vk == libtcod.KEY_SPACE: fog_of_war = not fog_of_war erase_map(con) if libtcod.console_is_key_pressed(libtcod.KEY_UP): player.move(0, -1) if libtcod.console_is_key_pressed(libtcod.KEY_DOWN): player.move(0, 1) if libtcod.console_is_key_pressed(libtcod.KEY_LEFT): player.move(-1, 0) if libtcod.console_is_key_pressed(libtcod.KEY_RIGHT): player.move(1, 0) return False
def get_choice(self, title=''): if not self.items: return None rows = ([title, ''] if title else []) char = ord('a') for i in self.items: rows.append("%s - %s" % (chr(char), i.describe())) char += 1 rows.append('') rows.append("ESC to abort") self.screen.show_aux("\n".join(rows)) key = T.console_wait_for_keypress(True) if key.vk == T.KEY_ESCAPE: return False if key.c < ord('a') or key.c > (char - 1): self.screen.add_message("Invalid choice %s" % chr(key.c)) return False return self.items[(key.c - ord('a'))]
def handle_keys(): global fov_recompute # key = libtcod.console_check_for_keypress() #real-time key = libtcod.console_wait_for_keypress(True) # turn-based if key.vk == libtcod.KEY_ENTER and key.lalt: # Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif key.vk == libtcod.KEY_ESCAPE: return True # exit game # movement keys if libtcod.console_is_key_pressed(libtcod.KEY_UP): player.move(0, -1) fov_recompute = True elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN): player.move(0, 1) fov_recompute = True elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT): player.move(-1, 0) fov_recompute = True elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT): player.move(1, 0) fov_recompute = True
def handle_keys(self): key = libtcod.console_wait_for_keypress(True) if key.vk == libtcod.KEY_ESCAPE: return 'exit' elif key.vk == libtcod.KEY_ENTER and libtcod.KEY_ALT: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif key.vk == libtcod.KEY_CONTROL and ord('s'): libtcod.sys_save_screenshot() move = False if self.mode == 'playing': if libtcod.console_is_key_pressed(libtcod.KEY_UP): self.move_player((0, -1)) move = True return 'move' elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN): self.move_player((0, 1)) move = True return 'move' elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT): self.move_player((-1, 0)) move = True return 'move' elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT): self.move_player((1, 0)) move = True return 'move' if move: return 'move' else: return "didnt-take-turn"
def handle_keys(): dx=0 dy=0 key = libtcod.console_wait_for_keypress(True); if libtcod.console_is_key_pressed(libtcod.KEY_UP): dy=-1 if not map[player.x+dx][player.y+dy].blocked: player.move(0, -1) elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN): dy=1 if not map[player.x+dx][player.y+dy].blocked: player.move(0, 1) elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT): dx=-1 if not map[player.x+dx][player.y+dy].blocked: player.move(-1, 0) elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT): dx=1 if not map[player.x+dx][player.y+dy].blocked: player.move(1, 0) if key.vk == libtcod.KEY_ESCAPE: return True
def handle_keys(): global playerx, playery #this is blocking, console_check_for_keypress() is nonblocking key = libtcod.console_wait_for_keypress(True) #exit if key.vk == libtcod.KEY_ESCAPE: return True #Alt+Enter: toggle fullscreen elif key.vk == libtcod.KEY_ENTER and key.lalt: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) #movement keys # checked this way so you can just hold it down elif libtcod.console_is_key_pressed(libtcod.KEY_UP): playery -= 1 elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN): playery += 1 elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT): playerx -= 1 elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT): playerx += 1
def handle_keys(): """ Handless the movement of the player. The function console_wait_for_keypress blocks all actions until the player makes a move. """ global fov_recompute #blocks the game and awaits input key = libtcod.console_wait_for_keypress(True) # handle setting keys, in this case handle escape or alt+enter # to toggle fullscreen if key.vk == libtcod.KEY_ENTER and key.lalt: libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif key.vk == libtcod.KEY_ESCAPE: return 'exit' if game_state == 'playing': #handle player movement if libtcod.console_is_key_pressed(libtcod.KEY_UP): player_move_or_attack(0, -1) elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN): player_move_or_attack(0, 1) elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT): player_move_or_attack(-1, 0) elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT): player_move_or_attack(1, 0) else: return 'didnt-take-turn'
def get_choice(self, title=""): if not self.items: return None rows = [title, ""] if title else [] char = ord("a") for i in self.items: rows.append("%s - %s" % (chr(char), i.describe())) char += 1 rows.append("") rows.append("ESC to abort") self.screen.show_aux("\n".join(rows)) key = T.console_wait_for_keypress(True) if key.vk == T.KEY_ESCAPE: return False if key.c < ord("a") or key.c > (char - 1): self.screen.add_message("Invalid choice %s" % chr(key.c)) return False return self.items[(key.c - ord("a"))]
def choice_menu(self, header, err_msg, width, params=[]): header += "\n" err_msg += "\n" ordered_params = [] if not self.elements: options = [err_msg] else: # correspond the order of params to the order of options ordered_elements = [element for element in self.elements.itervalues()] options = [element.name for element in ordered_elements] for param in params: # currently supports only one param ordered_params = [eval("element."+param) for element in ordered_elements] display.menu(header, options, width, ordered_params) key = libtcod.console_wait_for_keypress(True) #if key.c == ord('D'): # self.desc_menu(header, err_msg, width) #make menu disappear after choice is made display.render_all() #convert the ASCII code to an index index = key.c - ord('a') #if something legitimate was chosen, return it if index >= 0 and index < len(options): if self.elements: return self.elements.values()[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