Space Adventure Game (With Timer)

Title: Space Adventure Game (With Timer)

Description:

Welcome to my text-based adventure game, “Space Adventure Game”! This project showcases my skills in Python programming, including function development, decision branching, and input validation. In this version of the game, I’ve added a timer to increase the challenge and excitement.


Game Objective:

Navigate through different rooms, collecting items to win the game. The goal is to collect 7 items before encountering the villain, the Alien Warlord. You have 5 minutes to succeed before the Alien Warlord defeats you.


Key Features:

  • Functions: Organized code into functions for readability and maintainability.
  • Dictionary: Used a dictionary to link rooms and items, ensuring a smooth navigation experience.
  • Gameplay Loop: Implemented a loop to manage game flow, user input, and game status.
  • Decision Branching: Applied decision branching to handle user commands and game logic.
  • Input Validation: Validated user inputs to ensure valid commands and actions.
  • Timer: Added a 5-minute timer to increase the game’s difficulty and add pressure.



# TextBasedGame.py
# Silvia Pizarro McCants
# Date:06/18/2024

# Purpose:
# This script is a text-based adventure game called "Space Adventure Game".
# The player navigates through different rooms, collecting items to win the game.
# The goal is to collect 7 items before encountering the villain, the Alien Warlord.
# The game includes a timer to add pressure, giving the player 5 minutes to succeed.

# Custom print function to handle multiple lines
def custom_print(*args):
    for arg in args:
        print(arg)

# Function to show game instructions
def show_instructions():
    custom_print(
        "Space Adventure Game",
        "Collect 7 items to win the game, or be defeated by the Alien Warlord.",
        "Move commands: go South, go North, go East, go West",
        "Add to Inventory: get 'item name'",
        "Type 'QUIT' to exit the game."
    )

# Function to show player status
def show_status(current_room, inventory, rooms):
    directions = [key for key in rooms[current_room] if key != 'item']
    status_lines = [
        "---------------------------",
        f"You are in the {current_room}",
        f"Inventory: {inventory}",
    ]
    if "item" in rooms[current_room]:
        status_lines.append(f"You see a {rooms[current_room]['item']}")
    status_lines.append(f"Directions you can move: {', '.join(directions)}")
    status_lines.append("---------------------------")
    custom_print(*status_lines)

# Function to move to a different room
def move_to_room(current_room, direction, rooms):
    if direction.capitalize() in rooms[current_room]:
        current_room = rooms[current_room][direction.capitalize()]
    else:
        custom_print("You can't go that way!")
    return current_room

# Function to get an item from the current room
def get_item(current_room, item, inventory, rooms):
    if "item" in rooms[current_room] and item.lower() == rooms[current_room]['item'].lower():
        inventory.append(rooms[current_room]['item'])
        custom_print(f"{rooms[current_room]['item']} retrieved!")
        del rooms[current_room]['item']
    else:
        custom_print(f"No {item} in this room!")
    return inventory

def main():
    # Define the dictionary linking rooms to each other and items to rooms
    rooms = {
        'Attic': {'South': 'Space Station'},
        'Space Station': {'North': 'Attic', 'South': 'Alien Jungle', 'East': 'Ancient Ruins', 'West': 'Asteroid Field', 'item': 'Energy Core'},
        'Alien Jungle': {'North': 'Space Station', 'South': 'Villain’s Lair', 'East': 'Deserted Moon', 'West': 'Crystal Cavern', 'item': 'Crystal Shard'},
        'Asteroid Field': {'East': 'Space Station', 'item': 'Plasma Battery'},
        'Ancient Ruins': {'West': 'Space Station', 'South': 'Space Lab', 'item': 'Mystic Gem'},
        'Space Lab': {'North': 'Ancient Ruins', 'item': 'Quantum Chip'},
        'Deserted Moon': {'West': 'Alien Jungle', 'East': 'Space Lab', 'North': 'Crystal Cavern', 'item': 'Alien Alloy'},
        'Crystal Cavern': {'West': 'Alien Jungle', 'North': 'Villain’s Lair', 'East': 'Deserted Moon', 'item': 'Power Cell'},
        'Villain’s Lair': {'South': 'Alien Jungle', 'North': 'Crystal Cavern'}
    }

    # Start the player in the Attic with an empty inventory
    current_room = 'Attic'
    inventory = []

    show_instructions()

    start_time = time.time()
    time_limit = 5 * 60  # 5 minutes in seconds

    # Main game loop
    while True:
        elapsed_time = time.time() - start_time
        if elapsed_time > time_limit:
            custom_print("Time's up! The Alien Warlord has defeated you!")
            break

        show_status(current_room, inventory, rooms)

        move = input("Enter your move: ").strip().lower()

        if move == "quit":
            custom_print("Thanks for playing! Goodbye!")
            break

        move = move.split(maxsplit=1)

        if len(move) == 2:
            action, direction_or_item = move[0], move[1]
        else:
            custom_print("Invalid input, please enter a valid move.")
            continue

        if action == 'go':
            current_room = move_to_room(current_room, direction_or_item, rooms)

        elif action == 'get':
            inventory = get_item(current_room, direction_or_item, inventory, rooms)

        else:
            custom_print("Invalid move! Use 'go' or 'get' followed by direction or item name.")

        # Check if the player has collected all items
        if len(inventory) == 7:
            custom_print("Congratulations! You have collected all items and defeated the Alien Warlord!")
            break

        # Check if the player has encountered the villain without collecting all items
        if current_room == 'Villain’s Lair':
            if len(inventory) < 7:
                custom_print("NOM NOM...GAME OVER! The Alien Warlord has defeated you!")
            break

if __name__ == "__main__":
    main()

# Note:
# I added a timer to the game to add more fun and pressure. The player has 5 minutes to collect all items before the Alien Warlord defeats them.


Thinking Process:

For this version of the game, I wanted to add an element of excitement and urgency. By introducing a timer, I aimed to make the game more challenging and engaging. I focused on ensuring that the core mechanics, such as navigating rooms, collecting items, and encountering the villain, were solid. Organizing the code into functions made it easier to manage and extend. I also paid close attention to decision branching and input validation to handle various user inputs and maintain smooth game flow.


Silvia Pizarro McCants | UX & Graphic Design Portfolio

Print Friendly, PDF & Email