Text-Based Adventure Game in Python: The best way to learn Programming is by creating Projects.
Project-based learning not only helps in the logic building but it also enhances our problem-solving skill.
There are different types of projects that you can create as a beginner in Programming.
However, if you want to create creative and fun projects in Python, then there is nothing better than creating a game.
Most of the Programmers also started learning to code just because they want to develop their own game.
So, In this article, we are going to create one of the most simple games which is a Text-based Adventure Game using Python Programming Language.
Table of Contents
What is a Text-Based Adventure Game
A Text-Based Adventure Game is a type of game in which a player has to make choices (Yes / No) in every step of the game.
Based on these choices, the storyline changes, and at last, we get to know that whether the player wins or loses the game.
So, In this article also, we have a storyline for our game which we have discussed below.
Here is a diagram that will help you in better understanding the storyline of our game:
As you can see from the above diagram that at the starting of the story, a man came to us and ask for shelter.
Now, you get the two options to choose from, which is saying Yes or No to the man.
So, if you said yes, then after 2 minutes, the police will come to your house and will ask you whether the thief is inside or not.
Now again, you have two options, which is saying Yes or No to the police.
So, if you said Yes, then you will win the game. As the man who was asking for shelter is a thief. On the other hand, if you said no, then you will go to Jail and your Game is Over.
However, At the starting of the story, if you said “no” to the man who is asking for shelter. Then he will attack you.
Now, again you have 2 options, which is knocking him down or not.
So, if you knock him down, then you will win the game. Otherwise, he will kill you and you will lose the game.
Now that you understand the storyline of our game. Let’s take a look at the Python Code of this game.
Text-Based Adventure Game Python Code
Here is a code for our Text-Based Adventure Game in Python:
answer_yes = ["Yes", "Y", "yes", "y"] answer_no = ["No", "N", "no", "n"] print(""" WELCOME! LET'S START THE ADVENTURE You are standing outside of your house and you see a man running towards you and asking for urgent shelter. Will you provide shelter to him. (Yes / No) """) ans1 = input(">>") if ans1 in answer_yes: print("\nAfter 2 minutes, the Police came to your house, and ask you that whether the thief is in your house or not. Will you say (Yes / No)\n") ans2 = input(">>") if ans2 in answer_yes: print("\nYou are an honest person. He was a thief & You won the Game") elif ans2 in answer_no: print("\nYou helped a thief. Now, go to Jail. GAME OVER") else: print("\nYou typed the wrong input. GOODBYE!") elif ans1 in answer_no: print("\nNow, he is trying to kill you. Will, you knock him down? (Yes / No)\n") ans3 = input(">>") if ans3 in answer_yes: print("\nCongrats! He was a thief & You helped the police to catch him with your bravery.") elif ans3 in answer_no: print("\nSorry! You are dead. He was a thief & He killed you. GAME OVER") else: print("\nYou typed the wrong input. GOODBYE!") else: print("\nYou typed the wrong input. GOODBYE!")
As you can see that this is a very simple and easy-to-read Python Code. Now, let’s take a deep look at the working of this code.
Understanding the Code Above
In the first line of this code, we have used the List Variable for storing all the combinations of saying Yes.
Similarly, In the second line of this code, we have used another List Variable to store all the combinations of saying No.
In the next line, we have printed the intro of our game. Then we asked the user to type Yes or No with the help of input() in Python.
So, if the user type Yes, then the code inside the if block will get executed.
On the other hand, if the user type No, then the code inside the elif block will get executed.
At last, if the user types some wrong input, then the code inside the else block will get executed.
So, hope you like the article on Text-Based Adventure Game in Python. We also have an article on Python if elif else statement, which you must take a look at.