Skip to content

Commit ef38646

Browse files
committed
add text adventure game tutorial
1 parent 8bf9fb9 commit ef38646

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
114114
- [How to Make a Simple Math Quiz Game in Python](https://www.thepythoncode.com/article/make-a-simple-math-quiz-game-in-python). ([code](general/simple-math-game))
115115
- [How to Make a Network Usage Monitor in Python](https://www.thepythoncode.com/article/make-a-network-usage-monitor-in-python). ([code](general/network-usage))
116116
- [How to Replace Text in Docx Files in Python](https://www.thepythoncode.com/article/replace-text-in-docx-files-using-python). ([code](general/docx-file-replacer))
117+
- [How to Make a Text Adventure Game in Python](https://www.thepythoncode.com/article/make-a-text-adventure-game-with-python). ([code](general/text-adventure-game))
117118

118119

119120

general/text-adventure-game/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Make a Text Adventure Game in Python](https://www.thepythoncode.com/article/make-a-text-adventure-game-with-python)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pyinputplus
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"0": ["You embark on a new adventure, you are at a conjunction where do you go?", [1, 2], "Go Back", ""],
3+
"1": ["You Encounter an angry Mob of Programmers, what do you do?", [3, 4], "Go Right", ""],
4+
"2": ["You see the City of schaffhausen in front of you", [0, 3], "Go Left", ""],
5+
"3": ["I dont know why you did that but okay.", [4, 5], "Use Banana", "minus-clock"],
6+
"4": ["Seems like it worked they did not notice you. One of them slips you a banana", [4, 5], "Pull out Laptop", "plus-banana"],
7+
"5": ["The Banana was poisonous", ["end"], "Eat Banana", ""],
8+
"10": ["You fell over and now you are in grave pain ... ", ["end"], "Pull out Laptop", ""]
9+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)