Space Adventure Game (Without Timer)

Title: Space Adventure Game (Without 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. This version of the game focuses on the core mechanics without the added pressure of a timer.


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.


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.

# 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.

# 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()

    # Main game loop
    while True:
        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()

Thinking Process:

For this version, I focused on perfecting the core mechanics of the game, ensuring that the player could navigate rooms, collect items, and encounter the villain. I used functions to organize the code, making it easier to manage and extend. I also included decision branching and input validation to handle various user inputs and maintain game flow.


Silvia Pizarro McCants | UX & Graphic Design Portfolio

Print Friendly, PDF & Email