A text-based game is an electronic game that uses a text-based user interface, that is, the user interface employs a set of encodable characters such as ASCII or Unicode instead of bitmap or vector graphics.
Most of the developers start their Python journey by making a Text-based Adventure Game. It is similar to Dungeons and Dragons and the path the character takes depends on the player input. It is an easy to implement and fun project.
We will use:
if and elif statements which will take the character on different paths based on the input the player provides.
time module to give delays for extra effects in the game
input() and print() methods to take player inputs and print story
while loop for extra effects
Save the below code in the file named 'text_adv.py'.
import time
print("Welcome to the Adventure Game!\n\n")
name = input("What is your name? ")
age = int(input("What is your age? "))
health = 10
if age >= 12:
print("You are old enough to play!")
wants_to_play = input("Do you want to play? ").lower()
if (wants_to_play == "yes") or (wants_to_play == "y"):
print("Starting...\n")
time.sleep(1) # Adding delays for extra effects
print("Please wait!")
time.sleep(1)
count = 0
while count != 5:
print(".", end="")
time.sleep(2/5)
count += 1
print("\nYou are staring with", health, "health.")
print("Let's play!\n")
time.sleep(2)
left_or_right = input("First choice... Left or Right (left/right)? ")
if left_or_right == "left":
count = 0
while count != 5:
print(".", end="")
time.sleep(2/5)
count += 1
ans = input(
"\nNice, you follow the path and reach a lake... Do you swim across or go around (across/around)? ")
if ans == "around":
count = 0
while count != 5:
print(".", end="")
time.sleep(2/5)
count += 1
print("\nYou went around and reached the other side of the lake.")
elif ans == "across":
count = 0
while count != 5:
print(".", end="")
time.sleep(2/5)
count += 1
print("\nYou managed to get across, but were bit by a fish.")
print("You lose 5 health.")
health -= 5
ans = input(
"\nYou notice a house and a river. Which do you go to (river/house)? ")
if ans == "house":
count = 0
while count != 5:
print(".", end="")
time.sleep(2/5)
count += 1
print(
"\nYou go to the house and are greeted by the owner... He doesn't like you and hits you with a stick.")
print("You lose 5 health.")
health -= 5
if health <= 0:
count = 0
while count != 5:
print(".", end="")
time.sleep(2/5)
count += 1
print("\nYou now have 0 health and you lost the game...")
else:
count = 0
while count != 5:
print(".", end="")
time.sleep(2/5)
count += 1
print("\nYou have survived... You win!")
else:
count = 0
while count != 5:
print(".", end="")
time.sleep(2/5)
count += 1
print("\nYou fell in the river and die...")
else:
count = 0
while count != 5:
print(".", end="")
time.sleep(2/5)
count += 1
print("\nYou enter a jungle and get eaten by a bear...")
else:
print("\nExiting...")
count = 0
while count != 5:
print(".", end="")
time.sleep(2/5)
count += 1
print()
else:
print("\nYou are not old enough to play...")
print("\nExiting...")
count = 0
while count != 5:
print(".", end="")
time.sleep(2/5)
count += 1
print()
We start the game by asking the player their name and age. Next, we check whether the player's age is more than 12 years. If it is we ask whether they want to play. Otherwise we exit.
If the player chooses to play the game we start asking them questions and move the story forward based on their input.
To run the game, enter the following command in a Terminal/Command Line:
python3 text_adv.py
A sample output will look like this.
Welcome to the Adventure Game!
What is your name? Aniket
What is your age? 19
You are old enough to play!
Do you want to play? yes
Starting...
Please wait!
.....
You are staring with 10 health.
Let's play!
First choice... Left or Right (left/right)? left
.....
Nice, you follow the path and reach a lake... Do you swim across or go around (across/around)? around
.....
You went around and reached the other side of the lake.
You notice a house and a river. Which do you go to (river/house)? house
.....
You go to the house and are greeted by the owner... He doesn't like you and hits you with a stick.
You lose 5 health.
.....
You have survived... You win!
Comments
mashallah
19-May-2022 11:20:20 AMmashallah despices you