Creating a Text-Based Adventure and Quiz Game in Python

Posted in Web Development on July 2016

As never-ending web technologies continue to grow, I do my best to stay relevant and knowledgeable. When I first learned of Python, I was interested in it’s automation abilities. I eventually dug into Python when I found out how quickly and easily someone can scrape the web for information. Nevertheless, understanding the basics of a new language is obviously vital and one of the first personal projects I created, to begin learning the ins-and-outs of the language, was a simple text game. Below, I have two examples. The first is a text-based game that, depending on the users’ choices, branches into different scenarios. Think of the old Dungeons and Dragons game, but digital. Or the old Zork games. I know, I know. Who cares about text-based games when modern game graphics are bordering on reality. Well, not everyone fits the same mold. Recently, text adventure games have made a huge comeback. The second example is a quiz game that calculates the users’ final score—mainly building from the first example. Both are simple examples to help build knowledge and understanding of Python.

A few things to know if new to Python. First, it’s a stickler with formatting. Meaning, make sure your indents are nested correctly. Second, this is based on version 3 of Python. Lastly, to get started with the language on your own computer, and not use the emulator below, you will want to visit Python’s website for more information.

Text-Base Adventure Game

Press the play button to begin.

Things to Know about the Code Above

I’ve made the code’s readability as easy as possible with inline comments and \n
added to strings for better comprehension. Using web emulators, especially when developing a story driven application, can be difficult to read and write. Also, in order to limit code, I used one print statement instead of three (it’s more of a stylistic practice, not required) starting on line 18.

Python has a lot of modules, but, trying not to cause too much confusion for developers when coding, many are left out so you will need to import. Nevertheless, the first line imports the time module. It’s slowed to one second, but can be increased to give the reader more time to finish reading the previous sentence.

If somewhat familiar with coding, most of it should make sense, such as if, elif, and else statements. The only oddity, but will make a text-based game dynamic, will be how the user answers the questions. If a particular answer is given, the user will be branched to a separate section. Now, my code only scratches the surface of branching, but you can send a user into 20 different paths if you want. Below is a flowchart to make the branching a little easier to understand.

Quiz Game with Scoring

Things to Know About the Code Above

Much simpler than the text-based adventure game, the quiz uses the same basic ideas. Such as: print, variables, and the time module. In this case, instead of using sword and flower as a variable, it uses correct. When a user selects the correct answer (i.e. correct += 1) they will be given one point. If incorrect, they will receive zero points. Finally, at the end, correct will be printed again to display how many were answered correctly.

What Else Can Be Accomplished?

Creating text-based games are one of the most basic uses of Python, which is a great starting place for anyone wanting to learn. Moreover, I work with developers who want their young children to learn coding, and this is such a great basis to teach kids Python. Not only will it develop coding skills, but cognitive skills as they create questions and answers.

If you’re looking for a similar project, creating an adaptive quiz game is a great idea. You have the ability to led users into particular directions, which open possibilities to create a GRE-like system. Meaning, if someone selects an answer, you can increase or decrease the hardness of the following question. Nevertheless, Python is relatively easy to learn and its uses are boundless. If looking for a starting point, creating a text-based game is a wonderful and informative beginning.

Back To Top