Building-a-Text-Based-Adventure-Game-in-Python (1)
Building-a-Text-Based-Adventure-Game-in-Python (1)
by pwork
Data Type a d Variable
Identify the Data Type: Write the data type (int, float, str, bool) for each value.
100 → _____
"Mountain" → _____
3.14 → _____
False → _____
Declare Variables: Use Python to create variables for your adventure game.
character_name as a string
health as an integer
has_treasure as a boolean
Example:
character_name = "Alex"
health = 100
has_treasure = False
U er I put
Taking User Input: Ask the player for their character's name and starting location. Use input() to get the player's
responses.
Example Code:
Exercise: Write a Python script that asks for the character's name and age, then prints a message with their name
and age.
If State e t for C oice
Making Choices: Use if statements to create choices in the game. The player can choose where to go or what to
do.
Example Code:
if start_location.lower() == "forest":
print("You see tall trees and hear birds chirping.")
elif start_location.lower() == "village":
print("You walk through the busy village square.")
else:
print("Starting in a random location.")
C oo i g a Pat
1 Use if statements to let the player choose a path, such as going left or right.
Picki g Up a Ite
2 Use if statements to allow the player to decide whether to pick up items they encounter.
Picking Up an Item:
item = input("You see a sword on the ground. Do you want to pick it up? (yes/no) ")
if item.lower() == "yes":
has_sword = True
print("You picked up the sword!")
else:
has_sword = False
print("You leave the sword behind.")
Exercise: Write an if statement to check if a player's health is above 50. If it is, print "You're in good health!"
Otherwise, print "You need to find healing!"
W ile Loop for Repeated Actio
Using a While Loop for Actions: In a game, you may want to allow players to keep searching or fighting until they
succeed or fail.
Example Code:
attempts = 3
while attempts > 0:
choice = input("Do you want to search for treasure? (yes/no) ")
if choice.lower() == "yes":
print("You found a treasure chest!")
break # Ends the loop if treasure is found
else:
attempts -= 1
print(f"Attempts remaining: {attempts}")
if attempts == 0:
print("You ran out of attempts.")
Example Code:
health = 100
while health > 0:
print("You encounter a monster!")
action = input("Do you want to fight or flee? ")
if action == "fight":
health -= 20 # Lose 20 health points each time you fight
print("Your health is now ", health)
else:
print("You flee safely.")
break
if health <= 0:
print("You have been defeated.")
Exercise: Write a while loop that allows the player to keep searching for a "hidden key" until they find it or run out
of attempts (3 attempts total).
Putti g It All Toget er - Mi i Ga e
Using what you've learned, create a mini adventure game. Follow these steps:
1. Character Setup: Ask the player to enter a character name and choose a starting location (forest or village).
2. Decisions: Use if statements to create a path or action (like picking up an item or choosing a direction).
3. Looping Actions: Add a while loop for a repeated action (like searching for treasure or fighting monsters).
4. End Condition: Add a way to win or lose the game based on choices or remaining health points.
1 C aracter Setup
Ask for character name and starting location
2 Deci io
Use if statements for choices and actions
3 Loopi g Actio
Implement while loops for repeated actions
4 E d Co ditio
Define win/lose conditions based on choices or health
Exa ple Solutio a d Su ary Que tio
Example Solution:
# Character Setup
character_name = input("Enter your character's name: ")
print(f"Welcome, {character_name}, to the adventure game!")
# Starting Location
start_location = input("Do you want to start in the forest or the village? ")
# End of Game
if found_treasure:
print("You win!")
else:
print("You ran out of attempts. Better luck next time!")
Summary Questions: