How to create a text-based adventure game in Python?

Featured Img Text Based Game

Hello, there fellow learner! Today we are going to make a fun text-based adventure game from scratch. First, let’s understand what a text-based game and then we will implement the same in the python programming language.

What is a text-based game?

A text-based game is a completely text-based input-output simple game. In such type of game, users have options to handle various situations as they arrive with choices taken by the user in the form of inputs.

The storyline for our game

The figure below displays the small story we will be building in python in this tutorial. You can expand or change the story according to your own preferences.

text-based adventure game
Text Based Story Game

Implementation of the Text-Based Adventure Game in Python

Let’s first start off the story by printing the initial scene and how the story moves forward. This can be done by simply using the print function. To make it more fun we can add emoticons and emojis as well!

print("""WELCOME TO THE ADVENTURE GAME!
    Let's start the action! ☆-🎬-☆
    
    Lily wakes up in her bedroom in the middle of the night. She heard a loud BAN outside the house.
    Now she has two choices she can either stay in the room or check what the sound might be about.
    
    Type your choice: Stay or Evaluate?
""")

Good going! Now we have the scene set and it turns out to be interesting as well and look here comes you first choice! Let’s now take the input from user and enter the conditional statements for each choice made.

We need to make sure that our game has answers to all types of inputs made by the user and it doesn’t result in an error in any choice made.

def scene1():
    import time
    print("""WELCOME TO THE ADVENTURE GAME!
        Let's start the action! ☆-🎬-☆

        Lily wakes up in her bedroom in the middle of the night. She heard a loud BAN outside the house.
        Now she has two choices she can either stay in the room or check what the sound might be about.

        Type your choice: Stay or Evaluate?
    """)

    c1 = input()
    time.sleep(2)
    ans = 'incorrect'
    while(ans=='incorrect'):
        if(c1.upper()=="STAY"):
            print("\nLily decides to stay in the room and ends up staying inside forever as noone seems to come to help her.")
            ans = 'correct'
        elif(c1.upper()=="EVALUATE"):
            print("Lily exits the room silently and reaches the main hall.")
            ans='correct'
            scene2()
        else:
            print("ENTER THE CORRECT CHOICE! Stay or Evaluate?")
            c1 = input()

We take the first choice input and then we will create a variable that will confirm if our answer is correct or incorrect. Then we create the conditional loop and if-else statements. The game keeps on asking for the choice again and again until the answer given is valid.

Now the first scene is complete, we can move on to the next scene and build the whole game in the same way. Below we have the code for the second scene.

def scene2():
    import time
    print("""
            In the main hall, she finds a strange but cute teddy bear on the floor. 
            She wanted to pick the teddy up. 
            But should she? It doesn't belong to her. (•˳̂•̆)

            Type your choice: Pick or Ignore?

            """)
    time.sleep(2)
    c1 = input()
    ans = 'incorrect'
    while(ans=='incorrect'):
        if(c1.upper()=="PICK"):
            print("""\nThe moment Lily picked up the the teddy bear. The Teddy bear starts TALKING!The bear tells Lily that she is in grave danger as there is a monster in the house.And the monster has captured her PARENTS as well!But he hugged her and told her not to get scared as he knows how to beat the moster!""")
            time.sleep(2)
            print("""\nThe bear handed lily a magical potion which can weaken the moster and make him run away!He handed her the potion and then DISAPPEARED!Lily moved forward.""")
            ans = 'correct'
            pick="True"
        elif(c1.upper()=='IGNORE'):
            print("""\nLily decided not to pick up the bear and walked forward.""")
            ans='correct'
            pick="False"
        else:
            print("Wrong Input! Enter pick or ignore?")
            c1=input()
    time.sleep(2)
    scene3(pick)

The code for the third scene is as follows. Now the result of the third scene depends on the choice made in scene2 which is if the teddy bear was picked or ignored and if the main protagonist received the potion or not.

def scene3(pick_value):
    import time
    print("""\n\nAfter walking for a while, Lily saw the MONSTOR in front of her!
    It had red eyes and evil looks. She got very scared! """)
    time.sleep(2)
    if(pick_value=="True"):
        time.sleep(2)
        print("""But then she remembered! She had the magic portion and she threw it on the moster!
              Well she had nothing to lose!""")
        time.sleep(2)
        print("\n The monster SCREAMED in pain but he managed to make a portal and pushed Lily to a new world!")
    elif(pick_value=="False"):
        print("The monster attacked Lily and hurt her! She was then thrown to the new world by the monster!")

We will be ending chapter 1 of the story after three scenes. You can expand or even change the whole story according to your preference.

To start the story simply start the scene1 of the story.

scene1()
print("\n\n")
print("=================================END OF CHAPTER 1=================================")

The result of the story above is shown below. And it is pretty great!

text-based adventure game
Text Based Adventure Game Output

Conclusion

Now you know how to build simple and easy text-based adventure games all by yourself! You can try out your own unique story as well! Happy coding! Thank you for reading!