Adding a Person
To round our our example game, let's add a person. In fact, let's add a robot butler for the player.
Robutler is a man in the Stateroom. "Robutler stands attentive to your needs." The description of Robutler is "Robutler has four arms, treads, and a shiny metal body." Understand "robot" and "butler" as Robutler.
Add Headings
I7 provides a way to organize code through headings. Headings are any single-sentence paragraphs that begin with "volume", "book", "part", "chapter", or "section". Headings let you organize your story as if it were a book. In addition, the Index keeps track of all of your headings under the "Contents" sub-tab, and lets you jump directly to them. For this game, I would use three headers. "Chapter 1 - The Game World" would be the Stateroom, the Bathroom, and all of the props besides Robutler. "Chapter 2 - Other Characters" would be Robutler and his "every turn" rule. "Chapter 3 - The Sleep Puzzle" would be all of the rules for the sleep puzzle.
It would be cool if Robutler, like an annoying solicitor, followed the player around wherever they went-which right now is just to the bathroom, but still. For this to occur, we need to check every turn to see if Robutler is still in the same room as the player. If not, we need to move Robutler to the player's room and say something about Robutler joining the player. Try adding the following to the game's source.
Every turn:
if Robutler is not visible begin;
move Robutler to the
location of the player;
say "Robutler rattles
into the room, following you.";
end if.
This is the most complex rule we've written so far. "Every turn:" means that the rule is followed every turn. The "if...begin" and "end if" statements surround several actions that occur only if Robutler isn't in the same room as the player. We determine whether or not Robutler and the player are together with the "is not visible" condition. "visible" means the player can see the object. Using "is not visible" prevents Robutler from following the player onto the bed, since the player can see Robutler from the bed. The two actions move Robutler to the player and announce his arrival. To see it in action, run your game and go east.
You Are Getting Sleepy
So far the game is static. Other than Robutler following players around, nothing happens. We can change that by adding a sleep puzzle. Early text adventures often had puzzles where players had to sleep or eat or else they'd die. These days sleep and hunger puzzles are considered too annoying to include in a game. However, for a demonstration game like this, a sleep puzzle will do just fine.
The sleep puzzle will be very simple. Every once in a while, players will be told that they're getting sleepier, and that they should get in bed. If they don't get in bed after enough time has gone by, they'll die. If they get into bed, they win the game.
The basic unit of time in interactive fiction is the turn. A turn consists of the player typing a command and the game responding. In I7 one turn takes one minute, and the game begins at 9 AM. We're not including a clock in the game, so the absolute time doesn't matter to us. What does matter is how long we want to wait between warning messages. Let's have three warning messages. The first will come after 10 turns-in other words, at 9:10 AM. The second will come at 9:20 AM. The third will come at 9:25 AM. At 9:27 AM players will fall down dead.
Down at the bottom of your source, add the following paragraph:
At 9:10 AM: say "You realize that you are becoming exhausted." At 9:20 AM: say "Your exhaustion is threatening to send you crashing to the floor, unable to move." At 9:25 AM: say "You are about to drop dead of exhaustion. You really should get in bed."
These rules occur at 9:10 AM, 9:20 AM, and 9:25 AM, just as we planned. If you run the game and wait long enough, either by repeatedly typing Z to wait a turn or by moving around and looking at things, then you'll see all three messages.
That takes care of the warning messages. What about killing the player? We do so by ending the game. We can end the game in victory, end the game in death, or end the game and say a particular message. In this case we want to end the game in death at 9:27 AM. Add the following paragraph to the end of your source:
At 9:27 AM:
say "You stumble to your knees and then
collapse on the floor, quite surprised to find that the phrase 'drop
dead of exhaustion' can be literally true.";
end the game in death.
If we now play the game and wait 27 turns, we will fall over dead and the game will end.
There's one more thing we need to do: let the player win if they get in bed. We'll use an Instead rule to make the game end in victory when the player gets in bed.
Instead of entering the bed:
say "You climb into the bed, exhaustion overtaking you. You are asleep
before you can order Robutler to undress you.";
end the game in victory.
There we go: our very first puzzle, complete with a way to lose the game and a way to win it.
Where Next?
Resources
The Inform 7 website has example I7 games and more, while Baf's Guide to the IF Archive lists the best recent works of interactive fiction for you to play. For more advanced advice about writing a good game, turn to Dan Shiovitz's guide.
At this point we have a game, albeit a small and not very interesting one. The player can't go many places, there's only one puzzle, and that puzzle is a sleep puzzle. It would be nice if Robutler would take things from you for safekeeping and give them back to you if you wanted. The game doesn't understand the command >LIE ON THE BED. And at some point the ship should complete its hyperspace jump and let players leave the Stateroom.
While this short tutorial can't cover all of that, the manual that comes with I7 can and does. Now that you understand some of the basic concepts, read through the manual. Not only does it explain all of I7 in a straightforward manner, it's chock full of examples that you can steal. For example, in the documentation, open the "Alphabetical Index of Examples" and look for the example called "Lies". If you click on it, you'll see that the example shows not only how to handle >LIE DOWN ON THE BED but also >LIE DOWN NEAR THE BED and more. Once you've read through the manual and know more about I7's advanced features, you'll be ready to add to The Grand Tour or to write an entirely new game.