Ok, this isn’t what you expect from yer humble blogger, but it’s on my mind and it’s what you’re getting, so here we go.
I’ve been helping my son write a simple adventure game, and I figured it might be fun for any other parents looking for ways to write an adventure game in Python.
I used to write adventure games in Amstrad Basic, so I’m trying to apply all the knowledge I learnt there here, so let’s start with some basics. I like to draw a map…
This, very crude map, is required for a couple of reasons, every room here needs a unique number so that we can, for example, say in room 2 if we go west we arrive at room 3.
The map can be anything (we decided on a haunted mansion, you could draw a different map and call it a space ship or a dungeons and dragons type dungeon)
Python has a few ways we can use to encode this, I went with the simplest which was a very simple list – a list is a collection of things, can be a collection of numbers [1,2,3,5] or strings (ie text) [“alpha”,”beta”,”gamma”,”delta delta delta”] or a list of LISTS (mindblowing, I know) [ [1,2,3], [4,5,6], [7,8,9] ]
You can access any element in a list by referring to its index – so say we call our list numbers and it contains [15,16,17,20,50] then we can refer to the first item on the list by looking at item 0 (lists start from 0 going up) numbers = 15, numbers=20
So for an adventure game we’re going to use a list of lists – keeping a super rigerous structure means we can always refer to things in it that will work.
Python has a special keyword “None” which means … well, it literally means nothing. Sometimes we need to refer to Nothing.
We’re going to create a list of rooms, and this list of rooms will have, in every element some data about the room. Because I drew the map with room 1, I’m going to set room zero as a None list (if I was a good programmer, I’d go back and create a room zero, but for reasons that might become apparent the Number 0 and the word None can both mean the same thing, so best to avoid any possibility of a room zero)
Each room will have the following information: a title, a description and a set of exits – north, south, east and west. If the room doesn’t have an exit in a particular direction we specify the direction as None
[“Entrance”,”The main entrance to an old mansion. It looks dilapidated, old paintings hang on the walls looking as tired as everything else”,None,2,None,None],
[“Base of the Stairs”,”Standing at the base of the stairwell, it looks too dangerous to climb. Though there does appear to be an exit below it, to the south and a door that looks like it opens to an overgrown jungle is to the west.”, 2,7,3,None],
(and so on)
I then set up a variable containing the current room number (just called room)
room = 1
And now if we wanted to see our current location, rooms[room] would be the title of the current room. Movement is simply a matter of altering the value of room (going South from Room 1 is simply a matter of setting room=2)
Next we want to create a simple loop that displays the current location, displays any directions we can go in and then asks the user what they want to do.
(Python, by the way, is a bloody stickler for where spaces and indents are allowed, talk about yer grammar nazis)
Out loop can look like this:
alive=True # the has here means anything after the hash is a comment, so this is a comment.
while alive: # we keep doing this block (everything below the while) until the variable alive becomes False (which on this basis it never will)
#python needs any block to have an indent…
print(rooms[room]) #print title
if rooms[room]: print(“You can go North”)
if rooms[room]: print(“You can go South”) #etc, you can figure out the rest..
command = input(“What now?”)
And I’ll leave it there. What was fun about this exercise is showing Nathan how each small thing can do something different – showing him how to print “You can go North” he was able to figure out “You can go South” etc.
The next big thing is to break the command down in to a bunch of words and interpret those words so we can travel to locations. Python, it turns out, is pretty handy for splitting words up, so we did this:
command = command.lower() #converts the text in command to lowercase.
words = command.split() # split the command into a list of words, splitting where ever there’s a space
# and now we check movement.
if words == “north” and rooms[room]:
# user has typed North and we’re able to go north…
room = rooms[room] # so we go north, by moving to the room that is north of the current room
Again, knowing how to go North, and with a bit of prompting, Nathan was able to extrapolate how to go South and West and suddenly we had a walking adventure.
Next big bits were to add objects and let the user lift things as well as do stuff (we added a key which you could use to open a chest of drawers to find a diary) we’ve also got to add some monsters and a combat system, but that remains a future exercise.
Anyway, apologies if this is dull – but it’s your own fault for reading this far. Some of the code above may not work exactly (I’m typing it in to wordpress so syntax errors are sneaking in) but it should be a good starting point if you have python or pythonista on the ipad and want to help an adventure loving kid to start program their own adventure game.