def exit_building(self): if not self.inside_building: print('You are not in a building!') else: if self.city.blocks[self.block_location].has_badguy: print('Where do you think you are going?') print('Now stay and fight, coward!') else: self.inside_building = False os.system("clear") maps.print_map(self) self.print_location()
def game(): """Initialaze and handles gameplay """ plot.prologue() start_time = time.time() player = maps.START[:] level = 1 map_ = maps.load_map_for_level(level) character.create_character() health = character.get_stats()["HP"] game_over = False while health > 0 and not game_over: os.system('clear') maps.print_map(map_, level, player) player = functions.player_move(map_, level, player) if functions.is_next_level(level, player): level += 1 map_ = maps.load_map_for_level(level) if inventory.item_in_inventory("Sword of a Thousand Truths"): game_over = True health = character.get_stats()["HP"] game_total_time = int(time.time() - start_time) if health < 0: plot.bye_bye() if health > 0: plot.epilogue() highscore.update_highscore(game_total_time) highscore.print_highscore()
def start_game(sock): t = threading.Thread(target=wait_for_ready, args=[sock]) t.start() end = False while not end: data = recv_data_json(sock) if "end" in data: end = data["end"] if "map" in data: print_map(data["map"]) if "action" in data: t = threading.Thread(target=ask_input, args=[sock, data["msg"], data["action"]]) t.start() continue if "msg" in data: print(data["msg"] + '\n')
def move_in_direction(self, direction): self.direction = direction # Ninja can't move within city when ninja is inside building. if self.inside_building: print(text.warn_exit_building) # Ninja can't move beyond edges of city. elif direction == 'N': if self.ninja_on_edge(maps.north_edge): print(text.warn_invalid_direction) else: self.block_location = self.block_location - 3 os.system("clear") maps.print_map(self) self.print_location() elif direction == 'E': if self.ninja_on_edge(maps.east_edge): print(text.warn_invalid_direction) else: self.block_location = self.block_location + 1 os.system("clear") maps.print_map(self) self.print_location() elif direction == 'S': if self.ninja_on_edge(maps.south_edge): print(text.warn_invalid_direction) else: self.block_location = self.block_location + 3 os.system("clear") maps.print_map(self) self.print_location() elif direction == 'W': if self.ninja_on_edge(maps.west_edge): print(text.warn_invalid_direction) else: self.block_location = self.block_location - 1 os.system("clear") maps.print_map(self) self.print_location()
def init_mygame(): global configuration random.seed(configuration['seed']) image_files = dict() for tilekey, tiledict in configuration['maptiles'].iteritems(): image_files[tilekey] = tiledict['img'] for tilekey, tiledict in configuration['agentTiles'].iteritems(): image_files[tilekey] = tiledict aiBaseName = configuration['agentBaseTile'] # This must be consistent with the agent base locations state = {'prev_pos': configuration['agentInit']} state['inPause'] = False state['step'] = False plan = [] debugMap = configuration['debugMap'] # FSM fsm_state = 'init' steps_in_state = 0 direccion_guardia = 'north' # map if configuration['type'] == 'random': map = maps.create_map(configuration, state, configuration['debug']) else: map, configuration = maps.read_map(configuration) # display screen_size = [ configuration['map_size'][0] * tile_size, configuration['map_size'][1] * tile_size + text_size ] screen = pygame.display.set_mode(screen_size) images = { f: pygame.transform.scale( pygame.image.load(image_files[f]).convert(), (tile_size - 5, tile_size - 5)) for f in image_files } state['prev_pos'] = configuration['agentInit'] if configuration['save']: with open(configuration['file'], 'w') as f: f.write(maps.printable_map(map, configuration, False)) maps.print_map(map, configuration, images, screen, state, tile_size, fsm_state, configuration['debug'], "Running search") # Used to manage how fast the screen updates clock = pygame.time.Clock() # planner header = "" # --- In the IA201718 configuration we have planned in advance for the objective if ai201718: global aiPlan global aiMapText aiPlan, problem, result, use_viewer = searchSolution( map, configuration, state, aiBaseName, debugMap) if aiPlan: aiMapText = searchInfo(problem, result, use_viewer) print( "----------------------- STARTING SEARCH ---------------------" ) print("Retrieved a plan: {0}".format(aiPlan)) state['searchOk'] = True done = False else: aiMapText = "Search retrieved no plan for this problem" print("Search retrieved no plan for this problem") state['searchOk'] = False return state, plan, screen_size, screen, images, map, configuration, clock, header, fsm_state, steps_in_state, direccion_guardia
def init_game(): global aiPlan # This variable stores the calculated solution global aiMapText # This variable stores a text obtained during the search pygame.init() cycle = 0 winner = False state, plan, screen_size, screen, images, mapa, configuration, clock, header, fsm_state, steps_in_state, direccion_guardia = init_mygame( ) # If initialization returned state = None, then we are done done = not state['searchOk'] if configuration['debugMap']: print("-------------- INITIAL MAP -------------") print(mapa) # -------- Main Program Loop ----------- state['inPause'] = True displayText = aiMapText while not done: # --- Main event loop cycle = cycle + 1 done, state = read_events(configuration, state) if done: continue new_pos = list(state['prev_pos']) # --- Game logic should go here # --- In the IA201718 configuration we have planned in advance for the objective # --- The plan is stored in a global (aiPlan) if ai201718 and len(aiPlan) > 0 and not state['inPause']: nextElement = aiPlan.pop(0) nextAction = nextElement[0] new_pos, state = plan_move_agent(nextAction, mapa, state, configuration, new_pos, configuration['debug']) nextActionData = nextElement[1] # This field is reserved for plan step attributes (NOT USED YET) displayText = aiMapText if 'showText' in nextActionData.keys(): displayText = nextActionData['showText'] + '\n' + aiMapText # These are the effects of the movement state, mapa, new_pos = do_move_agent(state, mapa, new_pos) state['prev_pos'] = new_pos # --- Checking finish simulation done, winner = check_finish(state, configuration) # --- NPCs changes mapa, state, fsm_state, steps_in_state, direccion_guardia = move_npc( mapa, state, configuration, new_pos, fsm_state, steps_in_state, direccion_guardia, configuration['debug']) # --- Drawing code maps.print_map(mapa, configuration, images, screen, state, tile_size, fsm_state, configuration['debug'], show_text=displayText) # --- Limit to 60 frames per second clock.tick(60) time.sleep(configuration['delay']) if state['step']: state['inPause'] = True if configuration['debugMap']: print("-------------- FINAL MAP -------------") print(mapa) state['inPause'] = True maps.print_map(mapa, configuration, images, screen, state, tile_size, fsm_state, configuration['debug'], show_text=aiMapText) while state['inPause']: done, state = read_events(configuration, state) pygame.quit()
character_hp = 100 elif response.lower() == "evelyn": evelyn_inventory = Inventory('Evelyn', 'Dagger', 'Rotten Apple', 'An old but sharp dagger', '40', '0', 'A commonly found health item', '0', '10') print(evelyn_inventory.__str__()) chosen_character.append('evelyn') character_hp = 100 else: print("That response is not valid. Please type in a valid response") break # print out the location maps maps.print_map(maps.main_map) # Locations locations = ['City Tower', 'Shopping Centre', 'Sewer'] # Prompt for user to choose a location print("Locations:") for location in locations: print(location.title()) prompt_2 = "Choose a location: " def user_action_2(location): """user chooses a location to play the game in""" choice = f"{location}"
def command_mode(self): if self.state.menu: self.menu_mode() # Player won the game. elif self.ninja.win_game: os.system("clear") text.print_logo() print(text.win_message) exit(0) else: # Ninja under attack is turned on when entering buildings with badguys. # Whether they attack is randomly determined. If ninja did not block # as last move, the ninja's health is decremented. if self.ninja.under_attack_on: if logic.get_random_attack() == 1: if not self.ninja.is_blocking: print('YOU ARE HIT BY A FIERCE BLOW!') self.ninja.change_health(0, 1) # Warn when health is down to 1. if self.ninja.health == 1: print('Warning! {} health left!'.format( self.ninja.health)) # Ninja health is at zero. Player loses game. elif self.ninja.health < 1: text.print_logo() print(text.star_line) print( '\nYOU LOSE! All ninjas have bad days, try again!\n' ) print(text.exit_message) exit(0) else: # Ninja was blocking attack. print("Nice block, that would of hurt!") # Reset is_blocking. self.ninja.is_blocking = False # Get, parse, and execute player command. command = self.game_prompt('\nNext move: ') # If command is enter, or nothing, enter menu mode. if command == '' or len(command) == 0: self.state.paused = True self.state.menu = True os.system("clear") else: command = command.upper() # Move North, East, South, or West. if command in ['N', 'E', 'S', 'W']: self.ninja.move_in_direction(command) # Use stars, chucks, or sword. elif command in ['STARS', 'CHAKU', 'SWORD']: self.ninja.change_weapon(command) # Enter building. elif command == 'ENTER': self.ninja.enter_building() # Exit building. elif command == 'EXIT': self.ninja.exit_building() # Attack elif command == 'ATTACK': self.ninja.attack(self.badguys) # Block elif command == 'BLOCK': self.ninja.block_attack() # Status elif command == 'STATUS': os.system("clear") print('\nSTATUS\n{}\n'.format(text.yinyang_line)) self.ninja.print_location() print('\nHEALTH: {}'.format(self.ninja.health)) print('\nWEAPON: {}'.format(self.ninja.weapon)) print('\n{} Bad guys left to defeat!'.format(len( self.badguys))) # Map elif command == 'MAP': os.system("clear") self.ninja.print_location() maps.print_map(self.ninja) # Menu elif command == 'MENU': os.system("clear") self.state.menu = True # Help elif command == 'HELP': text.print_help() else: print('Invalid command. Type help or try again.')