Monday, 7 December 2020

Learn to Program Java Adventure Games

I’m pleased to announce my latest book which explains how to write a retro-style text adventure in Java using object oriented techniques. I already have a book on programming adventure games in C#. The new Java book explains all the techniques described in that book so that you can parse user commands (e.g. “put the sword into the sack”), take and drop objects, move around a map, save and load games and so on. But it also goes much further, to explore more advanced techniques.

For example, it can use adjectives to distinguish between similar objects. Maybe there are three swords here. The user says “take sword”. The game may now reply “Do you mean the gold sword, the silver sword or the Elvish sword”? Or the player may write commands such as “Put the small gold ring into the big brown wooden box.” 

I also explain how to use recursion to ‘unwind’ networks of objects into linear lists. For example, if you have a sack containing three boxes and each box contains three treasures, you would need to navigate a network of objects to locate each treasure. You can either do that recursively each time the player wants to interact with the treasure or you can create a linear list of treasures so that your code can iterate through that list (for 1 to 10, say) instead of recursing down the network of objects.

And I’ve added a ‘mass’ property to objects. The mass is defined as a value that indicates how difficult that object would be to carry. It’s not really ‘mass’ as a physicist would understand it! So a huge but very light kite might have the same mass as a small but very heavy gold brick. Every time the player picks up an object, the ‘mass’ being carried increases until no more objects can be carried. And it’s no good cheating by putting massive objects into low-mass containers (say, a sack) and then carrying that container. Thanks to recursion, my code calculates the entire mass of containers and everything they contain!

The Little Java Book of Adventure Game Programming is available in paperback or Kindle format, from Amazon stores worldwide:

Buy from Amazon US:

Buy from Amazon UK:

You can sign up to the Bitwise Books mailing list and download all the source code free. I hope you have fun writing some games!