Python Game - Dragon Cave Game

 Python for Beginners: Build a "Dragon Realm" Game🐲

Python Game - Dragon Cave Game
Python Game - The Dragon Cave

Are you looking for a fun way to practice your Python skills? This short game—originally featured by Al Sweigart in Invent Your Own Computer Games with Python—is a fantastic way to understand how programs make decisions.

The Story

You are in a land full of dragons. In front of you, you see two caves.

  • In one cave, the dragon is friendly and will share his treasure.
  • The other dragon is greedy and hungry, eating anyone on sight!
The Twist: The "safe" cave is randomized every single time you play. Even if you choose Cave 1 twice, the outcome might be different. This is the power of the random module!

Key Programming Concepts Used

To make this game work, we use four essential Python features:
  1. import random & time: We use modules to give the program "superpowers." random allows the computer to make a choice, and time creates suspense by pausing the text.
  2. User-Defined Functions: By using def, we break the game into organized chunks: displayIntro(), chooseCave(), and checkCave().
  3. The while Loop: This keeps the game running until the player decides to quit.
  4. Boolean Logic: The game checks if your input matches the "lucky" number generated by the computer.

The Game Logic Flow

The Python Code

Copy and paste this into your favorite editor (like VS Code or PyCharm) to try it out:

import random

import time

def displayIntro():

    print("You are in a land full of dragons. In front of you,")

    print("you see two caves. In one cave, the dragon is friendly,")

    print("and will share his treasure with you. The other dragon")

    print("is greedy and hungry, and will eat you on sight.")

    print()

def chooseCave():

    cave = ""

    # This loop ensures the player enters a valid choice

    while cave != "1" and cave != "2":

        print("Which cave will you go into? (1 or 2 ?)")

        cave = input()

    return cave

def checkCave(chosenCave):

    print("You approach the cave ... ")

    time.sleep(2) # Creates a dramatic pause

    print("It is dark and spooky ... ")

    time.sleep(2)

    print("A large dragon jumps out in front of you! He opens his jaws and ... ")

    print()

    time.sleep(2)

    # The computer "hides" the treasure in a random cave

    friendlyCave = random.randint(1, 2)

    if chosenCave == str(friendlyCave):

        print("Gives you his treasure!")

    else:

        print("Gobbles you down in one bite!")

# Main Game Loop

playAgain = "yes"

while playAgain == "yes" or playAgain == "y":

    displayIntro()

    caveNumber = chooseCave()

    checkCave(caveNumber)

    print("Do you want to play again? (yes or no)")

    playAgain = input().lower()


Why "Slow is Smooth" in Coding?


Notice the time.sleep(2) lines in the code. In programming, we often want things to happen instantly, but in User Experience (UX), pauses create tension and immersion. Moving "slowly" through the code execution here actually makes the game "smooth" and fun to play!


Challenge for You:

Can you modify the code to:

  1. Add a third cave?
  2. Add a "Health" system where you can survive one dragon bite?


Happy Coding! If you enjoyed this, feel free to follow the blog for more Python tutorials and automation tips.

Comments

Popular Posts

فرصتك للدراسة في ألمانيا: منحة ممولة بالكامل لطلاب الدراسات العليا

How to Deactivate Screen Reader in Kali Linux

Murphy's Law: Expect the Unexpected

Python 3.2.1.14 LAB: Essentials of the while loop

Data Analysis Roadmap 2026: From Excel Lover to Python-Powered Analyst