def start_app(gui_func, title): SCREEN_WIDTH = 60 SCREEN_HEIGHT = 50 LIMIT_FPS = 20 fullscreen = False tcod.sys_set_fps(LIMIT_FPS) with tcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, title, fullscreen) as root_console: while not tcod.console_is_window_closed(): root = TcodRootElement() context = gui.Context(root=root, visitor=gui.Visitor(root)) gui.update_ui(context, gui_func) class Renderer: pass renderer = Renderer() renderer.console = root_console constraints = gui.BoxConstraints.from_w_h(SCREEN_WIDTH, SCREEN_HEIGHT) root.layout(constraints) root.draw(renderer, Point(0, 0)) tcod.console_flush() tcod.console_wait_for_keypress(True)
def get_dir_action(level): message("direction?") update_screen(level) key = tcod.console_wait_for_keypress(flush=False) while not is_direction_key(key): if key.vk == 1: # ESC return None key = tcod.console_wait_for_keypress(flush=False) if key.vk in arrows: return arrows[key.vk] elif key.vk == 66 and key.text in "hjklyubn": return vim[key.text] else: return None
def get_id_action(level): message("--- (* to view inventory)") update_screen(level) key = tcod.console_wait_for_keypress(flush=True) while key.vk not in [66, 1]: key = tcod.console_wait_for_keypress(flush=False) if key.text == "*": inventory_menu(level) return get_id_action(level) if key.text in level.player.inventory: return key.text else: message("Unknown item") return None
def handle_keys(currPlayer, mapToUse): global fov_recompute #key = tcod.console_check_for_keypress() #real-time key = tcod.console_wait_for_keypress(True) #turn-based if key.vk == tcod.KEY_ESCAPE: sys.exit() # if key.vk == tcod.KEY_ENTER and key.lalt: # #Alt+Enter: toggle fullscreen # tcod.console_set_fullscreen(not tcod.console_is_fullscreen()) #movement keys if tcod.console_is_key_pressed(tcod.KEY_UP): mapToUse.movePlayer(currPlayer, 0) fov_recompute = True elif tcod.console_is_key_pressed(tcod.KEY_DOWN): mapToUse.movePlayer(currPlayer, 2) fov_recompute = True elif tcod.console_is_key_pressed(tcod.KEY_LEFT): mapToUse.movePlayer(currPlayer, 3) fov_recompute = True elif tcod.console_is_key_pressed(tcod.KEY_RIGHT): mapToUse.movePlayer(currPlayer, 1) fov_recompute = True
def menu(header, options, width): if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options.') #calculate total height for the header (after auto-wrap) and one line per option header_height = libtcod.console_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) #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 fov_recompute #key = tcod.console_check_for_keypress() #real-time key = tcod.console_wait_for_keypress(True) #turn-based if key.vk == tcod.KEY_ENTER and key.lalt: #Alt+Enter: toggle fullscreen tcod.console_set_fullscreen(not tcod.console_is_fullscreen()) elif key.vk == tcod.KEY_ESCAPE: return True #exit game #movement keys if tcod.console_is_key_pressed(tcod.KEY_UP): player.move(0, -1) fov_recompute = True elif tcod.console_is_key_pressed(tcod.KEY_DOWN): player.move(0, 1) fov_recompute = True elif tcod.console_is_key_pressed(tcod.KEY_LEFT): player.move(-1, 0) fov_recompute = True elif tcod.console_is_key_pressed(tcod.KEY_RIGHT): player.move(1, 0) fov_recompute = True
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 menu(header, options, width): if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options.') header_height = tcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header) height = len(options) + header_height + 2 window = tcod.console_new(SCREEN_WIDTH, SCREEN_HEIGHT) tcod.console_set_default_foreground(window, tcod.white) tcod.console_print_rect_ex(window, 0, 1, width, height, tcod.BKGND_NONE, tcod.LEFT, header) y = header_height + 1 letter_index = ord('a') for option_text in options: text = '(' + chr(letter_index) + ') ' + option_text tcod.console_print_ex(window, 0, y, tcod.BKGND_NONE, tcod.LEFT, text) y += 1 letter_index += 1 x = int(SCREEN_WIDTH /2 - width / 2) y = int(SCREEN_HEIGHT /2 - height / 2) tcod.console_blit(window, 0, 0, width, height, 0, x, y - 3, 1.0, 0.7) tcod.console_flush() key = tcod.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 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) 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: 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(): #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 = tcod.console_wait_for_keypress(True) if key.vk == tcod.KEY_ESCAPE: return True if key.vk == tcod.KEY_SPACE: fog_of_war = not fog_of_war eraseMap(con) if tcod.console_is_key_pressed(tcod.KEY_UP): player.move(0, 1) if tcod.console_is_key_pressed(tcod.KEY_DOWN): player.move(0, -1) if tcod.console_is_key_pressed(tcod.KEY_LEFT): player.move(-1, 0) if tcod.console_is_key_pressed(tcod.KEY_RIGHT): player.move(1, 0) return False
def get_key_event(turn_based=None): if turn_based: key = tcod.console_wait_for_keypress(True) else: # Real-time game play; don't wait for a player's key stroke key = tcod.console_check_for_keypress() return key
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_check_for_keypress() #real-time key = libtcodpy.console_wait_for_keypress(True) #turn-based #let monsters take their turn if self.game_state == 'playing' and self.player_action != 'didnt-take-turn': for obj in self.objects: if obj.ai: obj.ai.take_turn(self.game_map, self.player) if self.game_state == 'playing': if key.vk == libtcodpy.KEY_ENTER and key.lalt: #Alt+Enter: toggle fullscreen libtcodpy.console_set_fullscreen(not libtcodpy.console_is_fullscreen()) elif key.vk == libtcodpy.KEY_ESCAPE: return 'exit' #exit game #movement keys if libtcodpy.console_is_key_pressed(libtcodpy.KEY_UP): self.game_map.fov_recompute = self.player.move(0, -1, self.is_blocked_and_target) elif libtcodpy.console_is_key_pressed(libtcodpy.KEY_DOWN): self.game_map.fov_recompute = self.player.move(0, 1, self.is_blocked_and_target) elif libtcodpy.console_is_key_pressed(libtcodpy.KEY_LEFT): self.game_map.fov_recompute = self.player.move(-1, 0, self.is_blocked_and_target) elif libtcodpy.console_is_key_pressed(libtcodpy.KEY_RIGHT): self.game_map.fov_recompute = self.player.move(1, 0, self.is_blocked_and_target) else: return 'didnt-take-turn'
def handle_keys(): # key = tcod.console_check_for_keypress() #real-time key = tcod.console_wait_for_keypress(True) # turn-based if key.vk == tcod.KEY_ESCAPE: return True # exit game else: return True
def popup(con, message, width, height): dismiss = False while not dismiss: message_box(con, message, 50, width, height) libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) action = handle_popup(key) dismiss = action.get('dismiss')
def get_user_input(): key = tcod.console_wait_for_keypress(flush=False) if key.vk in arrows: return arrows[key.vk] elif key.vk == 66 and key.text in vim: return vim[key.text] elif key.vk == 1: return "ESCAPE" elif key.vk == 4 and (key.lalt or key.ralt): return "FULLSCREEN"
def handle_keys(): key = tcod.console_wait_for_keypress(True) key = key.vk if key.vk is not tcod.KEY_CHAR else chr(key.c) player = game.player GAME.last_player_action = PlayerAction.OTHER_ACTION if key == tcod.KEY_ESCAPE: pass if key in game.controls.actions['up']: if GAME.game_state == GameState.PLAYING: x = 0 y = -1 player.move_or_attack(x, y) elif key in game.controls.actions['down']: if GAME.game_state == GameState.PLAYING: x = 0 y = 1 player.move_or_attack(x, y) elif key in game.controls.actions['left']: if GAME.game_state == GameState.PLAYING: x = -1 y = 0 player.move_or_attack(x, y) elif key in game.controls.actions['right']: if GAME.game_state == GameState.PLAYING: x = 1 y = 0 player.move_or_attack(x, y) elif key in game.controls.actions['up_left']: if GAME.game_state == GameState.PLAYING: x = -1 y = -1 player.move_or_attack(x, y) elif key in game.controls.actions['up_right']: if GAME.game_state == GameState.PLAYING: x = 1 y = -1 player.move_or_attack(x, y) elif key in game.controls.actions['down_left']: if GAME.game_state == GameState.PLAYING: x = -1 y = 1 player.move_or_attack(x, y) elif key in game.controls.actions['down_right']: if GAME.game_state == GameState.PLAYING: x = 1 y = 1 player.move_or_attack(x, y) elif key in game.controls.actions['wait']: if GAME.game_state == GameState.PLAYING: GAME.last_player_action = PlayerAction.TAKE_TURN
def render_menu(self, header, options, width): header_height = self.owner.console.get_height_rect( 0, 0, width, config.SCREEN_HEIGHT, header) height = len(options) + header_height window = tcod.console.Console(width, height) window.default_fg = tcod.white window.print_rect(0, 0, width, height, string=header, bg_blend=tcod.BKGND_NONE, alignment=tcod.LEFT) y = header_height letter_index = ord('a') for option_text in options: text = '(' + chr(letter_index) + ') ' + option_text window.print(0, y, string=text, bg_blend=tcod.BKGND_SET, alignment=tcod.LEFT) y += 1 letter_index += 1 x = int(config.SCREEN_WIDTH / 2 - width / 2) y = int(config.SCREEN_HEIGHT / 2 - height / 2) window.blit(self.owner.root_console, dest_x=x, dest_y=y, src_x=0, src_y=0, width=width, height=height, fg_alpha=1.0, bg_alpha=0.7) tcod.console_flush() tcod.console_wait_for_keypress(True)
def menu(header, options, width): if len(options) > 26: raise ValueError("Can't have more than 26 options in popup menu") fmt = "\n".join([header.center(width)+"\n"] + options + ["\n" + "--- press space to continue ---".center(width)]) height = len(fmt.splitlines()) + 3 popup = tcod.console_new(width, height) tcod.console_set_default_background(popup, (0, 0, 20)) tcod.console_clear(popup) tcod.console_print_rect(popup, 1, 1, width+2, height+2, fmt=fmt) tcod.console_blit(src=popup, x=0, y=0, w=width+2, h=height, dst=root, xdst=SCREEN_WIDTH//2-width//2, ydst= 0, # SCREEN_HEIGHT//2-height//2, ffade=1.0, bfade=0.8) tcod.console_flush() key = tcod.console_wait_for_keypress(flush=True) while key.c != 32: # Space key = tcod.console_wait_for_keypress(flush=True) tcod.console_clear(popup)
def handle_keys(player, game_map): key = tcod.console_wait_for_keypress(True) if key.vk == tcod.KEY_ENTER and key.lalt: tcod.console_set_fullscreen(not tcod.console_is_fullscreen()) elif key.vk == tcod.KEY_ESCAPE: return True if tcod.console_is_key_pressed(tcod.KEY_UP): player.move(*DIR_NORTH, game_map) elif tcod.console_is_key_pressed(tcod.KEY_DOWN): player.move(*DIR_SOUTH, game_map) elif tcod.console_is_key_pressed(tcod.KEY_LEFT): player.move(*DIR_WEST, game_map) elif tcod.console_is_key_pressed(tcod.KEY_RIGHT): player.move(*DIR_EAST, game_map)
def handle_keys(): #key = libtcod.console_check_for_keypress() #real-time key = libtcod.console_wait_for_keypress(True) #turn-based global toggleDisplay global map if key.vk == libtcod.KEY_ALT: # random value for mySeed = random.random() print(mySeed) seed = generate_seed(mySeed) map =make_map(seed) render_all(map) libtcod.console_flush() if key.vk == libtcod.KEY_ENTER: #change color : bi to greys if toggleDisplay == True: toggleDisplay = False else: toggleDisplay = True print('toggleDisplay %s' % toggleDisplay) render_all(map) libtcod.console_flush() #data = generate(simple_sine) 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) 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): """create a menu""" 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(var.CON, 0, 0, width, var.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 = var.SCREEN_WIDTH / 2 - width / 2 y = var.SCREEN_HEIGHT / 2 - height / 2 libtcod.console_blit(window, 0, 0, width, height, 0, int(x), int(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 Player_Input(self): global fov_recompute for ent, (Player, Position, Move, Inventory) in self.world.get_components(Components.Player, Components.Position, Components.Can_Move, Components.Inventory): # Only affects the player. key = tcod.console_wait_for_keypress(True) key_char = chr(key.c) # Four cardinal directions if key.vk == tcod.KEY_KP8: Destination = Position.X, Position.Y - 1 Position.X, Position.Y = self.Collision(Position, Destination) # UP elif key.vk == tcod.KEY_KP2: Destination = Position.X, Position.Y + 1 Position.X, Position.Y = self.Collision(Position, Destination) # Down elif key.vk == tcod.KEY_KP4: Destination = Position.X - 1, Position.Y Position.X, Position.Y = self.Collision(Position, Destination) # Left elif key.vk == tcod.KEY_KP6: Destination = Position.X + 1, Position.Y Position.X, Position.Y = self.Collision(Position, Destination) # Right # Four Diagonal directions elif key.vk == tcod.KEY_KP7: Destination = Position.X - 1, Position.Y - 1 Position.X, Position.Y = self.Collision(Position, Destination) elif key.vk == tcod.KEY_KP9: Destination = Position.X + 1, Position.Y - 1 Position.X, Position.Y = self.Collision(Position, Destination) elif key.vk == tcod.KEY_KP1: Destination = Position.X - 1, Position.Y + 1 Position.X, Position.Y = self.Collision(Position, Destination) elif key.vk == tcod.KEY_KP3: Destination = Position.X + 1, Position.Y + 1 Position.X, Position.Y = self.Collision(Position, Destination) elif key_char == "a": self.Target_Control(Position.X, Position.Y, ent) elif key_char == 'k': self.Pickup(Position.X, Position.Y, Inventory.Inventory) elif key_char == "l": self.Loot_Drop() elif key_char == 't': self.Show_Inventory(Inventory) fov_recompute = True
def main(): init() playing = True wina = Window(0, 0, 10, screenheight) winb = Window(0, 0, screenwidth, screenheight) winc = Window(screenwidth // 2, screenheight // 2, 10, 10) winb.print_(50, 20, "Hello World!") winb.print_(0, 20, "Here I am!") wina.print_(0, 0, "Yo Yo Yo") winc.print_(10, 10, '@@YO@@') for y in range(winc.height): for x in range(winc.width): tcod.console_set_char_background(winc, x, y, tcod.Color(20, 20, 20), tcod.BKGND_SET) while not tcod.console_is_window_closed() and playing: root.clear() winb.flush() wina.flush() winc.flush() flip() key = tcod.console_wait_for_keypress(True) if key.vk == tcod.KEY_ENTER and key.lalt: # Alt+Enter: toggle fullscreen tcod.console_set_fullscreen(not tcod.console_is_fullscreen()) elif key.vk == tcod.KEY_ESCAPE: playing = False return if tcod.console_is_key_pressed(tcod.KEY_UP): winc.y -= 1 elif tcod.console_is_key_pressed(tcod.KEY_DOWN): winc.y += 1 elif tcod.console_is_key_pressed(tcod.KEY_LEFT): winc.x -= 1 elif tcod.console_is_key_pressed(tcod.KEY_RIGHT): winc.x += 1
def processinput(): print(_curmode) key = tcod.console_wait_for_keypress(True) metaflags = ModeFlags.NOFLAG if key.lalt or key.ralt: metaflags |= ModeFlags.ALT if key.shift: metaflags |= ModeFlags.SHIFT if key.lctrl or key.rctrl: metaflags |= ModeFlags.CTRL ch = chr(key.c) if key.vk not in _keytranslate else _keytranslate[key.vk] if (ch, metaflags) not in _handlers[_curmode]: print(f"no keybinding found in mode {_curmode} for {ch}.") else: kh = _handlers[_curmode][(ch, metaflags)] kh.handler(kh.exdata)
def readkey(): while True: key = T.console_wait_for_keypress(False) #print key.vk, repr(chr(key.c)) if key.vk in [T.KEY_SHIFT, T.KEY_CONTROL, T.KEY_ALT, T.KEY_CAPSLOCK, T.KEY_TEXT]: continue if key.c != 0 and chr(key.c) not in '\x1b\n\r\t': s = chr(key.c) if key.shift: # XXX horrible hack if s == '/': s = '?' elif s == '.': s = '>' else: s = s.upper() return s elif key.vk != 0: return key.vk
def menu(header, options, width): if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options.') #calculate total height for the header (after auto-wrap) and one line per option header_height = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header) if header == '': header_height = 0 height = len(options) + header_height #create an off-screen console that represents the menu's window window = libtcod.console_new(width, height) #print the header, with auto-wrap libtcod.console_set_default_foreground(window, libtcod.white) libtcod.console_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, header) #print all the options y = header_height letter_index = ord('a') for option_text in options: text = '(' + chr(letter_index) + ') ' + option_text libtcod.console_print_ex(window, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, text) y += 1 letter_index += 1 #blit the contents of "window" to the root console x = SCREEN_WIDTH//2 - width//2 y = SCREEN_HEIGHT//2 - height//2 libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7) #present the root console to the player and wait for a key-press libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) if key.vk == libtcod.KEY_ENTER and key.lalt: #(special case) Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) #convert the ASCII code to an index; if it corresponds to an option, return it index = key.c - ord('a') if index >= 0 and index < len(options): return index return None
def menu(header, options, width): if len(options) > 26: raise ValueError('CANNOT HAVE MORE THAN 26 OPTIONS YOU SILLY WILLY!!!') #calculate total height for header 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 off-screen console for menu 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 window contents 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 and wait for keypress libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) #convert ascii code to an index, if it is an option, return it index = key.c - ord('a') if index >= 0 and index < len(options): return index return None
def textinput(header, width): #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 = header_height+2 #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) text='' key=None while key==None or key.vk != libtcod.KEY_ENTER: libtcod.console_clear(con) libtcod.console_print_rect_ex(window, 0, 2, width, height, libtcod.BKGND_NONE, libtcod.LEFT, text) #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, 1.0) #present the root console to the player and wait for a key-press libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) text=text+chr(key.c) return text.strip()
def main(): player = Player(1, 1, 'O', tcod.green) tcod.console_set_custom_font( 'arial10x10.png', tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD) tcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'Maze', False, tcod.RENDERER_SDL2) con = tcod.console.Console(MAP_WIDTH, MAP_HEIGHT) tcod.sys_set_fps(LIMIT_FPS) fov_map = tcod.map.Map(MAP_WIDTH, MAP_HEIGHT) global compute_fov compute_fov = False cells = [[Cell() for y in range(MAP_COLS)] for x in range(MAP_ROWS)] makeMap(cells) choice = 0 (wall_set, cells_finished, cells) = prim.init_variables(MAP_ROWS, MAP_COLS) while len(cells_finished) != MAP_ROWS * MAP_COLS: cells = prim.generate_maze(wall_set, cells_finished, cells, MAP_ROWS, MAP_COLS) makeMap(cells) for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): fov_map.walkable[:] fov_map.transparent[:] render(player, con, fov_map, True) tcod.console_blit(con, 0, 0, MAP_WIDTH, MAP_HEIGHT, 0, 0, 0) tcod.console_flush() while not tcod.console_is_window_closed(): if player.xPos == MAP_WIDTH - 2 and player.yPos == MAP_HEIGHT - 2: render(player, con, fov_map, False) tcod.console_put_char_ex(con, 1, 1, 'S', tcod.pink, tcod.black) player.draw(con) tcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0) tcod.console_flush() key = tcod.console_wait_for_keypress(True) break render(player, con, fov_map, fog_of_war) tcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0) tcod.console_flush() player.erase(con) quit = keyboard_input(player, con) if quit: render(player, con, fov_map, True, qui) tcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0) tcod.console_flush() key = tcod.console_wait_for_keypress(True) if key.vk == tcod.KEY_ESCAPE: render(player, con, fov_map, False, qui) player.draw(con) tcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0) tcod.console_flush() key = tcod.console_wait_for_keypress(True) break
print("Month: ", Month) # End Simulation tcod.console_check_for_keypress(True) if tcod.console_is_key_pressed(tcod.KEY_SPACE): timer = 0 isRunning = False print("*PAUSED*") time.sleep(1) # Flush Console if needUpdate: BiomeMap(Chars, Colors) needUpdate = False key = tcod.console_wait_for_keypress(True) # Start Simulation if tcod.console_is_key_pressed(tcod.KEY_SPACE): isRunning = True print("*RUNNING*") time.sleep(1) # Profiler if tcod.console_is_key_pressed(tcod.KEY_ESCAPE): isRunning = False pr.disable() pr.print_stats(sort="time") if key.vk == tcod.KEY_CHAR:
def handle_keys(): global fov_recompute #key = libtcod.console_check_for_keypress() #real-time key = libtcod.console_wait_for_keypress(True) #turn-based key_char = chr(key.c) if key.vk == libtcod.KEY_F11: #Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif key.vk == libtcod.KEY_ESCAPE: return 'exit' r=True #movement keys if game_state == 'playing': if key_char == 'w' or key.vk == libtcod.KEY_KP8: r=player_move_or_attack(0, -1) elif key_char == 's' or key.vk == libtcod.KEY_KP2: r=player_move_or_attack(0, 1) elif key_char == 'a' or key.vk == libtcod.KEY_KP4: r=player_move_or_attack(-1, 0) elif key_char == 'd' or key.vk == libtcod.KEY_KP6: r=player_move_or_attack(1, 0) elif key_char == 'q' or key.vk == libtcod.KEY_KP7: r=player_move_or_attack(-1, -1) elif key_char == 'e' or key.vk == libtcod.KEY_KP9: r=player_move_or_attack(1, -1) elif key_char == 'z' or key.vk == libtcod.KEY_KP1: r=player_move_or_attack(-1, 1) elif key_char == 'c' or key.vk == libtcod.KEY_KP3: r=player_move_or_attack(1, 1) else: #test for other keys if key_char == 'o': #pick up an item upped=False for object in objects: #look for an item in the player's tile if object.x == player.x and object.y == player.y: if object.item: object.item.pick_up() upped=True break if "interact" in object.actions: object.actions["interact"](object) upped=True break if not upped: message('Maassa ei ole otettavaa.',libtcod.dark_red) return 'didnt-take-turn' elif key_char == 'r': #show the inventory; if an item is selected, use it chosen_item = inventory_menu('Paina esineen nappia kuluttaaksesi esineen, tai jotakin muuta peruuttaaksesi.\n') if chosen_item is not None: chosen_item.use() else: return 'didnt-take-turn' elif key_char == 't': #show the inventory; if an item is selected, drop it chosen_item = inventory_menu('Paina esineen nappia tiputtaaksesi esineen, tai jotakin muuta peruuttaaksesi.\n') if chosen_item is not None: chosen_item.drop() else: return 'didnt-take-turn' elif key_char == 'p': #go down stairs, if the player is on them if stairs.x == player.x and stairs.y == player.y: next_level() else: message('Maassa ei ole portaita joita kulkea alas.',libtcod.dark_red) return 'didnt-take-turn' else: message('Nappia ei tunnistettu',libtcod.dark_red) return 'didnt-take-turn' if not r: message('Osut muuriin.',libtcod.dark_red) return 'didnt-take-turn'