Text-based Adventure Game using Python














































Text-based Adventure Game using Python



Text-based Adventure Game using Python 

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!



More Articles of Aniket Sharma:

Name Views Likes
Pyperclip: Installation and Working 991 2
Number Guessing Game using Python 683 2
Pyperclip: Not Implemented Error 1033 2
Hangman Game using Python 16820 2
Using Databases with CherryPy application 1676 2
nose: Working 509 2
pytest: Working 512 2
Open Source and Hacktoberfest 868 2
Managing Logs of CherryPy applications 1005 2
Top 20 Data Science Tools 684 2
Ajax application using CherryPy 799 2
REST application using CherryPy 664 2
On Screen Keyboard using Python 5532 2
Elastic Net Regression 815 2
US Presidential Election 2020 Prediction using Python 794 2
Sound Source Separation 1165 2
URLs with Parameters in CherryPy 1635 2
Testing CherryPy application 637 2
Handling HTML Forms with CherryPy 1449 2
Applications of Natural Language Processing in Businesses 511 2
NetworkX: Multigraphs 649 2
Tracking User Activity with CherryPy 1404 2
CherryPy: Handling Cookies 822 2
Introduction to NetworkX 633 2
TorchServe - Serving PyTorch Models 1306 2
Fake News Detection Model using Python 735 2
Keeping Home Routers secure while working remotely 484 2
Email Slicer using Python 2998 2
NetworkX: Creating a Graph 1111 2
Best Mathematics Courses for Machine Learning 551 2
Hello World in CherryPy 681 2
Building dependencies as Meson subprojects 979 2
Vehicle Detection System 1081 2
NetworkX: Examining and Removing Graph Elements 608 2
Handling URLs with CherryPy 537 2
PEP 8 - Guide to Beautiful Python Code 759 2
NetworkX: Drawing Graphs 624 2
Mad Libs Game using Python 645 2
Hosting Cherry applications 613 2
Top 5 Free Online IDEs of 2020 867 2
pytest: Introduction 535 2
Preventing Pwned and Reused Passwords 582 2
Contact Book using Python 2095 2
Introduction to CherryPy 547 2
nose: Introduction 505 2
Text-based Adventure Game using Python 3002 2
NetworkX: Adding Attributes 2290 2
NetworkX: Directed Graphs 1021 2
Dice Simulator using Python 562 2
Decorating CherryPy applications using CSS 834 2

Comments

  • mashallah
    19-May-2022 11:20:20 AM

    mashallah despices you