Create your own Classic Text Adventure with Python

Thanks to some awesome suggestions from a few pioneering users, Trinket now supports neat text adventures.

Using our new add files feature Brian introduced a few days ago, a group of parents and teachers from SF Brightworks have made a text adventure game module that students can use to make their own classic adventure games:

Their code is in adventure.py and is imported, so it stays out of the way and students can focus on building the rooms and items in the game.

These games may be simple, but they’re a critical part of getting comfortable with text-based programming.  Many of today’s most famous programmers started out building simple text-based games, and classics like Zork and ADVENT inspired a whole generation of programmers to use computers for creative pursuits.

Using the Trinket above, see if you can alter the text of the existing locations.  Look for how the connections between locations are created and see if you can add another yourself!  Find the line where the ‘key’ is added and duplicate it to add another item in the same space.  Then press Run to see your changes updated in the game.

The coolest part of working at Trinket is seeing what creative things users are doing with code. Got an awesome project?  Get in touch.  If you’d like to help this adventure crew build their module for students, check out their github project and consider contributing!

8 thoughts on “Create your own Classic Text Adventure with Python

  1. could you provide a series of links to junk i need to download to get this started? I’m an old-school text-based RPGer and would love to have this on the comp.

    1. It’s all here. Just copy and paste it. (Click on the “adventure.py” tab above, and copy and paste that into your favorite text editor; save it as “adventure.py”. Then do the same with the “main.py” tab, saving it as “main.py”. Alter both to fit your whims.)

      If you don’t have Python — the programming language employed here — you can get it at http://www.python.org for free (version 3.5.0 just came out!). You might want a nice text editor, too; I’m partial to PyCharm (free in its Community Edition: https://www.jetbrains.com/pycharm/download/), but tastes obviously vary. There are excellent online resources for learning how to use the language. It’s constructed and formatted in a way that *just* *makes* *sense*.

      At any rate, once you have the files from above and Python, you can just run main.py in Python. Free utilities exist (like py2exe and cx_Freeze) that will allow you to bundle your code into a form that even people who don’t have Python can use, if you want to share your adventures with others. Have fun!

    1. Sure. But some of us are *also* fascinated by the exercise of writing our own parsers. This is an excellent way to get those people started — certainly better than my halting attempts using BASIC in the early 1980s.

  2. Traceback (most recent call last):

    File “”, line 8, in

    import urllib.request

    ImportError: No module named request ——-I copied and pasted and got that error is that normal?

  3. Hey,
    I’m trying to implement this as an eye-opening example to a Python based lesson I will be teaching with a group to some middle school students. It seems that the “rub key” phrase in the last few lines of the main code is not the proper way to define a phrase and returns an error. I am trying to find how to properly define a phrase in this framework or change the adventure.py code to make phrases work altogether.

    If you have any insight or help to lend that would be awesome.

  4. Hi there, i was trying this out and it constantly stated:

    Traceback (most recent call last):
    File “C:/Python27/adventure.py”, line 8, in
    import urllib.request
    ImportError: No module named request

  5. Hey,

    Im just getting into python. I am a bit lost in how you are supposed to format the characters. adding rooms is easy. is there a read me or somthing I can look at that shows how this all works together?

Comments are closed.