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.

Updates

Ok, no proper updates for a time, so here’s some now:

Baby:

Nathan due to be christened on the 2nd of January – personally I’m not bothered about this, but as I’d like him to go my old school (which is closest and will be easiest to get him into) he has to be christened. Current weight: 10lbs 1oz, up from 6lbs 1/2oz — getting bigger. He’s a good baby, crys when hungry (and, between 9:00pm and 12:00midnight – for reasons unknown although it’s most likely cus he’s tired). Currently Annette feeds him him in the middle of the night and during the day (mornings) and then we share it, until 9:00pm when I get him. Which would work out great but it’s fairly dented my working at the drawing table time.

College:

I’ve three assignments to complete for return to college, an excel spreadsheet (done – but still have ‘brief’ essay to do), a set of diagrams (mostly done) and a website (mostly done) these will eat into my holiday time, when I plan to get some drawing done. No word on the last big assignment which I’m expecting to fail (although I did expect to fail the last one and I got a distinction – along with almost everyone else in the class).

Work:

I tend to steer clear of mentioning the day job – the blog is for personal and drawing and that’s the way it’ll stay.

Other things:

Been playing with programming on the mac (about the only thing I can do while nursing Nathan is type one-handed while holding a bottle under my chin) and I’ve been getting into cocoa and objective-c programming, it’s bringing back memories – I used to program for fun waaaay back when I was 16/17 (programming the Amstrad CPC464 with it’s green screen and three screen modes and horrible blocky resolution).

Podcasts:

Despite a number of false starts – done nothing. I’m determined to do something, although I’ve been deterermined to get back to the drawing board for the past four weeks and not a great deal has come from that – my room currently looks like a council tip (and will continue to do so until I get all this rubbish filling it taken to the council tip).

Anyways, for enquiring minds: I’m fine, if I didn’t have college and an (unknown) deadline for The Megazine (not warhammer as I’d previously said) I’d be having a whale of a time, as it is the lack of drawing is worrying me (and may be the root cause of my old stomache pain – do a google search for ‘pain under left rib’ and see the startling number of results which amount to ‘I have that too – but I don’t know what it is’).

Anyhue, off to do some work (coming soon: the annual review)