Subscribe
Home Craft How to Create a Text Based Adventure Game in Python

How to Create a Text Based Adventure Game in Python

by Staff
0 comment

Practice your Python programming with some simple text processing and decision handling to create a playable game.

A text adventure game is a fun project you can undertake if you are learning how to program. You can make a text adventure game using Python, run it in a command line, and change the story based on the text that the player enters.

The Python script will cover several kinds of fundamental programming concepts. This includes print statements, if statements, and functions.


How to Create the Python Script and Add Story Content

You can create a script using a standard text file with a .py extension. If you are not familiar with Python syntax, take a look at some basic Python examples that may help you learn it faster. You can also look at other useful Python one-liners to perform certain tasks.

In the main function of the Python file, set up your story and welcome message.

  1. Create a new file called “AdventureGame.py”.
  2. In the file, add the main starting function. The function will include a brief opening story to welcome the player to the adventure game. It will then call another function called introScene().
    if __name__ == "__main__":
    while True:
    print("Welcome to the Adventure Game!")
    print("As an avid traveler, you have decided to visit the Catacombs of Paris.")
    print("However, during your exploration, you find yourself lost.")
    print("You can choose to walk in multiple directions to find a way out.")
    print("Let's start with your name: ")
    name = input()
    print("Good luck, " +name+ ".")
    introScene()


How to Create Multiple Scenes and Options in the Story

Your story will contain several scenes or “rooms”. You can create a function for each scene so that you can re-use it later if the player ends up entering the same room again.

Each scene will also have different choices of where to go. It is a good idea to map out your story before coding the scenarios, to make sure your story is well organized.

Each scene will have a list of valid directions, and an if-statement for the multiple paths the player can take. Depending on the path the player takes, the program will call the next scene.

Create functions for the scenes that will occur in the story.

  1. Create the introScene() function above the main function. Add a message and the directions that the player can walk in.
    def introScene():
    directions = ["left","right","forward"]
    print("You are at a crossroads, and you can choose to go down any of the four hallways. Where would you like to go?")
    userInput = ""
    while userInput not in directions:
    print("Options: left/right/backward/forward")
    userInput = input()
    if userInput == "left":
    showShadowFigure()
    elif userInput == "right":
    showSkeletons()
    elif userInput == "forward":
    hauntedRoom()
    elif userInput == "backward":
    print("You find that this door opens into a wall.")
    else:
    print("Please enter a valid option.")
  2. Depending on the user’s input, the program will call another scene. For example, if the player types “left”, the program will display the scene showShadowFigure() to the player. From this room, if the player goes backward, the game will take them back to the intro scene. If they go left or right, they will either enter another room or hit a dead end.
    def showShadowFigure():
    directions = ["right","backward"]
    print("You see a dark shadowy figure appear in the distance. You are creeped out. Where would you like to go?")
    userInput = ""
    while userInput not in directions:
    print("Options: right/left/backward")
    userInput = input()
    if userInput == "right":
    cameraScene()
    elif userInput == "left":
    print("You find that this door opens into a wall.")
    elif userInput == "backward":
    introScene()
    else:
    print("Please enter a valid option.")
  3. Add the camera scene for if they turn right. This is where the player can find one of the exits. Call the quit() function to end the game. The player can also still choose to move backward to the previous scene.
    def cameraScene():
    directions = ["forward","backward"]
    print("You see a camera that has been dropped on the ground. Someone has been here recently. Where would you like to go?")
    userInput = ""
    while userInput not in directions:
    print("Options: forward/backward")
    userInput = input()
    if userInput == "forward":
    print("You made it! You've found an exit.")
    quit()
    elif userInput == "backward":
    showShadowFigure()
    else:
    print("Please enter a valid option.")
  4. Back to the beginning of the adventure game, you will still need to add the functions for the remaining scenes. Add the hauntedRoom() scene for if the player chooses to move forward. This will also end the game depending on the player’s choice.
    def hauntedRoom():
    directions = ["right","left","backward"]
    print("You hear strange voices. You think you have awoken some of the dead. Where would you like to go?")
    userInput = ""
    while userInput not in directions:
    print("Options: right/left/backward")
    userInput = input()
    if userInput == "right":
    print("Multiple goul-like creatures start emerging as you enter the room. You are killed.")
    quit()
    elif userInput == "left":
    print("You made it! You've found an exit.")
    quit()
    elif userInput == "backward":
    introScene()
    else:
    print("Please enter a valid option.")
  5. You can also add more interesting content to the game. Create a global variable, at the very top of the file, called “weapon”. It will either be true or false depending on if the player finds it.
    weapon = False
  6. In one of the rooms, set the weapon variable to true if the player finds it. The player can use it in the next room if needed.
    def showSkeletons():
    directions = ["backward","forward"]
    global weapon
    print("You see a wall of skeletons as you walk into the room. Someone is watching you. Where would you like to go?")
    userInput = ""
    while userInput not in directions:
    print("Options: left/backward/forward")
    userInput = input()
    if userInput == "left":
    print("You find that this door opens into a wall. You open some of the drywall to discover a knife.")
    weapon = True
    elif userInput == "backward":
    introScene()
    elif userInput == "forward":
    strangeCreature()
    else:
    print("Please enter a valid option.")
  7. If the player finds the weapon, they can kill the enemy in the next room, and find another exit. Otherwise, the enemy will kill them.
    def strangeCreature():
    actions = ["fight","flee"]
    global weapon
    print("A strange goul-like creature has appeared. You can either run or fight it. What would you like to do?")
    userInput = ""
    while userInput not in actions:
    print("Options: flee/fight")
    userInput = input()
    if userInput == "fight":
    if weapon:
    print("You kill the goul with the knife you found earlier. After moving forward, you find one of the exits. Congrats!")
    else:
    print("The goul-like creature has killed you.")
    quit()
    elif userInput == "flee":
    showSkeletons()
    else:
    print("Please enter a valid option.")


How to Run the Python Script

You can run the script using a terminal or command prompt. As you enter input into the terminal, the story will continue to move forward to the next scene.

  1. Using a terminal or command prompt, navigate to the location where you stored the file.
    cd C:UsersSharlDesktopPython
  2. Run the script.
    python AdventureGame.py
  3. The opening message will welcome you to start playing the game.
  4. Type from the available options listed, such as “left”, “right”, or “backward”. If you enter an invalid input, the game will prompt you for a valid one.
  5. You can also replay the game to choose another path.

You can download the full source code for the project from this GitHub repository.

Create a Simple Game Using Just One Python Script

You can create a text adventure game using a Python script, and run it in a terminal or command line. Inside the Python file, you can present the player with a welcoming message and initial story. The player can then type in their actions based on the options you present.

If you want to become a more well-rounded Python developer, you can have a look at some of the useful tools that you can use or integrate with Python.

Read the full article here

You may also like

Leave a Comment

Iman Hearts is one of the biggest lifestyle news and articles portals, we provide the latest news and articles about family, lifestyle, entertainment, and many more, follow us to get the latest news about what matters to you.

 

© 2022 Iman Hearts. All rights reserved. Sitemap