Adventures in Programming – coding

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[0] = 15, numbers[3]=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

rooms =[

[“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][0] 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][0]) #print title

print(rooms[room][1])#print description

if rooms[room][2]: print(“You can go North”)

if rooms[room][3]: 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[1] == “north” and rooms[room][2]:

# user has typed North and we’re able to go north…

room = rooms[room][2] # 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.

Adventures in Programming

When I was 14 years old, my brother (then 13) and I received a joint birthday/Christmas present – this was not-uncommon. His birthday was 2 days before Christmas, my 3 days after, and we were one year apart (Mark passed away in 1997, if you’re wondering why this is all past tense). In 1994, my mum bought us an Amstrad CPC464 with a green screen, which, if memory serves, were about £399.

I fell in love.

Mark, ended up with the shittier end of the deal though, not fussed on computers (though he liked games) mum and dad ended up getting him a different present. The computer quickly became mine.

Every minute spent with my Amstrad was a delight, the noise of the tape deck and its bizarre mix of tones as it loaded programs, the clack of the mighty keys (hewn from the toughest of plastics, mined down Alan Suger’s working man’s silicon valley pits) and the glowing green screen – literally casting a sickly green spell over me. Decades before the matrix, I knew what it was like to be a lone hacker tapping on green screen console and hoping the universe was more exciting.

And the thing was, it could be.

Buying and playing games was boring, sitting in front of the screen keying in computer listings that allowed you to fly planes dropping bombs on an undefined enemy, to throw bananas at a monkey across the sky or collect squares as a snake all built line by hard won line over several evenings thanks to the back of magazines like Amstrad Computer User and Amstrad Magazine was living in the future.

It set me up for the rest of my life. Later that year, I took a one week job placement for school and found myself there for the next 15 years or so, doing tech support, writing software and sometimes selling computers. It also let me keep connected to comics in a period when I’d let peer pressure convince me comics where for kids, I’d still pick up Computer and Video Games PURELY for the work of Jerry Paris, one of the greatest unsong british comic artists that ever sloshed ink across a page. (Jerry and I are now friends, so that’s come into a lovely full circle).

Anyway, one of the main things I did with my Amstrad was write adventure games. I was obsessed. I fooled myself in to thinking I liked the ability to create mad, wild adventures that could go anywhere or do anything, but really … really… I loved the absolute control I had over the world. I could make the computer do all sorts of things and I could push it in fun, unsurprising ways.

The Amstrad didn’t have much memory, so one of my first little innovations was to create a very basic compression – it was crude, but it worked (how crude? Any letter of the alphabet that was followed by a space would have its ASCII number increased by 64, the space then would be deleted – this let me remove all spaces from a bit of text for storage – I mean it wasn’t neccessary at all for the tiny adventures I’d write, but I thought I was pretty clever for it).

I enjoyed taking the computers rigid data and turning it in to text. Taking an extant adventure game which would tell you, brusquely “You can go: NORTH, SOUTH” and turning it into an adventure with a little more finesse, so it became “You can go North or South” (cleverer than it seems)

My oldest son is now 13, he’s lived his entire life with at least three or four computer’s within easy reach. He’s done a little programming, in scratch, but lately he’s expressed an interest in doing something in python – it’s not a language I’m familiar with, but I figured, why don’t we write an adventure game together. So that’s what we’ve been doing.

And it’s fun.

You know with your help…

New MacBook Pro

With just a little help from you, you could prevent another poor comic artist living another year without an apple computer upgrade (yes, I got one recently but there have now been **two** updates of that model since — **two**!) It’s not a lot to ask, if I’ve in any small measure added anything to your lives. Come on! It’s only £1800 or thereabouts. You miserable gits. Sod it.

You do realise my wife will NEVER let me buy another computer don’t you? this one here, the one I’m typing this on? this is the last one – nothing more after this.
(*maybe if I break it … hmmm…*)

(*breaks down in tears*)

Creative Struggling Site Look

Ok, I’ve spent faaaar too long worrying about how the site looks. I’m offering a reward: a Dredd page to someone who comes up with a nice redesign of the site, preferably to include my artwork and it needs to be a wordpress template (or set). If a Dredd page isn’t tempting enough I’ll offer a single drawn page to your script instead (or, if it’s really a spectacular template, both). Anyway, I’ve reverted to this older template cus it lets me display the blogstrips as full on as possible. I might attempt a redesign for the new year, but I’m trying to avoid the siren like lure of the computer. It’s a useless habit that needs to be put to bed.