The Art of the Text Adventure: A Guide to Building Text-Based Games With Python Chapters: 1. A Fitting Background (some information before you get started) a. The appeal of a text-based game as a learning tool. b. The appeal of Python as a first language. c. Setting up the environment. 2. A Vivid Description (how to use Python to describe the scene) a. Printing out a scene description. b. Using Timing for better gameplay. 3. Creating a Personalized Experience (capturing information from the player) a. Getting user input to personalize their character. b. Using string variables to store that input. c. Printing out stored information with formatted strings. 4. Two Paths, Diverged (letting the player make some decisions) a. Using if, elif, and else statements with input to actually implement gameplay. 5. Materialistic Pursuits (giving the character some health, cash, an inventory, and other information) a. Using variables of other types (integer, float) to store information about the character. b. Using lists for inventory storage. c. Using dictionaries to more easily describe a character. 6. A Forgiving Attitude (allowing for user error in inputs) a. Using booleans & while loops to make sure that input is valid. 7. Keeping A Detailed Account (printing out information about the character) a. Using a for loop to tell them everything in their inventory. b. Using .items() to give them character information from a dictionary. 8. Algorithmic Practices (creating reusable code for easier game creation) a. Using functions for different monotonous tasks. 9. Class Structure a. Storing the whole game in a class, because it makes more sense. 10. A Final Reflection a. Going over what we learned. Bonus Sections: 1. A Turtle Talker a. Printing slowly, character by character.
Chapter 1: A Fitting Background The Appeal of a Text-Based Game as a Learning Tool As you go down this path of learning how to build one yourself, it might be helpful to know a bit more about text-based games and their roles in combining programming with creative storytelling. Since the creation of the first video game, Tennis for Two in 1958 by Physicist William Higinbotham, the world has been overrun with millions of game titles and concepts, innovations and cultural overhauls. The advancement of video game technology has worked as a metric to judge technology’s improvement over time. A faster computer means a faster game, a higher resolution screen means a better looking game. Tennis for Two was played using two brick-like controllers with a dial and button, the graphics a blue dot bouncing back and forth on a small cathode-ray screen about 5 inches across. A truly immersive experience. The best achievement of the game was its purpose: to show players the real-world creative applications of advancements in computer technology. 19 years later, in 1977, Colossal Cave Adventure was released. Will Crowther, its creator, had built an incredible display of creativity in this, the world’s first text adventure. The player would control a character that traveled through a dangerous cave, finding treasure and points. Escaping alive at the end, success was directly tied to how many points you could accumulate. This game involved thought and problem solving; it was immersive and creative; it was fun. Text-based games excel at creating an atmosphere for the player to enjoy by mainly just printing text descriptions out of scenes and taking in instructions on what to do:
These adventures aren’t just fun to play, but fun to make. Games are entertaining to develop, to creatively put together and craft. Text-based games don’t require you to be adept at using a complicated game engine, but rather you can program a game from the bottom-up, learning the concepts and applying them in whatever ways you can imagine. Programming is logical and uses discrete, rigid processes to develop larger, more flexible algorithms. It is also creative, and commonly requires you to problem-solve and come up with methods that apply the basic concepts in interesting ways. The case for learning to program text-based games is that you will find yourself, after understanding a few concepts, with the ability to turn your ideas for stories and adventures into actual, playable games. Along the way, learning how to code is a pretty useful skill to pick up too.
The Appeal of Python as a First Language Now that my expert persuasive skills have had time to settle in, no doubt you’ll be wanting to start learning how to program a game. Programming, although consisting of the same process, will be a little different based upon the language you choose to code in. Coding requires typing out commands for the computer to interpret, and those commands will be different from language to language, even when they do the same thing. In this book, you’ll be starting off by learning how to code in Python (named after the comedy troupe Monty Python), one of the most popular languages in the world. It’s very good for beginners because the syntax is so straight-forward and easy to understand. The language is 30 years old, and the internet is filled with guides and experts ready to provide help for whatever questions you have along the way. You can learn how to code in Scratch, if you’d like, but it's a lot harder to develop that skill into an actual typed out language than to simply start from scratch (pun intended). Python allows for an easy entrance into coding without any prior knowledge, and has applications in the real world, with companies like Google and Amazon commonly using the language.
Setting up the Environment Just a warning, this will require a computer.
Before we start coding, you’ll have to set up a virtual environment to program in. These are called IDEs (Integrated Development Environments), and consist of a text editor and a compiler or interpreter. The text editor is where you write your code, while the compiler converts the code into a lower-level language for the computer to understand when you “run” your program. Which means Google Docs won’t cut it.
A. If you are hesitant to download anything, you can head over to https://replit.com/ and sign-up for free with an email. Then, once you’re in, you can create a new Python file whenever we want to move into another mini-project. They’ll all be saved to your profile, along with any other coding you feel like doing. They also give you access to almost every language imaginable for no charge. Not sponsored but still a great environment.
B. If you want something more traditional, let’s talk through getting the official Python IDE downloaded. 1. Whether you’re on Windows, Mac, or some other monstrosity, you’ll head over to https://www.python.org/downloads/. 2. Click the yellow ‘Download’ Button. You’ll be getting Python 3.9.7 or later, if another one has come out. Make sure it’s Python 3, not Python 2 you download because they have different syntax.
3. Open up the downloaded executable and follow the instructions to download Python. 4. On your computer, you can find the Python 3.9.7 Shell (or what your version is) and open it. Now you have your very own Python shell open, that’s a command line interpreter where you can write very basic programs. You’ll want to click ‘File’ and ‘New File’ on the top left of the window. This will open a text file in the Python IDLE where you can write the longer programs we will create. Clicking ‘File’ and ‘Save’, you can name and put this program wherever you want on your computer and it will be saved as a “.py” file. Going back into the window, you can click ‘Run’ and ‘Run Module’ which will interpret the code in the Python file back in the shell. If you ever make any changes to your file, remember to save before the shell can run the new version.
C. If you want something more professional, some other suggestions are: PyCharm, Visual Studio Code, and Atom. If you want to use them, YouTube has some great guides that are made by programmers much better at explaining setup than I am.
Chapter 2: A Vivid Description Printing Out a Scene Description Open up your tool of choice (see Chapter 1 for download assistance) and stretch your fingers, it’s time to embark on a journey of creation. First thing you’ll want to do, in order to make sure your environment is properly set up is type this out:
>>> print("Hello, World!") Figure 2.01
If everything is in order, this should display to the screen (wherever your output is going) the words
Hello, World! If it doesn’t, you’ll want to figure out what’s wrong before moving forward. What, you might be asking, are we going to do with the ability to display these very specific words to the screen? We can actually write any characters we want, arranged in any order. Such is the benefit of programming: almost complete freedom. Freedom to, say, print out a description of a scene for the player to read. In order to display something else, we’ll have to see within Figure 2.01 what to change. Taking the print statement, we are able to replace those words encased by “” with what we want to print. For instance:
>>> print("You wake up in a cave, to your right is a door. It’s dark brown wood, moldy and surrounded by a rusted metal outline.") Figure 2.02
If you type this out, the interpreter will respond to you by displaying that exact description from “you wake up...” to “...outline”. So you’ve probably guessed the format here, you can use print("_") to display whatever you desire, just typing the characters inside of the quotations. The print() is what we call a function, it’s named “print” and it performs the specific task of displaying whatever we need to the screen. The quotations around the words simply means “these are characters” and not, say, a variable. We call this printing a string, or a grouping of characters. We’ll talk more about these concepts later in Chapter 3. Main Ideas:
● You can print out any words you’d like with print(""). Practice: ● 2.1 - Print out a text description of a scene, maybe something with a forest or a door. Remember your quotes (“”).
Using Timing for Better Gameplay
Taking a Breath Between Lines When you open a game, is the dialogue all spat out in a single, scary paragraph? No! You’re treated to a nice and user-friendly display of sentences or phrases, one after another, in a digestible duration. We can do this in our game as well, telling our code to wait in between messages so that the player has time to read. Let’s see an example in a greeting:
1 2 3 4
from time import sleep print("Hello,") sleep(1) print("how is your day?") Figure 2.03
This code greets you, waits a second, and then asks how your day is. How nice, but how’s it work? First, in line 1, we made the “sleep” function usable to us. In Python, and in other coding languages, not all the commands are automatically available for use instantly. If you specifically want to make a visual game, or generate a random number, or wait some time in between lines of code, you’ll have to download that functionality into your program before being able to use it. Line 2 is a print statement, just like we went over in the previous section, that this time prints out “Hello,”. Line 3 uses our downloaded command, which is a function just like print that takes in some argument and performs an operation. This function, instead of displaying some given text to the screen, waits a particular amount of time before executing the next line of code. I wrote in ‘1’ and therefore waited one second. If I had written “sleep(2)”, the code would wait 2 seconds before going to line 4, where another print statement is used.
Character-by-character Printing When games display text, they’ll sometimes have time between both paragraphs and individual characters. In Python, this is possible to do but requires some more knowledge to fully be comfortable using. I recommend you see this exercise as a measurement of your learning. Flip to Bonus Section 1 when you think you’re ready. Main Ideas: ● First, by importing the time library with from time import sleep, you can then use
sleep(_) with any number of seconds inside the function. Your program will, reaching that line, wait that number of seconds before moving on. Practice: ● 2.2- Greet the user, with multiple messages, giving them time in between to read each before moving on to the next.