This location handles what happens inside the cave. You can find a sword by rummaging through the junk in the cave enough times. There is also a locked door, which you can unlock if you have the key, open if it's closed and unlocked, close if it is opened, and lock if it is closed and unlocked. It's not as complicated as it may sound. . There is a stack of junk in one corner of this dank cavern room and c door = 2 , an open C , a closed , door in the other. Behind you a stream of daylight enters from the cavern opening. This particular location has an area description that only ever changes when the door is open, at which point we say "an open door" instead of "a closed door." So we plug the first part of the area description in, then use the conditional to determine whether we should say "an open" or "a closed," and then tack on the rest of the description. c door = 2 ^ g Go through the door. p Treasure! Yay, yay! You found all the treasure in the world! Yes! Now you can go home, retire, and start that junkyard business you've always dreamed of owning! w * c Close the door. p Ok. s door = 1 To handle the door, we use a state variable called "door" and designate that it should be 0 if it is closed and locked, 1 if it is closed and unlocked, and 2 if it is open. Locking, unlocking, opening, and closing the door, therefore, become rather simple. Above, we check to see if the door is open, and, if so, provide options to go through the door and to close the door. If we go through the door, we win. If we close the door, we set "door" back to 1. C * o Open the door. c !door p It's locked! C p Ok. s door = 2 c a:key c !door * un Unlock the door. p Ok. The door is now unlocked. s door = 1 C * lo Lock the door. p Ok. The door is now locked. s door = 0 On the other hand, if the door is closed (locked or unlocked), we provide an option to open the door. If we open the door when it's locked, we fail; if we open the door when it's unlocked, we set "door" to 2, to indicate that it is now open. If we have the key, we also want to provide options to lock or unlock the door. ^ l Leave the cavern. c d:sword p After bribing that kobold to let you in? You should at least do something to make this trip worth your while. C g kobold Regardless of the state of the door, we want the player to be able to leave the way he came in. However, note a potential situation where the player could get himself into a situation where it's impossible to win, except that we have coded for that here. If we just let the player leave no matter what, consider the following scenario: the player finds the coin, bribes the kobold, enters the cave, then leaves without finding the sword. In that situation, the player could never enter the cave again, because he wouldn't be able to find another coin, nor would he have the sword to scare the kobold off with. Without being able to enter the cave again, there would be no way to win the game. It's important to think about your game design and figure out if there are any situations like this one. It's considered bad game design to let the player reach a situation, however unlikely, where it's impossible to win. To solve the problem here, we check to see if the player has the sword when he tries to leave the cave. If not, we prevent him from leaving. This is a crude way of solving the problem -- it's more elegant to let the player do what he wants to do -- but it works. * rum Rummage through the junk. s rummaged + 1 c rummaged = 1 p You search through the junk but find nothing of interest. Maybe if you looked harder.... C rummaged = 2 p You found a sword hidden at the bottom of the pile! a sword C p You search through the junk but find nothing more of interest. The above section of code lets the player rummage through the junk in the cave and find the sword hidden within it. We keep a state variable called "rummaged" which remembers how many times the player has rummaged through the junk so far. The first time, the player gets a message saying he didn't find anything and a hint that suggests he should rummage some more. The second time, he finds the sword. Any further rummaging he does is unsuccessful. Finally, we round out the menu choices with something frivolous: * hit Hit your head against the cave wall really hard. p Ouch! Well, that's the end of that. You hit your head so hard, you died right there on the spot. l |
|