Naturally we want to take advantage of all of PHP's features, so we're going to design our world using XML and load it into the game using SimpleXML. Each part of the world will be called a "room", and will have a name, a description, and information on the directions available. Thus, in XML, a room will look like this:
<ROOM>
<NAME>The pub</NAME>
<DESC>The pub is full of drunken revellers - the war against the orks has been won, and the fighting heroes have returned home</DESC>
<NORTH>1</NORTH>
<SOUTH>2</SOUTH>
<WEST>3</WEST>
<EAST>4</EAST>
</ROOM>
Our world will consist of a collection of ROOM elements, and SimpleXML will convert that to an array of ROOM objects. Thus, the numbers inside NORTH, SOUTH, WEST, and EAST point to array indices - if our Pub is the first item in the world, it will be room 0, so going north will jump to room 1.
Planning a text-based adventure world can be difficult, and the easiest way to make sure you get things correct is to use a program like Excel to plan your rooms. The game world we're going to use has eight rooms in total, so it is fairly easy. Here is the full XML for the rooms - save it as gameworld.xml:
<WORLD>
<ROOM>
<NAME>The pub</NAME>
<DESC>The pub is full of drunken revellers - the war against the orks has been won, and the fighting heroes have returned home</DESC>
<NORTH>1</NORTH>
<SOUTH>2</SOUTH>
<WEST>3</WEST>
<EAST>4</EAST>
</ROOM>
<ROOM>
<NAME>Your house</NAME>
<DESC>Your house overlooks the Lamb and Lion pub, which makes it easy to stumble home when drunk.</DESC>
<NORTH>-</NORTH>
<SOUTH>0</SOUTH>
<WEST>6</WEST>
<EAST>7</EAST>
</ROOM>
<ROOM>
<NAME>A shady alley</NAME>
<DESC>The alley is dark with a peculiar mist making it hard to see the other side. You can hear someone whistling poorly.</DESC>
<NORTH>0</NORTH>
<SOUTH>5</SOUTH>
<WEST>-</WEST>
<EAST>-</EAST>
</ROOM>
<ROOM>
<NAME>Castle</NAME>
<DESC>The castle was built hundreds of years ago when the city was much richer than it is now.</DESC>
<NORTH>6</NORTH>
<SOUTH>-</SOUTH>
<WEST>-</WEST>
<EAST>0</EAST>
</ROOM>
<ROOM>
<NAME>City walls</NAME>
<DESC>The city walls are eight metres high and two thick, and have kept invaders out for centuries.</DESC>
<NORTH>7</NORTH>
<SOUTH>-</SOUTH>
<WEST>0</WEST>
<EAST>-</EAST>
</ROOM>
<ROOM>
<NAME>Misty alley</NAME>
<DESC>A dangerous-looking man stands before you holding a small dagger. "Come any closer and I kill yer!"</DESC>
<NORTH>2</NORTH>
<SOUTH>-</SOUTH>
<WEST>-</WEST>
<EAST>-</EAST>
</ROOM>
<ROOM>
<NAME>Stables</NAME>
<DESC>The stables are full of sleeping horses, but at least it is nice and warm in here.</DESC>
<NORTH>-</NORTH>
<SOUTH>3</SOUTH>
<WEST>-</WEST>
<EAST>1</EAST>
</ROOM>
<ROOM>
<NAME>Gardens</NAME>
<DESC>The gardens are peaceful and surprisingly of flowers for this time of year.</DESC>
<NORTH>-</NORTH>
<SOUTH>4</SOUTH>
<WEST>1</WEST>
<EAST>-</EAST>
</ROOM>
</WORLD>
As you can see, the dead ends have been marked with a "-" in the direction information - we will make sure the script checks for this special value in order to stop people exiting the game world.
Want to learn PHP 7?
Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!
Next chapter: Text game v1 >>
Previous chapter: Making games
Jump to:
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.