def edit_mode(self): self.state.game_state = Constants.PAUSE self.state.game_map.game_maps[self.state.game_map.game_map_id][self.state.get_target_x()][self.state.get_target_y()].targeted = True while not libtcod.console_is_window_closed(): self.state.turn += 1 Util.render_all(self.state) libtcod.console_flush() Input.handle_keys(self.state, False) if self.state.game_state == Constants.PLAYING: self.play_game()
def play_game(self): self.state.game_state = Constants.PLAYING self.state.game_map.game_maps[self.state.game_map.game_map_id][self.state.get_target_x()][self.state.get_target_y()].targeted = False self.state.game_map.game_maps[self.state.game_map.previous_map_id][self.state.get_target_x()][self.state.get_target_y()].targeted = False while not libtcod.console_is_window_closed(): sleep(0.20) self.state.turn += 1 Util.render_all(self.state) libtcod.console_flush() Input.handle_keys(self.state, True) if self.state.game_state == Constants.PAUSE: self.edit_mode() self.state.game_map.process_map(self.state)
def display_menu(self, header, options, width, con, override_height=None, option_char=True): 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, MapConstants.SCREEN_HEIGHT, header) if header == "": header_height = 0 height = len(options) + header_height + override_height if override_height: height = override_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: if option_char: text = "(" + chr(letter_index) + ") " + str(option_text) else: text = str(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 = MapConstants.SCREEN_WIDTH / 2 - width / 2 y = MapConstants.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) 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 play_game(self): self.state.set_game_state(Constants.PLAYING) self.state.set_player_action(None) self.state.fov_recompute = True while not libtcod.console_is_window_closed(): self.state.turn += 1 Util.render_all(self.state) libtcod.console_flush() Util.check_level_up(self.state) for object in self.state.objects: object.clear(self.state.con) self.state.set_player_action(Constants.DID_NOT_TAKE_TURN) while self.state.get_player_action() == Constants.DID_NOT_TAKE_TURN: Input.handle_keys(self.state) player_action = self.state.get_player_action() if player_action == Constants.EXIT or self.state.player.color == libtcod.dark_red: self.save_game() break if self.state.get_game_state() == Constants.PLAYING and self.state.get_player_action() != Constants.DID_NOT_TAKE_TURN: AiUtils.dijkstra_on_map(self.state, self.state.player.x, self.state.player.y) monsters_still_alive = False for object in self.state.objects: if object.ai: monsters_still_alive = True object.ai.take_turn(self.state) if not monsters_still_alive and self.state.game_type == Constants.BATTLE: old_player_coords = self.state.player.x, self.state.player.y self.state.game_map.generate_battle_map(self.state) self.state.player.x, self.state.player.y = old_player_coords if player_action == Constants.NEXT_LEVEL: self.next_level() elif player_action == Constants.PREVIOUS_LEVEL: self.previous_level() elif player_action == Constants.EXIT or self.state.player.color == libtcod.dark_red: Util.render_all(self.state) self.save_game() break self.state.status_panel.message('###### Turn ' + str(self.state.turn) + ' has ended')
def refresh(state): state.fov_recompute = True Util.render_all(state) libtcod.console_flush() for object in state.objects: object.clear(state.con)