0% found this document useful (0 votes)
19 views

Building-a-Text-Based-Adventure-Game-in-Python (1)

Â

Uploaded by

ngomyhanh1410
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Building-a-Text-Based-Adventure-Game-in-Python (1)

Â

Uploaded by

ngomyhanh1410
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Buildi g a Text-Ba ed

Adve ture Ga e i Pyt o


This worksheet guides you through Python basics including data types, variables, input, if statements, and loops.
You'll apply each topic to create an adventure game where players make choices, explore, and encounter
challenges. The worksheet covers character creation, decision-making, looping actions, and game conditions.

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:

character_name = input("Enter your character's name: ")


start_location = input("Do you want to start in the forest or the village? ")

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.

C ecki g Player Healt


3 Use if statements to check the player's health and provide appropriate feedback.
More If State e t Exa ple
Choosing a Path:

path = input("Do you want to go left or right? ")


if path.lower() == "left":
print("You find a small stream.")
elif path.lower() == "right":
print("You encounter a mountain trail.")
else:
print("You stand still, unsure of where to go.")

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.")

Counting with Loops: Use loops to track repeated 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? ")

# If Statements for Choices


if start_location.lower() == "forest":
print("You hear the sounds of animals around you.")
elif start_location.lower() == "village":
print("People are bustling around the village square.")
else:
print("Invalid choice, starting in the village by default.")

# Treasure Hunt Loop


found_treasure = False
attempts = 3
while not found_treasure and attempts > 0:
search = input("Do you want to search for treasure? (yes/no): ")
if search.lower() == "yes":
print("Congratulations! You found the hidden treasure!")
found_treasure = True
else:
attempts -= 1
print(f"Attempts left: {attempts}")

# End of Game
if found_treasure:
print("You win!")
else:
print("You ran out of attempts. Better luck next time!")

Summary Questions:

1. What is a variable? Give an example of a variable in your game.

2. How do if statements add choices to your game?


3. Describe a situation in your game where you used a loop.

You might also like