A PHP port of the Python 'adventurelib' library

dev-master 2019-03-20 14:46 UTC

This package is auto-updated.

Last update: 2023-11-21 01:29:35 UTC


The PHP Adventurelib library is a PHP implementation of the Python adventurelib library, trying to stay as faithful to the functionality as possible.

There are differences between how the two languages function that make the interfaces a bit different, but the spirit of the library is the same: to provide a simple structure to create text-based adventure games.

Making a simple game

Here's an example of creating a simple one-room game. In a PHP file (such as game.php) put the following:


require_once __DIR__.'/vendor/autoload.php';

$game = new \Adventurelib\Game();
    new \Adventurelib\Room('Test Room 1', 'Just a basic room with not much in it')



Then you can run this game on the command line: php game.php. You'll be presented with a '>prompt waiting for your input. In a simple game like this, only one command is included:help`.

Adding commands

To add in additional commands, use the \Adventurelib\Command class to create an instance. The first parameter is the command match pattern and the second is the closure to execute when the command is called. For example, to add a "look" command to show information about the current room, you'd use this code:

$look = new \Adventurelib\Command('look', function($game) use ($cli) {
    $room = $game->getCurrentRoom();
    echo $room."\n";

    // List out items in the room
    $itemList = [];
    foreach ($room->items as $item) {
        $itemList[] = $item->getName();
    echo "You see ".implode(", ", $itemList);

    // List the exits
    echo 'You can exit '.implode(', ', array_keys($room->getExits()));
$game->addCommand('look', $look);