Nov-15-2019, 06:42 PM
my full game code is below i need to put in a save load function I know it goes something like
dct = {'hp': 0, 'gold': 100} #<- state import json json.dump(dct, open("savegame.json", "w"), indent=4) #SAVINGbut i have no idea where to put this in my code or how to do it. my teacher did not go over this so beginners tips would be amazing. please help. thank you.
__version__ = 8 # 2) print_introduction: Introduction to game def print_introduction(): print('Welcome to The Haunted House adventure. You are locked inside this creepy old house and you need to escape!') print('There are 6 rooms in this house for you to explore. You must find 2 key items and solve a riddle in order to be free!') print('\n') print('Strange noises and whispers fill the air. Will you make it out alive to tell your story?! Let us find out...') # 3) get_initial_state: Make the initial state for your player def get_initial_state(): ''' Dictionary tells player initial state : Returns: dict: A dictionary with the player's current state field values ''' initial_state_dict = {'game status': 'playing', 'location': 'living room', 'shiny fork': False, 'golden key': False, 'bait':False, 'life jacket':False, 'jaws': False } print('\n') print('Now, let us begin this escape!') return initial_state_dict #while not done: # 4) print_current_state:some text describing the game def print_current_state(the_player): ''' This function is needed to print the current location Args: the_player (from dict): gives current player state ''' #print location: print('\n_____________________________________________________') print('Current location:', the_player['location'], '\n') if the_player['location'] == 'living room': print('You are in the living room and hear strange whispers as your hairs on the back of your neck stand up... What are you going to do next?') if the_player['location'] == 'kitchen': if the_player['shiny fork']: print('This might help you in one of the bedrooms') else: print('You walk into the kitchen and looked around. As you scan the room your eyes rest on a shiny fork. What are you going to do?') if the_player['location'] == 'bedroom 1': if the_player['shiny fork']: if the_player['golden key']: print ('Now you can enter bedroom 2') else: print('After grabbing the shiny fork, you begin to walk down the hall when you hear a loud noise behind you! You start running and make it to the first bedroom and slam the door shut behind you... whew! Not long after that, you discover a golden key in the bedroom') else: print('The door is locked, it maybe jammed. You need something sharp to open it. Maybe a fork from the kitchen') if the_player['location'] == 'bedroom 2': if the_player['golden key'] == False: print('You cannot open the locked closet door without the golden key.') if the_player['golden key'] == True: print('Aha it worked! You insert the golden key into the locked closet door and turn the knob... You brush aside the old cobwebs and make your way through the secret passage to reach the basement.') if the_player['location'] == 'basement': print('After making your way through the secret passage, you step into the cold, damp basement. Shivers go through your spine as you try to keep warm') if the_player['location'] == 'garden': print('You break free and sprint out into the garden! Taking a quick look behind you, you see nothing but whisps of air filling the void. You cannot shake the feeling that something is still after you, but never again will you go inside that haunted house!') # 5) get_options: list of commands available def get_options(the_player): if the_player['location'] == 'living room': actions_list = ['kitchen','bedroom 1', 'bedroom2', 'quit'] #print('kitchen') #print('explore bedroom') #print('quit') return actions_list if the_player['location'] == 'bedroom 1': if the_player['shiny fork'] == True: if the_player['golden key'] == True: actions_list = ['kitchen','bedroom 2','quit'] return actions_list else: actions_list = ['kitchen', 'pick up golden key','bedroom 2','quit'] return actions_list else: actions_list = ['kitchen'] return actions_list if the_player['location'] == 'bedroom 2': actions_list = ['basement'] return actions_list if the_player['location'] == 'basement': actions_list = ['garden'] return actions_list if the_player['location'] == 'garden': actions_list = ['pier', 'harbor', 'reef', 'deep sea'] return actions_list #If player is in the kitchen location: elif the_player['location'] == 'kitchen': if the_player['shiny fork']: actions_list = ['bedroom 1','quit'] return actions_list else: actions_list = ['pick up shiny fork', 'bedroom 1','quit'] return actions_list # 6) print_options: the list of commands available to player def print_options(available_options): print('\nChoose one of the options below. Locations and actions to choose from:') for item in available_options: print(item) # 7) get_user_input: handles valid commands def get_user_input(available_options): command = '' while command not in available_options: if command == 'home': return 'quit' command = input('What would you like to do next? ') #user input based on the list of options provided return command #this value is returned from the function and stored under the variable 'chosen_command' in the main function #this variable is used as a parameter for process_command function below. # 8) process_command: based on dictionary def process_command(chosen_command, the_player): if chosen_command == 'quit': the_player['game status']= chosen_command if chosen_command == 'pick up shiny fork': the_player['shiny fork'] = True if chosen_command == 'pick up golden key': the_player['golden key'] = True if chosen_command == 'bedroom 1': the_player['location'] = chosen_command if chosen_command == 'kitchen': the_player['location'] = chosen_command if chosen_command == 'bedroom 2': if the_player['golden key']: the_player['location'] = chosen_command else: print('Find golden key to open this room') if chosen_command == 'basement': the_player['location'] = chosen_command if chosen_command == 'garden': the_player['location'] = 'garden' the_player['game status'] = 'win' # 9) print_game_ending: tells the user if they won or lost, quit def print_game_ending(the_player): if the_player['game status'] == 'quit': print('\nYOU QUIT.') if the_player['game status'] == 'lose': print('\nYOU LOSE.') if the_player['game status'] == 'win': print('You have succcessfully escaped and vowed to never go back to the haunted house again') print('\n') print('YOU WON!') #get_initial_state() is printed here: if the_player: print('') # 1) Main function that runs the game # def main(): # introduction to the game print_introduction() # the initial state for game the_player = get_initial_state() # sees if player won or not while the_player['game status'] == 'playing': # Gives players current state print_current_state(the_player) # this Gives options to player available_options = get_options(the_player) # once given input gives more options print_options(available_options) # this Gives Valid User Input chosen_command = get_user_input(available_options) # this Process Commands and change state process_command(chosen_command, the_player) # Give user message print_game_ending(the_player) # the main function if __name__ == "__main__": ''' the main function call each function ''' main() # print_introduction() # print(get_initial_state())