A Text Adventure in Python
Exploring the Standard Library
Jeffrey B. Armstrong
Approximatrix, LLC
Introduction

- Build a text adventure
- with only the standard library
- Basic steps to start a game
- Introduce some fun Python modules
History

- Text Adventures/Interactive Fiction
- Popular when graphics were a “problem”
- Multiple commercial entities
- Still enjoy “cult” following
Necessities
- Command prompt
- Locations to visit
- Game file(s) describing our world
- Puzzles, items, characters, etc.
Command Prompts
- cmd module
- Provides “support for line-oriented command interpreters”
- Absurdly simple to implement
- Might lack some power
Locations
- Room class
- Needs an id, name, description
- List of connected rooms
- List of items in the room
- List of NPCs in the room
Persistent Rooms
- pickle
- Lets us serialize our rooms easily
- Not editable
- Plain ol’ text files
- Hand-editable
- Implies some arbitrary formatting
Persistent Rooms
- json module
- Allows “serializing” our rooms
- Hand-editable
- Expandable via adding fields
Text Wrapping
- textwrap
- Automagically handles our wrapping needs
Data Files
- sqlite3 module
- Simple SQL database
- Store the entire game in a single file
Data Files
- tempfile module
- Make a temporary, working copy of the game
- shutil module
- For copying the clean game file to a temp file
- For creating our saves
What’s Missing?
- Status Bar
- NPCs
- Puzzles
- “Advanced” text parsing
Where We’ve Been
- cmd
- json
- textwrap
- sqlite3
- tempfile
- shutil
Want More?
- https://github.com/ArmstrongJ/
- An older, actual adventure named robotadventure
- http://www.pyweek.org/
- A reason to write your own
- http://www.ifcomp.org/
- If you think yours is worthy…