Signup/Sign In
PUBLISHED ON: MARCH 25, 2022

Text-Based Adventure Game Python Project

Text-Based Adventure Game Using Python

What is the definition of a Text-Based Game?

A text-based game is a basic input-output game that is entirely text-based. Users have alternatives to address different scenarios in this sort of game since they come with choices made by the user in the form of inputs.

Python implementation of a Text-Based Adventure Game

Let's begin by printing the first scene and then seeing how the tale progresses. Simply use the print function to do this. We can also add emoticons and emojis to make it more entertaining!

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?
""")

You're doing well! Now that we've created the scenario, it turns out to be rather exciting, and here comes your first decision! Now we'll take the user's input and create conditional statements for each option selected.

We must ensure that our game provides responses to all forms of user inputs and that no option made by the user results in an error.

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'll start with the first option and then add a variable to validate if our response is accurate or erroneous. The conditional loop and if-else statements are then created. The game will keep asking you for your option until you offer a good response.

Now that the first scene is finished, we can go on to the next scene and continue building the game in this manner. The code for the second scenario may be seen below.

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 following is the code for the third scene. The outcome of the third scene is now determined by the decision made in scene 2, namely whether the teddy bear was chosen or ignored, and whether the main protagonist got the potion or not.

After three scenes, we'll wrap up Chapter 1 of the tale. Depending on your preferences, you may extend or even modify the whole tale.

Simply begin the first scene of the tale to begin the story.

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

The outcome of the preceding narrative is shown below. And it's a lot of fun!

Adventure

Conclusion

You now know how to create basic text-based adventure games on your own! You may even try out your own original narrative! Have fun coding! Thank you for taking the time to read this!



About the author:
Adarsh Kumar Singh is a technology writer with a passion for coding and programming. With years of experience in the technical field, he has established a reputation as a knowledgeable and insightful writer on a range of technical topics.