Build an Adventure generator
By the end of this tutorial you'll know how to:
- Store information in variables
- Create lists
- Use Python's random module
- Display dynamic text
- Create your own simple text-based game ideas
Why This Project Is Cool?
Game developers often need random events, loot drops, character names, and quests. Today we're building a tiny adventure generator that can create a new story every time you run it.
Step 1: Import Random
Python comes with a built-in module called "random".
Code
import random
This gives us access to tools that can make choices for us.
Step 2: Create Our Adventure Lists
Code
import random
heroes = ["Knight", "Wizard", "Rogue", "Hunter"]
locations = [
"Dark Forest",
"Ancient Castle",
"Crystal Cave",
"Lost Temple"
]
enemies = [
"Dragon",
"Goblin King",
"Giant Spider",
"Necromancer"
]
Think of these as buckets full of possibilities.
__________
__________
Step 3: Pick Random Items
Code
import random
heroes = ["Knight", "Wizard", "Rogue", "Hunter"]
locations = [
"Dark Forest",
"Ancient Castle",
"Crystal Cave",
"Lost Temple"
]
enemies = [
"Dragon",
"Goblin King",
"Giant Spider",
"Necromancer"
]
hero = random.choice(heroes)
location = random.choice(locations)
enemy = random.choice(enemies)
"random.choice()" grabs one item from a list.
Step 4: Generate The Story
Code
print(f"A {hero} entered the {location} to defeat the {enemy}!")
The "f" before the quotation marks lets us insert variables directly into text.
Final Code
import random
heroes = ["Knight", "Wizard", "Rogue", "Hunter"]
locations = [
"Dark Forest",
"Ancient Castle",
"Crystal Cave",
"Lost Temple"
]
enemies = [
"Dragon",
"Goblin King",
"Giant Spider",
"Necromancer"
]
hero = random.choice(heroes)
location = random.choice(locations)
enemy = random.choice(enemies)
print(f"A {hero} entered the {location} to defeat the {enemy}!")
Example Output
A Wizard entered the Crystal Cave to defeat the Dragon!
Run it again and you'll get a completely different adventure.
Challenge Mode ⭐
- Tresure Rewards?
- Magic Items?
- A Random Quest Giver?