To play just call the number or text the word ‘Play’ to – +44 (0) 118 324 7006 †

51339jW18WL.Image._As a child I loved playing/reading the “Choose Your Own Adventure” books. (See Amazon) The very fact that I was able to decide how my story went was probably one of the first role playing games I ever played. I remember going through all of them in my local library, then waiting patiently for the new editions to arrive.

As a challenge, my girlfriend and I set about recreating the same sort of thing using PHP. She chose to go down the browser route and I decided to try out the Twilio platform which would allow the user to play via their phone, initially using the touch-tone signals and eventually moving onto SMS based play. I’d previously played with Twilio at a very low level, creating a messaging platform for the radio show, The Games Cast.

Twilio lets you use standard web languages to build voice, VoIP and SMS applications via a web API.

I set about creating the main game engine in PHP. Twilio lets you assign URLs to a phone number so I simply set Twilio to send all the call details via a POST command to a URL set up to receive the data, process it and move the player around the game. The URL is an OOP PHP game engine built by me to handle the request and the player session. Once the game has worked out where the player is and what they can do it builds a response back to Twilio, in the form of TwiML (an XML schema which tells Twilio what to do). The TwiML tells Twilio to play an audio file and wait for a response (or for SMS it tells Twilio what text to send)

<!--?xml version="1.0" encoding="UTF-8"?-->

        audio/story_start.mp3
        audio/call_costs.mp3
        audio/grid_forest.mp3
        audio/path_ne.mp3

The game is setup like a grid, each grid square represents part of the map. The grid square can have four routes off (points on the compass) it can give the user an item for their inventory or it can require an item from their inventory (or in fact do both) – for example, the user can find a key, or it can require a key to unlock the door.

To play just call the number or text the word ‘Play’ to – +44 (0) 118 324 7006 †

If the player is playing by calling the game then we needed some pre-recorded audio for each grid square and each item of inventory, remembering to also handle instances where the required inventory item isn’t in the players inventory. The MySQL DB also has text elements for each piece of audio so if the user is playing by text they get the same output. The game engine recognises whether the player is playing by audio or by text – and the player can do either and pick up their same game session on whichever method they choose, as long as its on the same number.

<!--?xml version="1.0" encoding="UTF-8"?-->

    Welcome to The Adventure Game. Navigate the map and reach the end of the story. We hope you have fun playing. Get ready... here goes!
    Texts to The Adventure Game are charged at your normal text rate. You are texting a standard UK number. Check you have enough free 'texts' in your text bundle.
    You're in the middle of a dark and very expensive forest. You're a little lost and quite frankly you'd like to get home to your mum.
    There are paths to the north and the east. Reply with 'north' or 'east' to select your path.

At the moment the game grid is only a 3×3 square and there’s only two items for the inventory. I plan to expand the map and make the game much bigger – then I have a very good use for it.

You can play by either SMS or voice call, the game engine remembers where you were and you can carry on with the story.

To play just call the number or text the word ‘Play’ to – +44 (0) 118 324 7006 †

I’ve put the first iteration of the code up on github: Github: PhoneAdventureGame – be warned though, the code needs a good go through and there are some parts I’m not happy with. The GitHub code also doesn’t have any DB creation scripts yet, I’m going to try and upload those very soon. Also, in this version the start and end grid cells are in a PHP file which can easily be read on GitHub, I’m going to move those into the database shortly. I also need to make the SMS version a little more chatty and to accept things like “Go north” or “Take the north path” rather than being so strict.

Leave a Reply