Learn to Code via Tutorials on Repl.it!

← Back to all posts
How to make a Text Adventure (python)
BananaJellyfish (206)

Text Adventure

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

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':
   print('Invalid command')

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:

inv.append('item name')

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!')
elif a == 'no':
    print('sure, just kill this spider with your poop!')
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.

Removing errors

In case someone enters something that is not the required input, at the end of all your if and elif statements put:

    print('please enter a valid command')

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:

import os

then when you want text to be cleared, so the console isn't consumed by lots of text:

print('You: hello')
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:


For example:

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
    print('Monster health: '+red+mHealth+white+'\n Monster 
    power: '+red+mPower+white+'\n Your Health: 
    '+green+pHealth+white+'Would you like 
    #loop until input is 1 or 2
    while input() != '1' and != '2':
        if input() == '1':
            print('You did '+red+'10'+white+' damage')
            mHealth -= 10
        elif input() == '2':
            #define each area to make this work
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.')
    #if the players health is less than 1, use the lose 
    #function(define this first)
    if pHealth < 1:
       #print you beat it, return to the area(if you've 
       #defined it)
       print('You beat it!')
Fireblazer (5)

This is so cool!
I am new so i don't know that much
but I have made a 100 line adventure game so far and this would be Big help

LegendaryWolf (694)

Not useful for meh coz I already know how to make one, but nice effort :)