w3resource

Python Project: Text-Based Adventure Game - Solutions and Explanations

Text-based Adventure Game:

Develop a simple text-based adventure game with multiple choices for the player.

Input values:
Player makes choices by selecting options presented in the game.

Output value:
Story progression based on player choices, with different outcomes.

Example:

Input values:
1. Enter the dark cave.
2. Follow the path through the forest.
Output value:
You chose to enter the dark cave. Inside, you find a treasure chest.
Input values:
1. Open the chest.
2. Leave the cave.
Output value:
You chose to open the chest. Congratulations! You found a valuable gem.

Here are two different solutions for a text-based adventure game in Python. This game will allow players to make choices that influence the story's progression, leading to different outcomes.

Solution 1: Basic Approach using Conditional Statements

Code:

# Solution 1: Basic Approach Using Conditional Statements

def start_adventure():
    """Start the text-based adventure game"""
    print("Welcome to the Adventure Game!")
    print("You find yourself at a crossroads in a dense forest.")
    
    # Present the first choice to the player
    print("1. Enter the dark cave.")
    print("2. Follow the path through the forest.")
    
    # Get the player's choice
    choice1 = input("What do you want to do? (1 or 2): ")

    # Branching story based on the first choice
    if choice1 == '1':
        print("\nYou chose to enter the dark cave. Inside, you find a treasure chest.")
        print("1. Open the chest.")
        print("2. Leave the cave.")
        
        # Get the player's choice for the second scenario
        choice2 = input("What do you want to do? (1 or 2): ")

        if choice2 == '1':
            print("\nYou chose to open the chest. Congratulations! You found a valuable gem.")
        elif choice2 == '2':
            print("\nYou chose to leave the cave. As you exit, the cave entrance collapses behind you.")
        else:
            print("\nInvalid choice. The adventure ends here.")
    
    elif choice1 == '2':
        print("\nYou chose to follow the path through the forest. The path splits into two directions.")
        print("1. Take the left path.")
        print("2. Take the right path.")
        
        # Get the player's choice for the second scenario
        choice2 = input("What do you want to do? (1 or 2): ")

        if choice2 == '1':
            print("\nYou chose the left path. After a short walk, you find a beautiful clearing with a hidden waterfall.")
        elif choice2 == '2':
            print("\nYou chose the right path. Unfortunately, it leads to a dead end.")
        else:
            print("\nInvalid choice. The adventure ends here.")
    
    else:
        print("\nInvalid choice. The adventure ends here.")

# Start the game
start_adventure()

Output:

Welcome to the Adventure Game!
You find yourself at a crossroads in a dense forest.
1. Enter the dark cave.
2. Follow the path through the forest.
What do you want to do? (1 or 2): 1

You chose to enter the dark cave. Inside, you find a treasure chest.
1. Open the chest.
2. Leave the cave.
What do you want to do? (1 or 2): 1

You chose to open the chest. Congratulations! You found a valuable gem.

Explanation:

  • The game starts with a narrative setup where the player is presented with an initial choice.
  • The player's choices are handled with ‘if-elif’ statements that branch the story based on their input.
  • Each choice leads to a different part of the story, with subsequent choices influencing the outcome.
  • This approach is simple and effective but can become complex with more choices and outcomes.

Solution 2: Using a Class to Structure the Game

Code:

# Solution 2: Using a Class to Structure the Game

class AdventureGame:
    """Class to handle the text-based adventure game"""

    def __init__(self):
        """Initialize the game"""
        print("Welcome to the Adventure Game!")
        print("You find yourself at a crossroads in a dense forest.")

    def start(self):
        """Start the adventure by presenting the first choice"""
        print("1. Enter the dark cave.")
        print("2. Follow the path through the forest.")
        choice1 = input("What do you want to do? (1 or 2): ")
        self.first_choice(choice1)

    def first_choice(self, choice):
        """Handle the first choice and branch the story"""
        if choice == '1':
            self.enter_cave()
        elif choice == '2':
            self.follow_path()
        else:
            print("\nInvalid choice. The adventure ends here.")

    def enter_cave(self):
        """Handle the scenario of entering the dark cave"""
        print("\nYou chose to enter the dark cave. Inside, you find a treasure chest.")
        print("1. Open the chest.")
        print("2. Leave the cave.")
        choice = input("What do you want to do? (1 or 2): ")

        if choice == '1':
            print("\nYou chose to open the chest. Congratulations! You found a valuable gem.")
        elif choice == '2':
            print("\nYou chose to leave the cave. As you exit, the cave entrance collapses behind you.")
        else:
            print("\nInvalid choice. The adventure ends here.")

    def follow_path(self):
        """Handle the scenario of following the path through the forest"""
        print("\nYou chose to follow the path through the forest. The path splits into two directions.")
        print("1. Take the left path.")
        print("2. Take the right path.")
        choice = input("What do you want to do? (1 or 2): ")

        if choice == '1':
            print("\nYou chose the left path. After a short walk, you find a beautiful clearing with a hidden waterfall.")
        elif choice == '2':
            print("\nYou chose the right path. Unfortunately, it leads to a dead end.")
        else:
            print("\nInvalid choice. The adventure ends here.")

# Create an instance of the AdventureGame class
game = AdventureGame()

# Start the adventure
game.start()

Output:

Welcome to the Adventure Game!
You find yourself at a crossroads in a dense forest.
1. Enter the dark cave.
2. Follow the path through the forest.
What do you want to do? (1 or 2): 2

You chose to follow the path through the forest. The path splits into two directions.
1. Take the left path.
2. Take the right path.
What do you want to do? (1 or 2): 2

You chose the right path. Unfortunately, it leads to a dead end.

Explanation:

  • The game is encapsulated within an 'AdventureGame' class, making the code modular and easy to manage.
  • The class methods ('start', 'first_choice', 'enter_cave', 'follow_path') handle different parts of the game, separating the logic for different scenarios.
  • This structure allows for better organization, especially as the game grows in complexity, and makes it easier to extend the game with new scenarios or choices.
  • This approach leverages Object-Oriented Programming (OOP) principles to create a more maintainable and scalable game structure.


Become a Patron!

Follow us on Facebook and Twitter for latest update.