How to make a Text Adventure (python)
Hello! This is a text adventure tutorial. i am new to markdown so it may not look great.
How to Make a Text adventure:
Defining Functions is useful for making areas in your text adventure
Let's make the first area:
def area(): a = input('There are some trees, and a treasure chest. What would you like to do?')
This is the basic text. Now add:
if a == 'trees': trees() #define this as another function elif a == 'chest': chest() else: print('Invalid command') area(1)
Don't forget that you need to indent this within the area() function!
This allows for you to be able to link back to area 1 just by calling area() where you want to.
Making lists is useful if you want an inventory.
First, make the inventory:
inv = ['torch', 'sword']
Printing out the list:
print(*inv, sep=", ")
Note - the * makes spaces between the items and the sep=", " makes it listed by commas
Adding to the list:
The list's name is, of course, inv(short for inventory). To add an item, You do:
An example of this:
inv = ['poo','orange'] a = input('Would you like to pick up the sword?') if a == 'yes': print('You picked up the sword!') inv.append('sword') area() elif a == 'no': print('sure, just kill this spider with your poop!') spider()
Deleting items from a list
I'm not going to spend much time on this. It is simply:
inv.remove('insert item name')
not something you use often, but could be helpful.
In case someone enters something that is not the required input, at the end of all your if and elif statements put:
else: print('please enter a valid command') area()
the defining functions will come in useful here.
a = input('take poop?') while a != yes and != no: print('please enter yes or no')
to make things neater, put this at the start:
then when you want text to be cleared, so the console isn't consumed by lots of text:
os.system('clear') #example print('You: hello') os.system('clear') print('Old man: You have chosen DEATH')
Making things look colourful
First, get the colours(put this at the start):
red = "\033[0;31m" green = "\033[0;32m" orange = "\033[0;33m" blue = "\033[0;34m" purple = "\033[0;35m" cyan = "\033[0;36m" white = "\033[0;37m"
and using them:
Note - whatever colour the text finishes on is the colour the user's input will be, so it is worthwhile putting +white at the end of your print statements
Also, before users enter input, put in:
a = input('Wut ur name?\n')
This means that when the user enters input, it will be on a new line. You can also use this in the middle of print startements, like this:
print('Will you:\n(1)Kill him\n(2)Give him some cheese')
A good fight will make a text adventure much more enjoyable. If you want the player to randomly encounter monsters, create a function:
#define the functions def show_stats(): #globalise variables global mPower, mHealth, pHealth #stats print('Monster health: '+red+mHealth+white+'\n Monster power: '+red+mPower+white+'\n Your Health: '+green+pHealth+white+'Would you like to:\n(1)Fight(2)Run\n') #loop until input is 1 or 2 while input() != '1' and != '2': if input() == '1': print('You did '+red+'10'+white+' damage') mHealth -= 10 encounter() elif input() == '2': #define each area to make this work area() def encounter(): #globalise variables global mHealth, pHealth, mPower while pHealth > 0 and mHealth > 0: print('The monster swipes at you. It does '+red+mPower+white+' damage.') show_stats() #if the players health is less than 1, use the lose #function(define this first) if pHealth < 1: lose() else: #print you beat it, return to the area(if you've #defined it) print('You beat it!') area()