Ever thought about building your own adventure game? Well, you’re in luck! In this post, I’ll walk you through creating a super simple text-based game using Python. It's a fun little project where you can flex your coding muscles and have a laugh along the way. No crazy graphics—just you, your creativity, and a bit of code.
What’s the Game About?
You, the hero, stand before two mysterious doors. One leads to an empty room with a hidden sword, and the other? A fire-breathing dragon! Do you have what it takes to find the sword, defeat the dragon, and claim victory? Or will you meet a fiery end? 😬
Let’s dive in!
Step-by-Step Breakdown
Here’s how we can bring this story to life with code.
1. Ask for the Player’s Name
name = input(f"{'Enter Your Name: ':^30}")
print("Welcome, " + name + ", to the land of adventure!")
The first thing we do is ask for the player's name. You can’t go on a great adventure without a name, right? The input() function lets the player type in their name, and then we give them a warm, heroic welcome.
2. Making the First Choice: Left or Right?
print("You are standing in front of two doors. One is to the left and the other is to the right.")
choice = input(f'{"Which door do you want to choose? (left/right): ":^30}')
The player faces two doors. They get to pick which one to open. Will it be the left or the right? This choice is going to determine their fate, so choose wisely!
3. What Happens Behind the Left Door?
if choice == "left":
print(f'{"You are in a room with no doors. It is empty.":^30}')
If the player chooses the left door, they end up in an empty room. It looks boring, but don’t give up just yet! There's something cool hidden here—a sword! 💪
4. Finding the Sword
if choice3 == "yes":
print(f'{"You see a sword on the ground.":^30}')
choice4 = input(f'{"Do you want to take the sword? (yes/no): ":^30}')
if choice4 == "yes":
has_sword = True
print(f'{"You took the sword!":^30}')
If they decide to look around, they’ll find a sword lying on the ground. This is where they can choose to pick it up or leave it behind. If they grab the sword, a flag has_sword = True gets set, which means they’re ready for battle later!
*5. Facing the Dragon 🐉
*
_if choice == "right":
print(f'{"You are in a room with a dragon!":^30}')
choice5 = input(f'{"Do you want to fight the dragon? (yes/no): ":^30}')
if choice5 == "yes":
if has_sword:
print(f'{"You defeated the dragon and won the game!":^30}')
else:
print(f'{"You were eaten by the dragon and lost the game!":^30}')
_
Eventually, the player needs to face the dragon in the right room. If they remembered to take the sword earlier, they can fight and win! 🎉 But if they skipped the sword, well... it’s game over! 😬
6. Wrapping It Up
print(f'{"Thank you for playing!":^30}')
At the end, no matter what happens, the game says a nice "Thank you for playing!" because we're all winners here (even if the dragon had lunch).
Full Code
Here’s the full game in Python:
name = input(f"{'Enter Your Name: ':^30}")
print("Welcome, " + name + ", to the land of adventure!")
print("You are standing in front of two doors. One is to the left and the other is to the right.")
choice = input(f'{"Which door do you want to choose? (left/right): ":^30}')
has_sword = False # Flag to track whether the player has taken the sword
if choice == "left":
print(f'{"You are in a room with no doors. It is empty.":^30}')
choice2 = input(f'{"Do you want to stay here? (yes/no): ":^30}')
if choice2 == "yes":
print(f'{"You are still in the empty room.":^30}')
elif choice2 == "no":
print(f'{"You are back in front of the two doors.":^30}')
else:
print(f'{"Invalid choice. Please choose yes or no: ":^30}')
choice3 = input(f'{"Do you want to look around? (yes/no): ":^30}')
if choice3 == "yes":
print(f'{"You see a sword on the ground.":^30}')
choice4 = input(f'{"Do you want to take the sword? (yes/no): ":^30}')
if choice4 == "yes":
has_sword = True
print(f'{"You took the sword!":^30}')
else:
print(f'{"You left the sword.":^30}')
print(f'{"You return to the two doors.":^30}')
while choice != "right":
choice= input(f'{"Now, you must choose the right door to proceed. (right): ":^30}')
if choice == "right":
print(f'{"You are in a room with a dragon!":^30}')
choice5 = input(f'{"Do you want to fight the dragon? (yes/no): ":^30}')
if choice5 == "yes":
if has_sword:
print(f'{"You defeated the dragon and won the game!":^30}')
else:
print(f'{"You were eaten by the dragon and lost the game!":^30}')
else:
print(f'{"You chose not to fight the dragon and left the room.":^30}')
print(f'{"Thank you for playing!":^30}')
Try Adding Your Own Twist
This is just the start! You can tweak the game and add your own ideas. Here are some ways to spice it up:
Add more rooms with different challenges.
Create extra items for the player to find.
Add puzzles that need to be solved to unlock doors.
Let your imagination run wild! 🎮
Why It’s Cool to Build a Game Like This
Building a CLI game in Python is a great way to practice coding concepts like loops, conditionals, and user input. Plus, it's fun! Once you get the hang of it, you can start making more complex games, or even dive into something like Pygame for graphical games.
Conclusion
That’s it! We built a little adventure game with Python. It’s a simple, fun project that anyone can try. So, are you ready to fight some dragons? 🐉 Let me know how your adventure goes!
Happy coding!
Top comments (0)