Text-Based Game Demo

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
TerryL
Posts: 78
Joined: Sat Nov 23, 2013 8:57 pm

Text-Based Game Demo

Post by TerryL » Mon Dec 30, 2013 10:10 pm

I couldn't find examples of a text-based game on the forum or revonline, so I thought it would be a fun challenge to try. I came up with two very different methods.

The first method uses ask & answer dialogs in a button on several cards, each card representing a location or "room" along the pathway. The second method uses custom commands in a parser field on one card, similar to the classic game Zork.

1) Any other ideas on how to reproduce one of those old text-based games?
2) The demo includes speech. I'm unsure about proper use of the revUnloadSpeech command. The Dictionary states it is required for a standalone application in a shutdown handler. But if I have two stacks open that both call revLoadSpeech, then close one stack that calls revUnloadSpeech in a closeStack handler, the remaining open stack has no speech engine to use. Calling revLoadSpeech in a resumeStack handler in the stack script doesn't help. if revSpeechVoices() = empty then revLoadSpeech --doesn't work. Once unloaded, speech can't be loaded again until LiveCode is quit and restarted. What am I missing? I'm using Win7, LC6.5.0.

The parser field code and the text game demo v1.1 updated 1/3/2014:

Code: Select all

on returnInField  --parser field code
   local Response
   put last line of me into Response  --load entry
   if Response contains "new game" or Response contains "start over" or Response contains "good-bye" then  --start over
      put "Score: 0" &cr& "Items: " into fld "Display"
      put "> " into line 7 to -1 of me
      select after text of me  --insert cursor
      set the ResponseNumber of me to "1"  --custom property to manage responses
   else if Response contains "vocabulary" or Response contains "words" then
      answer "Game Vocabulary:" &cr& "yes, no, ok, examine, take, use, go, open, close, under, north, south, east, west." with "OK" titled "Info"
   else do ("Parse" & the ResponseNumber of me) && "Response"  --trigger custom commands in correct order
end returnInField

on Parse1 X  --begin, Parse1 is first custom command
   if X contains "yes" or X contains "ok" then
      put cr&cr& "Let's begin a garden adventure with Yellow Cat & Bumblebee!" &cr&\
      "You are Home." &cr& "North is the kitchen, west is the garden." &cr&cr& "> " after me
      select after text of me  --insert cursor
      set the ResponseNumber of me to the ResponseNumber of me + 1  --go Parse2
   else exit to top
end Parse1

on Parse2 X  --n or w
   if X contains "north" or X contains "kitchen" then  --kitchen
      put cr&cr& "Before you can go north to the kitchen you must answer a question." &cr&\
      "What color is the sky on a bright sunny day?" &cr&cr& "> " after me
      select after text of me  --insert cursor
      set the ResponseNumber of me to the ResponseNumber of me + 1  --go Parse3
   else if X contains "west" or X contains "garden" then  --garden
      put cr&cr& "But first you must answer a question before going west to the garden." &cr&\
      "What color is the sky on a bright sunny day?" &cr&cr& "> " after me
      select after text of me  --insert cursor
      set the ResponseNumber of me to the ResponseNumber of me + 3  --go Parse5
   else exit to top
end Parse2

--on Parse3, Parse4, Parse5..., Parse90  --too many custom commands?
Attachments
TextGameDemo.zip
(5.84 KiB) Downloaded 197 times
Last edited by TerryL on Fri Jan 03, 2014 10:42 pm, edited 2 times in total.
Beginner Lab (LiveCode tutorial) and StarterKit (my public stacks)
https://tlittle72.neocities.org/openxtalk.html

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Text-Based Game Demo

Post by Simon » Tue Dec 31, 2013 1:33 am

Hi Terry,
I've got a crazy idea...
What happens if you actually draw the floor plan and move an object through based on the answers?
Using the intersect function you could get rid of much of the coding.
Of course the floorplan would be hidden.

Each room could have a "dot on the floor" and using the intersect again with an array, each dot could be mapped to the revSpeak instructions.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Text-Based Game Demo

Post by jacque » Tue Dec 31, 2013 7:59 pm

It sounds like you've settled on using a field for input instead of ask/answer dialogs. That's good because dialogs are intrusive and distracting.

The IDE manages all its libraries when it starts up and closes them when it shuts down. You don't need to do that during development, revSpeech will be available to your stacks automatically. Only a standalone needs to handle loading and unloading the standard libraries. That's why you shouldn't use a closeStack handler to close the library; the library will unload on every change. The dictionary is correct that loading should occur during startup and unloading should only occur on a shutdown.

You won't get a startup message in the IDE because it has been handled already long before your stack is opened. But in a standalone you will get one, so you can place the loading code in a startup handler without any problem. During development it will never run, and in a standalone it will. The same goes for shutdown, which will fire in your standalone reliably. I can't recall offhand if the IDE passes a shutdown message to your stack, but if it does, it does no harm to unload the same library twice; the second unload is ignored.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply