def splash_screen(): ''' display a simple splash screen which asks user to load/start game. ''' while True: print '"Load" existing game or "start" new game?' input_string = raw_input("load/start? ") if input_string == 'load' or input_string == 'l': player_game = GameInstance(load=DEFAULT_SAVE_FILE) upper_main(player_game) elif input_string == 'start' or input_string == 's': player_game = GameInstance() upper_main(player_game) # Just so I can test stuff. (Joshua) elif input_string == 'debug' or input_string == 'd': player_game = GameInstance() action_list = ['exit', 'take eggs from fridge', 'look inside fridge', 'go kitchen', 'go hallway'] # ^ Anyone know how I can have this work without it being backwards? refers to action_list.pop() below user_input.opening_setup(player_game,lambda (x): 'Joshua') counter = 1 while True: command = player_game.parser.parse(action_list.pop()) # next command print "%i: %s" % (counter, command.raw) player_game.take_action(command) player_game.check_events() counter += 1 else: invalid_input("Please enter \"load\" or \"start\"", input_string=input_string, tag='bad load/save choice')
def choose_object(item_list, input=default_input): ''' prompts the user to choose an object from the given list ''' grab_object = input("Grab what?") for item in item_list: if grab_object.lower() == item.name.lower(): print "You grabbed it!" item_list.remove(item) return item else: invalid_input("can't find it!", input_string=grab_object, tag='request to grab unknown object', extra='avail_choices='+str(item_list)) return None
def help_info(input=default_input): """Provides a information about the available prompts should the player need it""" help_info_dict = {"go": "This command will move your character to another room of your choice.", "exit": "This command exits the game! Careful!", "stats": "This command provides you with info about your character.", "location": "This command tells you your current location.", "help": "show this menu", } while True: info_choice = input("What would you like to know more about? ('back' returns you to the prompt)").lower() try: print help_info_dict[info_choice] except KeyError: if info_choice == "back": return False else: invalid_input("That's not a valid command! type 'back' to exit help.", input_string=info_choice, tag='unknown help request')
def take_action(self, command, input=user_input.default_input): # This method now uses the Command object returned from the Parser # This means you can call commands with 2 words, e.g. 'look desk' # TODO: # - Move this code to user_input maybe # ^ Any thoughts on this? This code will become very large as we implement more verbs. # Unless we can devise a smart way to handle them all. if command: if command.verb.name == 'exit': self.exit_game() if command.verb.name == 'look': # call look function of object of command if command.object == None: print self.player.current_location.description elif command.object.type == 'error': invalid_input("I don't understand %s" % command.object.name, input_string=command.raw, tag='unknown object error', game=self) elif 'in' in command.object.prepositional_modifiers: self.config_loader.get_by_type_and_name('item', command.object.name).look_in() else: # If there is no object of look it will print the current room's description self.config_loader.get_by_type_and_name('item', command.object.name).look() if command.verb.name == 'go': if command.object != None: try: target_room = self.config_loader.get_by_type_and_name('room', command.object.name) if target_room.name in self.player.current_location.exits: self.player.current_location = target_room else: print 'Cannot go to '+Fore.GREEN+"{}".format(target_room.name) except ValueError as err: if err.message[0:16] == 'Cannot find room': invalid_input(err.message, input_string=command.raw, tag='room not found', game=self) else: raise else: print self.player.current_location.exits travel_location = input("Which Room?") try: self.player.current_location = self.config_loader.get_by_type_and_name('room', self.player.current_location.exits[int(travel_location)-1]) except ValueError: try: self.player.current_location = self.config_loader.get_by_type_and_name('room', travel_location) except ValueError: invalid_input('Place not recognized.', input_string=travel_location, tag='room specified not found', game=self) if command.verb.name == 'stats': print ' ### USER STATS ### ' self.player.player_status() print ' ### GAME STATS ### ' print 'game started : ', self.GAME_START print 'commands entered : ', self.commands_entered if command.verb.name == 'quests': user_input.get_events_list(self) if command.verb.name == 'help': user_input.help_info() if command.verb.name == 'location': self.player.player_location() if command.verb.name == 'inventory' or command.verb.name == 'bag': print self.player.inventory.list_of_items() if command.verb.name == 'save': self.save_game() if command.verb.name == 'name': print self.player.name else: print "I'm not sure what you mean." self.commands_entered += 1 else: invalid_input('Command not recognized.', input_string=command.raw, tag='unknown command', game=self)