It takes some time for a novice programmer, such as myself, to learn the ins and out of a new programming language. One of the quickest and fun way to understanding the basics of a new language is to create a simple program in order to gain a little bit of experience and to showcase some of the important concepts of the language .
This, I believe, is the next step in getting acquainted or comfortable in using a new language after successfully executing a much simpler program, “Hello World!”.
Inspired by BitLife – Life Simulator, Path of Adventure Text-based roguelike, and many other similar games, I wanted to make a simple text-based game that provides the user some choices that will lead to different scenarios.
The following learnings are what I will be applying to achieve this simple text-based game:
- Python If…Else Statement – Executes a block of code if the condition is true otherwise, another block of code can be executed;
- Python Functions – Block of code that only runs when called;
- Python User Input – Asks user for input;
- Python Time Module, time.sleep() – Adds delay in the execution of a program.
It is important to organize ideas first before coding and there are various techniques in which this can be achieved such as writing pseudocodes and creating flowcharts but I will be focusing on the latter.
Creating a flowchart will help to visualize and understand the workflow of a program a little easier. A lot of novice programmers and non-technical people can get intimidated with a structured text and so, a flowchart provides a visual aid that helps to make that bit understandable and less intimidating to look at.
More so, a flowchart helps reduce unnecessary codes as it lists each necessary steps to solve certain problems.
Text-Based Game Code Snippet
import time def start(): print("It took a few moments to realize that you\n" "weren't inside the comfort of your home but\n" "you were staring up at rows of darkened treetops.\n" "Anxiety sets in as you realize you weren't\n" "supposed to be there. Suddenly you hear a growl\n" "behind. You: \n") time.sleep(3) print("A. Turn towards the source\n" "B. Get up and run\n" "C. Lie down and accept the inevitable") choice = input("Choice: ") if choice == 'a' or choice == 'A': turn() elif choice == 'b' or choice == 'B': run() elif choice == 'c' or choice == 'C': over()
This is the first 23 lines of code for this simple text-based game, the rest are similar to this code snippet.
This specific function def start() will print the two print statements containing the beginning of the story and the choices available. I put a 3-seconds delay between the two statements using the time module time.sleep() as to not overwhelm the user and give them time to read, after 3-seconds the second print statement will print along with the input function.
If…Else statements was used to make the code execute certain tasks depending on the choices made by the user. If the user inputs ‘a’ or ‘A’, the code will execute a function called turn(), if the user inputs ‘b’ or ‘B’, the code will execute a function called run(), and if the user inputs ‘c’ or ‘C’, the code will execute a function called over().
def over(): print("\nYou died. Wasn't exactly a wise decision.\n" "Do you want to play again?\n" "Press Y for yes\n" "Press N for no\n") choice = input("Choice: ") if choice == 'y' or choice == 'Y': start() else: print("Thanks for playing!") exit()
This is the function def over() which will only execute if the user inputs certain choices that will lead to this scenario.
Here I gave the user two choices on whether they want to play again or not. If the user inputs ‘y’ or ‘Y’, function def start() will be called and the game will start again from the beginning but if the user inputs ‘n’ or ‘N’ (or any another keys), a print statement will print “Thanks for playing!” and the program will stop.
Creating a simple text-based game with Python has only scratch the surface of the great things that could be achieved using this language such as game development, machine learning and artificial intelligence, data science, etc. This simple text-based game is certainly a good starting point that covers some of the few important concepts of not only Python but other new languages as well.