|
| 1 | +# Import pyinputplus for choice inputs and os to clear the console. |
| 2 | +import pyinputplus |
| 3 | +import os |
| 4 | +import json |
| 5 | + |
| 6 | +# setting up some variables |
| 7 | +currentKey = '0' |
| 8 | +currentKeys = [] |
| 9 | +itemAlreadyAdded = False |
| 10 | + |
| 11 | +# Get the Story Prompts |
| 12 | +# A dictionary is used because we dont want to allow |
| 13 | +# duplicate keys |
| 14 | +with open('story.json', 'r') as f: |
| 15 | + storyPrompts = json.load(f) |
| 16 | + |
| 17 | +inventory = { |
| 18 | + 'banana(s)': 0, |
| 19 | + 'clock(s)': 2, |
| 20 | + 'swords(s)': 0, |
| 21 | +} |
| 22 | + |
| 23 | +# Check if the prompts are valid |
| 24 | +for prompt in storyPrompts: |
| 25 | + promptText, keys, *_ = storyPrompts[prompt] |
| 26 | + |
| 27 | + # Add ":" at the end of the prompt Text |
| 28 | + if not promptText.endswith(':'): |
| 29 | + storyPrompts[prompt][0] = promptText + ': ' |
| 30 | + |
| 31 | + # Check if the keys are strings, if not transform them |
| 32 | + storyPrompts[prompt][1] = [str(i) for i in keys] |
| 33 | + |
| 34 | + |
| 35 | +# Giving the user some instructions. |
| 36 | +print('Type in the number of the prompt or -i to view your inventory ... have fun.') |
| 37 | + |
| 38 | +# Prompt Loop |
| 39 | +while True: |
| 40 | + # Clearing the Console with cls |
| 41 | + os.system('cls') |
| 42 | + # Get the current prompt all its associated data |
| 43 | + currentPrompt, currentKeys, _, action = storyPrompts[currentKey] |
| 44 | + # Finish the Adventure when the next keys list contains the string 'end' |
| 45 | + if 'end' in currentKeys: |
| 46 | + break |
| 47 | + # Look for inventory Changes |
| 48 | + if not itemAlreadyAdded: |
| 49 | + if 'minus' in action: |
| 50 | + inventory[action.split('-')[1]+'(s)'] -= 1 |
| 51 | + if 'plus' in action: |
| 52 | + inventory[action.split('-')[1]+'(s)'] += 1 |
| 53 | + # Add Option Descriptions to the current Prompt with their number |
| 54 | + for o in currentKeys: |
| 55 | + |
| 56 | + invalidOption = False |
| 57 | + |
| 58 | + thisaction = storyPrompts[o][3] |
| 59 | + if 'minus' in thisaction: |
| 60 | + item = storyPrompts[o][3].split('-')[1]+'(s)' |
| 61 | + if inventory[item] == 0: |
| 62 | + print(storyPrompts[o][3].split('-')[1]+'(s)') |
| 63 | + invalidOption = True |
| 64 | + |
| 65 | + if not invalidOption: |
| 66 | + currentPrompt += f'\n{o}. {storyPrompts[o][2]}' |
| 67 | + |
| 68 | + |
| 69 | + currentPrompt += '\n\nWhat do you do? ' |
| 70 | + |
| 71 | + # Get the input from the user, only give them the keys as a choice so they dont |
| 72 | + # type in something invalid. |
| 73 | + userInput = pyinputplus.inputChoice(choices=(currentKeys + ['-i']), prompt=currentPrompt) |
| 74 | + |
| 75 | + # Printing out the inventory if the user types in -i |
| 76 | + if '-i' in userInput: |
| 77 | + print(f'\nCurrent Inventory: ') |
| 78 | + |
| 79 | + for i in inventory: |
| 80 | + print(f'{i} : {inventory[i]}') |
| 81 | + |
| 82 | + print ('\n') |
| 83 | + |
| 84 | + input('Press Enter to continue ... ') |
| 85 | + |
| 86 | + itemAlreadyAdded = True |
| 87 | + |
| 88 | + continue |
| 89 | + else: |
| 90 | + itemAlreadyAdded = False |
| 91 | + |
| 92 | + currentKey = userInput |
| 93 | + |
| 94 | +# Printing out the last prompt so the user knows what happened to him. |
| 95 | +print(storyPrompts[currentKey][0]) |
| 96 | +print('\nStory Finished ...') |
0 commit comments