Below is an example of a simple text-based RPG written in Python. Every time you run it, the game will pick a random setting for you before letting you choose your next step!
Random Setting Example
Imagine a setting like this:
“Welcome to the Neon Wasteland - a post-apocalyptic metropolis lit by flickering neon signs and shadowed by towering ruins. Here, high-tech scavengers and outlaw hackers roam the deserted avenues, searching for relics of a bygone era.”
Your adventure will take place in a setting just as vivid and unexpected. Enjoy the mystery of what your world might be!
Python Code for a Simple Text-Based RPG
import random
def get_random_setting():
settings = [
"The Forgotten Realms of Eldara - a mystical land where ancient magic and epic legends intertwine.",
"CyberArcadia - a dystopian cyberpunk metropolis where neon lights mask the corruption underneath.",
"The Savage Wilds - a prehistoric world where dinosaurs roam and nature reigns supreme.",
"Steamheart City - a bustling steampunk metropolis filled with gears, steam-powered inventions, and eccentric inventors.",
"Galactic Frontier - a far-flung region of space where alien species, interstellar pirates, and cosmic mysteries abound.",
"Neon Wasteland - a post-apocalyptic urban sprawl lit by flickering neon backdrops, where rogue hackers and scavengers vie for survival."
]
return random.choice(settings)
def intro():
setting = get_random_setting()
print("==============================================")
print(" WELCOME TO THE TEXT-BASED RPG!")
print("==============================================")
print(f"Setting: {setting}")
print("\nYou are a wandering adventurer in a realm full of mystery and danger.")
print("What will you do on the first day of your journey?")
print("\nChoose your next step:")
print("1. Explore the mysterious surroundings")
print("2. Visit the lively local tavern")
print("3. Head towards the distant, looming mountains")
print("==============================================\n")
def explore():
outcomes = [
"While exploring, you encounter a wise old mystic who teaches you a powerful spell.",
"You stumble upon a hidden cache containing rare artifacts and glittering treasure.",
"A sudden ambush by bandits forces you to think quickly to escape unharmed."
]
print(random.choice(outcomes))
def tavern():
outcomes = [
"At the tavern, rumors of an ancient curse and lost magical relics spark your curiosity.",
"You befriend a mysterious traveler who hands you a cryptic map pointing to secret locations.",
"A brawl erupts in the tavern, and amid the chaos, a stranger slips you a small, mysterious key."
]
print(random.choice(outcomes))
def mountains():
outcomes = [
"The mountains test your endurance as you battle fierce creatures guarding hidden paths.",
"You discover an abandoned fortress high in the peaks, whispered to have been home to legendary heroes.",
"The rugged mountain terrain challenges you with its treacherous trails and biting cold winds."
]
print(random.choice(outcomes))
def main():
intro()
choice = input("Enter your choice (1-3): ").strip()
print("\n==============================================\n")
if choice == "1":
explore()
elif choice == "2":
tavern()
elif choice == "3":
mountains()
else:
print("Invalid choice! Your adventure comes to an abrupt end.")
print("\n==============================================")
print(" THANK YOU FOR PLAYING!")
print("==============================================")
if __name__ == '__main__':
main()
How to Play:
- Copy the code into a file named, for example,
rpg_game.py.
- Run the code using Python (for example, by typing
python3 rpg_game.py at the command line).
- Read the introductory text that shows your randomly chosen setting.
- Make your choice by entering 1, 2, or 3 and see what adventure unfolds!
This simple framework can be expanded with more choices, complex narratives, and added features like inventory management or combat mechanics. Happy coding and may your adventures be epic!