Writing a Text-Based Adventure Game in Python

Here is a simple text-based adventure game implemented in Python:

# define the different rooms in the game
rooms = {
    'bedroom': {
        'description': 'You are in a bedroom. There is a bed, a dresser, and a door to the south.',
        'exits': {'south': 'hallway'}
    },
    'hallway': {
        'description': 'You are in a hallway. There are doors to the north, east, and west.',
        'exits': {'north': 'bedroom', 'east': 'kitchen', 'west': 'bathroom'}
    },
    'kitchen': {
        'description': 'You are in a kitchen. There is a stove, a refrigerator, and a door to the west.',
        'exits': {'west': 'hallway'}
    },
    'bathroom': {
        'description': 'You are in a bathroom. There is a sink, a toilet, and a door to the east.',
        'exits': {'east': 'hallway'}
    }
}

# start the player in the bedroom
current_room = 'bedroom'

# game loop
while True:
    # print the description of the current room
    print(rooms[current_room]['description'])

    # get the player's input
    command = input('What do you want to do? ')

    # check if the player wants to go to a different room
    if command in ['north', 'south', 'east', 'west']:
        # check if the desired direction is a valid exit
        if command in rooms[current_room]['exits']:
            # update the current room
            current_room = rooms[current_room]['exits'][command]
        else:
            # the desired direction is not a valid exit
            print("You can't go that way.")
    else:
        # the player entered an invalid command
        print("I don't understand what you want to do.")

Here’s a breakdown of how the code works:

  1. The rooms dictionary defines the different rooms in the game. Each room has a description and a dictionary of exits that specifies which rooms can be accessed from that room.
  2. The current_room variable stores the player’s current location. It is initialized to the 'bedroom' room.
  3. The game loop runs indefinitely, until the player decides to quit. Inside the loop, the game prints the description of the current room and asks the player for a command.
  4. The command variable stores the player’s input. If the player enters a direction ('north', 'south', 'east', or 'west'), the game checks if that direction is a valid exit from the current room. If it is, the game updates the current_room variable to the new room. If it is not, the game prints an error message.
  5. If the player enters any other command, the game prints an error message.

That’s it! This is a very simple example of a text-based adventure game, but you can add more complexity by adding more rooms, items, and gameplay mechanics. For example, you could add a inventory system to keep track of the items the player has collected, or add puzzles that the player must solve to progress through the game.

This game consists of four rooms: a bedroom, a hallway, a kitchen, and a bathroom. The player starts in the bedroom and can navigate between the rooms by entering a direction (north, south, east, or west). The game will print the description of the current room and ask the player for a command. If the player enters a valid direction, the game will move the player to the corresponding room. If the player enters an invalid command or tries to go in an invalid direction, the game will display an error message.

This is just a simple example of a text-based adventure game. You can add more rooms, items, and gameplay mechanics to make the game more interesting. For example, you could add a inventory system to keep track of the items the player has collected, or add puzzles that the player must solve to progress through the game.

Facebook
Twitter
LinkedIn
Pinterest

Table of Contents

Related posts