Home Backend Development Python Tutorial Creating a Simple Adventure CLI Game in Python: Let&#s Get Coding!

Creating a Simple Adventure CLI Game in Python: Let&#s Get Coding!

Sep 07, 2024 pm 02:00 PM

Creating a Simple Adventure CLI Game in Python: Let

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}')
Copy after login

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: Let&#s Get Coding!. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

What are regular expressions? What are regular expressions? Mar 20, 2025 pm 06:25 PM

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

What are some popular Python libraries and their uses? What are some popular Python libraries and their uses? Mar 21, 2025 pm 06:46 PM

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

See all articles