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'{"Jetzt müssen Sie die richtige Tür auswählen, um fortzufahren. (rechts): ":^30}')
if choice == "right":
print(f'{"Du bist in einem Raum mit einem Drachen!":^30}')
choice5 = input(f'{"Willst du gegen den Drachen kämpfen? (ja/nein): ":^30}')
if choice5 == "yes":
if has_sword:
print(f'{"Du hast den Drachen besiegt und das Spiel gewonnen!":^30}')
sonst:
print(f'{"Du wurdest vom Drachen gefressen und hast das Spiel verloren!":^30}')
sonst:
print(f'{"Du hast dich entschieden, nicht gegen den Drachen zu kämpfen und hast den Raum verlassen.":^30}')
print(f'{"Danke fürs Spielen!":^30}')
Versuchen Sie, Ihren eigenen Twist hinzuzufügen
Das ist erst der Anfang! Sie können das Spiel optimieren und Ihre eigenen Ideen hinzufügen. Hier sind einige Möglichkeiten, es aufzupeppen:
Fügen Sie weitere Räume mit unterschiedlichen Herausforderungen hinzu.
Erstelle zusätzliche Gegenstände, die der Spieler finden kann.
Füge Rätsel hinzu, die gelöst werden müssen, um Türen zu öffnen.
Lassen Sie Ihrer Fantasie freien Lauf! ?
Warum es cool ist, so ein Spiel zu entwickeln
Das Erstellen eines CLI-Spiels in Python ist eine großartige Möglichkeit, Codierungskonzepte wie Schleifen, Bedingungen und Benutzereingaben zu üben. Außerdem macht es Spaß! Sobald Sie den Dreh raus haben, können Sie mit der Entwicklung komplexerer Spiele beginnen oder sogar in etwas wie Pygame für grafische Spiele eintauchen.
Fazit
Das ist es! Wir haben ein kleines Abenteuerspiel mit Python erstellt. Es ist ein einfaches, unterhaltsames Projekt, das jeder ausprobieren kann. Bist du bereit, gegen ein paar Drachen zu kämpfen? ? Lass mich wissen, wie dein Abenteuer verläuft!
Viel Spaß beim Codieren!
The above is the detailed content of Creating a Simple Adventure CLI Game in Python: Lets Get Coding!. For more information, please follow other related articles on the PHP Chinese website!