school projects,  🖥️ Digital Experiments

Dragon game

Untitled dragon text adventure game.

While taking a college coding course recently, we learned how to make a dragon game in python. In this game we embark with the dragon slayer on an epic adventure, through this mystical castle filled with peril and treasure, in “untitled dragon game.” You find yourself in the Great Hall with paths leading to the Bedroom, Kitchen, and Library. Each room holds secrets and challenges, from hidden items like Armor, Swords, and Potions to encounters with the fearsome Dragon in the Dungeon. Collect five magical items to conquer the dragon and claim victory, or risk becoming its next meal! Navigate using simple commands like ‘go North’, ‘go South’, ‘go East’, ‘go West’, and gather items with ‘get [item]’. Will you emerge as the hero of this daring quest?

If yes! Let’s dive into the code and see how it all works!

Diving into The Code

Here’s the complete code for our untitled Dragon Quest game. Copy and paste it into a Python file (e.g., dragon_quest.py) and run it to start your adventure.

rooms = {
    'Great Hall': {'South': 'Bedroom', 'East': 'Kitchen', 'West': 'Library'},
    'Bedroom': {'North': 'Great Hall', 'East': 'Cellar', 'item': 'Armor'},
    'Cellar': {'West': 'Bedroom', 'item': 'Helmet'},
    'Kitchen': {'West': 'Great Hall', 'item': 'Sword'},
    'Library': {'East': 'Great Hall', 'North': 'Gallery', 'item': 'Shield'},
    'Gallery': {'South': 'Library', 'item': 'Potion'},
    'Dungeon': {'South': 'Great Hall', 'item': 'Dragon'}  # Villain room
}

def show_instructions():
    print("Dragon Quest Game")
    print("Collect 5 items to win the game, or be eaten by the dragon.")
    print("Move commands: go South, go North, go East, go West")
    print("Add to Inventory: get 'item name'")
    print("Type 'exit' to quit the game")

def show_status(current_room, inventory):
    print(f"\nYou are in the {current_room}")
    print(f"Inventory: {inventory}")
    if 'item' in rooms[current_room]:
        print(f"You see a {rooms[current_room]['item']}")
    print("----------------------")

def get_player_command():
    return input("Enter your move: ").strip()

def move_player(current_room, direction):
    if direction in rooms[current_room]:
        return rooms[current_room][direction]
    else:
        print("You can't go that way!")
        return current_room

def main():
    current_room = 'Great Hall'
    inventory = []

    show_instructions()

    while True:
        show_status(current_room, inventory)
        command = get_player_command()

        if command.lower() == 'exit':
            print("Thank you for playing! The dragon says goodbye.")
            break
        elif command.lower().startswith('go '):
            direction = command[3:].capitalize()
            current_room = move_player(current_room, direction)
            if current_room == 'Dungeon':
                print("NOM NOM NOM...GAME OVER!")
                print("Thank you for playing the game. I Hope you enjoyed it.")
                break
        elif command.lower().startswith('get '):
            item = command[4:]
            if 'item' in rooms[current_room] and rooms[current_room]['item'].lower() == item.lower():
                inventory.append(rooms[current_room]['item'])
                print(f"{item} picked up!")
                del rooms[current_room]['item']
                if len(inventory) == 5:
                    print("Congratulations! You have collected all the items and defeated the dragon!")
                    print("Thank you for playing the game. I Hope you enjoyed it.")
                    break
            else:
                print("There is no such item here!")
        else:
            print("Invalid command! Please enter 'go North', 'go South', 'go East', 'go West', 'get [item]', or 'exit'.")

if __name__ == "__main__":
    main()

How the game works

  1. Rooms and Items: The rooms dictionary defines the game’s map. Each room has connections to other rooms (e.g., ‘South’, ‘East’) and may contain an item.
  2. Instructions: The show_instructions function prints the game’s instructions.
  3. Game Status: The show_status function displays the player’s current location and inventory.
  4. Player Input: The get_player_command function gets the player’s next move.
  5. Move Logic: The move_player function updates the player’s location based on the direction they choose.
  6. Main Game Loop: The main function runs the game, checking player commands, updating the game state, and ending the game if the player wins or encounters the dragon.

Running the Game

To play the game, open your terminal or command prompt, navigate to the directory containing your dragon_quest.py file, and run:

python untitled_dragon_quest.py

Follow the instructions printed on the screen to navigate through the rooms, collect items, and avoid the dragon!

Conclusion

This simple text adventure game demonstrates how you can create an interactive game using Python. The game is cross-platform, meaning it can be run on any system with Python installed, whether it’s Windows, macOS, or Linux.

Feel free to expand the game by adding more rooms, items, and challenges. Happy coding and may your adventures be thrilling!