Writing an Adventure Game 09

This shall be the last post in our series on writing an adventure game in BB4W.   Posts are getting rather long, and it really is over to you to complete the game now.  If you want an example of a more complicated adventure game engine, then do check out GOLIATH.

In this edit, we have added a procedure for allowing the apple in the woods to constantly grow back once it has been eaten (ultimately preventing the player from starving to death forever).  I've also added a cake object as a special gift for all my readers - enjoy!

Source code follows:

     REM an adventure game in BB4W
     REM by Mr Street

     REM version 1.0.0.1 setting up the world
     REM version 1.0.0.2 moving around the world
     REM version 1.0.0.3 fixing it so you can't walk out of the grid
     REM version 1.0.0.4 health decreases each turn (because you get hungry)
     REM version 1.0.0.5 set-up some objects for the player to interact with
     REM version 1.0.0.6 allowing objects to be picked up
     REM version 1.0.0.7 allowing obejcts to be dropped
     REM version 1.0.0.8 eating the apple
     REM version 1.0.0.9 the apple always grows in the woods.

     REM let's set up a 'world'
     
SIZE% = 3
     DIM World$( SIZE%, SIZE% ) : REM creates a 3x3 grid
     REM now 'populate' the grid with some space names
     
PROC_populateWorld
     REM now some data about our player
     REM player's starting position
     
x% = 2
     y% = 2
     REM player's health
     
health% = 50

     REM the objects
     
numObjects% = 3 : REM start with just a small number of objects
     
DIM prefix$( numObjects% )   : REM eg "a" or "an" or "the"
     
DIM object$( numObjects% )   : REM object name, eg "sword"
     
DIM objectx%( numObjects% )  : REM object x position
     
DIM objecty%( numObjects% )  : REM object y position
     
DIM edible%( numObjects% )   : REM set to -1 for an edible object, otherwise is 0

     
PROC_createObjects
     REM a list of objects that the player has
     
DIM playerObject%( numObjects% )
     REM the apple always grows in the woods
     
appleDelay% = 10
     PROC_main
     STOP


     
DEFPROC_main
     LOCAL command$ : REM user's input
     REM the main program
     REM repeat until the player is dead
     
REPEAT
       
REM show the player's current position
       
PROC_showCurrent
       REM show objects here
       
PROC_showObjects
       REM get some input from the player
       
PRINT REM a blank line
       
INPUT "What now? > " command$
       PRINT REM a blank line
       REM deal with user's input
       
PROC_executeCommand( command$ )
       REM the apple always grows in the woods
       
PROC_growApple
     UNTIL health% <= 0
     REM game over message
     
PRINT "GAME OVER!"
     ENDPROC

     
DEFPROC_executeCommand( command$ )
     REM deals with the users input
     
LOCAL com$, gridError$
     LOCAL ok% : REM checks whether the user's input is acceptable
     
ok% = TRUE REM assume user's input makes sense
     
gridError$ = "You can't go in that direction!" : REM a message for when you move off the grid
     
com$ = FN_convlc( command$ ) : REM convert to lowercase so it is easier

     
CASE TRUE OF
       WHEN 
com$ = "n" OR com$ = "north"
         IF y%<SIZE% THEN
           
y% += 1
         ELSE
           PRINT 
gridError$
           ok% = FALSE
         ENDIF

       WHEN 
com$ = "s" OR com$ = "south"
         IF y% >1 THEN
           
y% -= 1
         ELSE
           PRINT 
gridError$
           ok% = FALSE
         ENDIF

       WHEN 
com$ = "e" OR com$ = "east"
         IF x%<SIZE% THEN
           
x% += 1
         ELSE
           PRINT 
gridError$
           ok% = FALSE
         ENDIF

       WHEN 
com$ = "w" OR com$ = "west"
         IF x%>1 THEN
           
x% -= 1
         ELSE
           PRINT 
gridError$
           ok% = FALSE
         ENDIF

       WHEN LEFT$(
com$,3) = "get"
         thisObject$ = RIGHT$(com$, LEN(com$)-4)
         PROC_getObject( thisObject$ )

       WHEN LEFT$(com$,4) = "drop"
         thisObject$ = RIGHT$(com$, LEN(com$)-5)
         PROC_dropObject( thisObject$ )

       WHEN LEFT$(com$,3) = "eat"
         thisObject$ = RIGHT$(com$, LEN(com$)-4)
         PROC_eatObject( thisObject$ )


       OTHERWISE REM this MUST be the last statement in the case
         
PRINT "I don't understand you!"
         ok% = FALSE


     ENDCASE

     
REM if the user's input was acceptable, then they have
     REM made one 'turn' and should lose one health point
     
IF ok% THEN
       
health% -= 1
     ENDIF

     ENDPROC


     
DEFPROC_showCurrent
     PRINT "----------------------------------"
     PRINT "You are at the : "World$(x%, y%)
     PRINT "Your health is : "STR$(health%)
     ENDPROC


     
DEFPROC_showObjects
     REM look for any objects here
     
LOCAL n%
     REM look through list of objects
     
FOR n% = 1 TO numObjects%
       REM is the player's location the same as the object location?
       
IF x% = objectx%(n%) AND y% = objecty%(n%) THEN
         
REM objects is here
         
PRINT "There is "prefix$(n%)" "object$(n%)" here."
       ENDIF
     NEXT
     ENDPROC


     
DEFPROC_getObject( this$ )
     REM allows player to pick up objects
     
LOCAL n%, found%, index%
     LOCAL objectError$ : objectError$ = "I don't see one of those here!"
     WHILE n% <=numObjects% AND NOT found%
       IF this$ = object$(n%) THEN
         
found% = TRUE
         
index% = n%
       ELSE
         
n% += 1
       ENDIF
     ENDWHILE
     
:
     IF found% THEN
       
REM check that the object is at the player's location
       
IF x% = objectx%(index%) AND y% = objecty%(index%) THEN
         
REM object is here, so pick it up
         
playerObject%(index%) = TRUE REM add to list of player's objects
         
objectx%(index%) = -1 : REM remove object from the grid
         
objecty%(index%) = -1 : REM remove object from the grid
         
PRINT "OK, you take "prefix$(index%)" "object$(index%)
       ELSE
         PRINT 
objectError$
       ENDIF
     ELSE
       PRINT 
objectError$
     ENDIF
     ENDPROC

     
DEFPROC_dropObject( this$ )
     REM allows player to drop objects
     
LOCAL n%, found%, index%
     LOCAL objectError$ : objectError$ = "You are not carrying one of those!"
     WHILE n% <=numObjects% AND NOT found%
       IF this$ = object$(n%) THEN
         
found% = TRUE
         
index% = n%
       ELSE
         
n% += 1
       ENDIF
     ENDWHILE
     
:
     IF found% THEN
       
REM check that the player has the object
       
IF playerObject%(index%) THEN
         
REM player has one so can drop it
         
playerObject%(index%) = FALSE REM remove from list of player's objects
         
objectx%(index%) = x% : REM add object to the grid
         
objecty%(index%) = y% : REM add object to the grid
         
PRINT "OK, you drop "prefix$(index%)" "object$(index%)
       ELSE
         PRINT 
objectError$
       ENDIF
     ELSE
       PRINT 
objectError$
     ENDIF
     ENDPROC


     
DEFPROC_eatObject( this$ )
     REM allows player to eat objects
     
LOCAL n%, found%, index%
     LOCAL healthBonus% : healthBonus% = 10 : REM health gained from eating things
     
LOCAL objectError$ : objectError$ = "You are not carrying one of those!"
     WHILE n% <=numObjects% AND NOT found%
       IF this$ = object$(n%) THEN
         
found% = TRUE
         
index% = n%
       ELSE
         
n% += 1
       ENDIF
     ENDWHILE
     
:
     IF found% THEN
       
REM check that the player has the object
       
IF playerObject%(index%) THEN
         
REM player has one so can try to eat it
         REM check whether the object is edible
         
IF edible%(index%) THEN
           
REM object is one of the edible objects
           
playerObject%(index%) = FALSE REM remove from list of player's objects
           
objectx%(index%) = -1 : REM remove object from the grid
           
objecty%(index%) = -1 : REM remove object from the grid
           
PRINT "OK, you eat "prefix$(index%)" "object$(index%)
           health% += healthBonus%
           PRINT "You gain "STR$(healthBonus%)" health points!"
         ELSE
           PRINT 
"I'm not eating one of those!"
         ENDIF
       ELSE
         PRINT 
objectError$
       ENDIF
     ELSE
       PRINT 
objectError$
     ENDIF
     ENDPROC


     
DEFPROC_growApple
     REM if the apple does not exist, then grow a new apple
     
IF objectx%(1) = -1 AND objecty%(1) = -1 AND playerObject%(1) = FALSE THEN
       
appleDelay% -= 1
     ENDIF
     IF 
appleDelay% <= 0 THEN
       
REM reset the apple delay
       
appleDelay% = 10
       REM put the apple in the woods
       
objectx%(1) = 1
       objecty%(1) = 3
     ENDIF
     ENDPROC



     
DEF FN_convlc(A$)
     REM converts to lower case
     
SYS "CharLowerBuff", !^A$, LEN(A$)
     = A$


     DEFPROC_populateWorld
     REM puts some names of the spaces into the World
     
LOCAL x%, y%
     REM start with the first row
     
FOR y% = 1 TO 3
       FOR x% = 1 TO 3
         READ World$(x%,y%)
       NEXT
     NEXT
     ENDPROC
     
:
     REM the data for our 3x3 grid
     
DATA "Hills",  "Mountains", "Forest"
     
DATA "Castle", "Village", "Fields"
     
DATA "Woods",  "Swamp",  "Lake"

     
DEFPROC_createObjects
     REM puts the names of objects into a list
     
LOCAL n%
     FOR n% = 1 TO numObjects%
       READ prefix$(n%)
       READ object$(n%)
       READ objectx%(n%)
       READ objecty%(n%)
       READ edible%(n%)
     NEXT
     ENDPROC
     DATA
 "an", "apple", 1, 3, -1
     
DATA "a",  "sword", 3, 3, 0
     
DATA "a",  "cake", 2, 2, -1