How to Write a Text Based Game Script in PHP

Written by kevin walker
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Write a Text Based Game Script in PHP
(Digital Vision./Digital Vision/Getty Images)

Text-based games, or interactive fiction games, are the predecessors of modern, graphical adventure games like Myst. However, a vibrant fan community still exists around the creation of text-based games. Most interactive fiction authors use dedicated languages like Inform and TADS. However, some choose to develop games with general programming languages like PHP. A basic text-based game is fairly simple: the computer keeps a list of rooms, each with a name, description and list of connections to other rooms. The player is asked for a command and gives directions to navigate the world.

Skill level:
Easy

Other People Are Reading

Instructions

  1. 1

    Create a text file in your favourite plain text editor (such as Windows Notepad or jEdit). Name it "phpIF.php."

  2. 2

    Paste the PHP tags on the first and last lines of the file:

    <?php

    ?>

    All the code for the rest of the tutorial will go between these.

  3. 3

    Paste the code for the Room class:

    class Room {

    protected $description = "";

    protected $name = "";

    protected $rooms = array(

    "ne" => NULL,

    "n" => NULL,

    "nw" => NULL,

    "e" => NULL,

    "c" => NULL,

    "w" => NULL,

    "se" => NULL,

    "s" => NULL,

    "sw" => NULL

    );

    public function __construct ($n = "", $desc = "") {

    $this->description = $desc;

    $this->name = $n;

    }

    public function get Description () {

    return $this->description;

    }

    public function get Name () {

    return $this->name;

    }

    public function set Room ($direction = "c", $room) {

    $this->rooms[$direction] = $room;

    return True;

    }

    public function getNewRoom ($direction = "") {

    return $this->rooms[$direction];

    }

    }

    This stores three variables: the room's description, name, and an array of connections to other rooms, with the direction to the other room used as a key.

  4. 4

    Paste the following to create the rooms:

    $start Room = new Room ("First Room", "A small room. There is a door to the north.");

    $second Room = new Room ("Second Room", "A short hallway that ends in a dead end. There is a door to the south.");

    $start Room->set Room("n", $second Room);

    $second Room->set Room("s", $first Room);

    $current Room = $start Room;

    This creates two rooms and gives them names and descriptions. It then connects them with a doorway: north from the first room and south from the second room. Finally, it sets the player's current room as the starting room.

  5. 5

    Paste the following for the main loop, which will repeat until the player quits:

    $play = True;

    while ($play) {

    print $current Room->get Name();

    print $current Room->get Description();

    $input = readline("(Enter your command. Type QUIT to quit.) >");

    if ($input == "QUIT") {

    $play = False;

    } else {

    if ($input == 'nw' ||

    $input == 'n' ||

    $input == 'né' ||

    $input == 'e' ||

    $input == 'e' ||

    $input == 'e' ||

    $input == 'e' ||

    $input == 'e' ||

    $input == 'e') {

    $current Room = $current Room->getNewRoom($input);

    }

    }

    }

    This prints the room name and description and gets the player's command, before comparing it against the possible commands.

Tips and warnings

  • Commands valid within the room can be added to the room's array of options. For example, in addition to options like "n" and "nw," you could add options like "open door."

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.