def show_player_stats(): player_stats=libtcod.console_new(20,20) libtcod.console_set_default_background(player_stats,libtcod.darkest_grey) libtcod.console_set_default_foreground(player_stats, libtcod.white) # libtcod.console_clear(player_stats) libtcod.console_print_frame(player_stats, 0, 0, libtcod.console_get_width(player_stats), libtcod.console_get_height(player_stats), clear=True) height=0 libtcod.console_print_rect(player_stats, 1, 1, libtcod.console_get_width(player_stats)-2, libtcod.console_get_height(player_stats)-2, "Name: %s \nHealth: %s/%s\nView distance: %s\nStrength: %s\nTo hit: %s\nExp: %s"# %(P.player.name,P.player.health,P.player.max_health, P.player.view_distance, P.player.strength,P.player.to_hit,P.player.exp)) libtcod.console_print_ex(player_stats, libtcod.console_get_width(player_stats)//2, 0, libtcod.BKGND_DEFAULT, libtcod.CENTER, "Player Stats") libtcod.console_print_ex(player_stats, libtcod.console_get_width(player_stats)//2, libtcod.console_get_height(player_stats)-1, libtcod.BKGND_DEFAULT, libtcod.CENTER, "[spacebar]") libtcod.console_blit(player_stats,0,0, libtcod.console_get_width(player_stats), libtcod.console_get_height(player_stats), 0,5,5, 1.0,0.1) key = libtcod.console_check_for_keypress(libtcod.KEY_PRESSED) while not (libtcod.KEY_SPACE==key.vk): key = libtcod.console_check_for_keypress(libtcod.KEY_PRESSED) libtcod.console_blit(player_stats,0,0, libtcod.console_get_width(player_stats), libtcod.console_get_height(player_stats), 0,5,5, 1.0,0.1) libtcod.console_flush() R.render()
def getkey(self): """ Get a key. Manages user input, using non-blocking methods. Returns: ASCII code for an alphanumeric key, else libtcod code """ key = libtcod.console_check_for_keypress(True) if key.vk == libtcod.KEY_NONE: return None if not key.vk == libtcod.KEY_CHAR: return key.vk return chr(key.c)
def handle_keys(self): key = libtcod.console_check_for_keypress(libtcod.KEY_PRESSED | libtcod.KEY_RELEASED) if key.vk == libtcod.KEY_ESCAPE: return 'exit' elif key.vk == libtcod.KEY_CHAR: if key.pressed: self.pressed.add(key.c) else: try: self.pressed.remove(key.c) except KeyError: pass if ord('w') in self.pressed: self.screen.move(0, -1) elif ord('s') in self.pressed: self.screen.move(0, 1) elif ord('a') in self.pressed: self.screen.move(-1, 0) elif ord('d') in self.pressed: self.screen.move(1, 0) if self.state == 'playing': if libtcod.console_is_key_pressed(libtcod.KEY_UP): self.player.move(0, -1) self.fov_recompute = True elif libtcod.console_is_key_pressed(libtcod.KEY_DOWN): self.player.move(0, 1) self.fov_recompute = True elif libtcod.console_is_key_pressed(libtcod.KEY_LEFT): self.player.move(-1, 0) self.fov_recompute = True elif libtcod.console_is_key_pressed(libtcod.KEY_RIGHT): self.player.move(1, 0) self.fov_recompute = True else: return None return 'action'
def handle_keys(self): if self.st.server_response: # si el servidor ya nos ha respondido key = libtcod.console_check_for_keypress() # esperamos la pulsación if not self.clientChat.enabled: # si el chat no esta en marcha if key.vk == libtcod.KEY_ESCAPE: # salimos del juego utils.send_msg(self.clientSock, key) self.st.server_response = False return 0 elif key.vk == libtcod.KEY_UP or key.vk == libtcod.KEY_DOWN or key.vk == libtcod.KEY_LEFT or key.vk == libtcod.KEY_RIGHT: self.st.server_response = False elif key.c == 99: # si se ha pulsado la 'c' self.clientChat.enabled = True # habilitamos el chat utils.send_msg(self.clientSock, key) # mandamos la pulsación else: # si el chat está habilitado if key.vk == libtcod.KEY_ENTER: # mandamos la cadena si no está vacía if self.clientChat.string != "": utils.send_msg(self.clientSock, self.clientChat.name+self.clientChat.string) self.st.server_response = False self.clientChat.reset() elif key.c != 0: # escribimos en el chatbox self.clientChat.buffer(key.c) return 2
def item_selector(items, default=None, equipped=[], title="INVENTORY"): libtcod.console_clear(cons.menu_console) libtcod.console_set_default_background(cons.menu_console, libtcod.black) libtcod.console_set_default_foreground(cons.menu_console, libtcod.white) libtcod.console_rect(cons.menu_console, 0, 0, M.MAP_WIDTH, M.MAP_HEIGHT, True) libtcod.console_print_ex(cons.menu_console, 40, 0, libtcod.BKGND_NONE, libtcod.CENTER, title) libtcod.console_print_ex(cons.menu_console, 1, M.SCREEN_HEIGHT-1, libtcod.LEFT, libtcod.BKGND_NONE, "[j / k]: Highlight item [SPACEBAR]: Select [q]: quit") count = 0 for item in items: libtcod.console_print_ex(cons.menu_console, 1, count+3, libtcod.BKGND_NONE, libtcod.LEFT, item.name) if item in equipped: libtcod.console_print_ex(cons.menu_console, libtcod.console_get_width(cons.menu_console)-1, count+3, libtcod.BKGND_NONE, libtcod.RIGHT, "(EQUIPPED)") count = count + 1 if default: count = items.index(default) else: count = count -1 key = libtcod.console_check_for_keypress(True) while not key.vk == libtcod.KEY_SPACE and not ord('q') == key.c: for i in range(len(items[count].name)): libtcod.console_set_char_background(cons.menu_console, i+1, count+3, libtcod.white) libtcod.console_set_char_foreground(cons.menu_console, i+1, count+3, libtcod.black) if key.pressed and key.c == ord('k') and count > 0: for i in range(len(items[count].name)): libtcod.console_set_char_background(cons.menu_console, i+1, count+3, libtcod.black) libtcod.console_set_char_foreground(cons.menu_console, i+1, count+3, libtcod.white) count = count -1 elif key.pressed and key.c == ord('j') and count < len(items)-1: for i in range(len(items[count].name)): libtcod.console_set_char_background(cons.menu_console, i+1, count+3, libtcod.black) libtcod.console_set_char_foreground(cons.menu_console, i+1, count+3, libtcod.white) count = count +1 key = libtcod.console_check_for_keypress(True) libtcod.console_blit(cons.menu_console,0,0,M.SCREEN_WIDTH,M.SCREEN_HEIGHT,0,0,0,1) libtcod.console_flush() if ord('q') == key.c: count=-1 return count
def handle(self): key = libtcod.console_check_for_keypress(True) # True for turn-based for input_handler in self.input_handlers: result = input_handler.handle(key) return result
def check_key(self): event = libtcod.console_check_for_keypress(libtcod.KEY_PRESSED) return self._interpret_event(event)
# Contains a list of items on the player's current tile tile_items = list() # This is the first run of the game. firstRun = True turncount = 0 ## Changes the keyboard repeat delay. # libtcod.console_set_keyboard_repeat(0, 0) ############### ## Game loop ## ############### while not libtcod.console_is_window_closed (): if not firstRun: key = libtcod.console_check_for_keypress(libtcod.KEY_PRESSED) #key = libtcod.console_wait_for_keypress(True) else: # If this is the first run of the game, then build a game world and place # the player on it. key = libtcod.console_check_for_keypress() # M.gameworld = [[Tile(floor=False) for y in range (M.MAP_HEIGHT)] # for x in range (M.MAP_WIDTH)] bsp = bspgen.Bsp(M.MAP_WIDTH,M.MAP_HEIGHT, M.gameworld) bsp.render_bsp() place_player() add_items() S.display_status() R.render() draw_items() clear_items()