Writing an Adventure Game 04

We shall look at a simple mechanic to make the player lose health points after each move due to hunger.  I want the player to only lose health if they have made a valid move - ie the game should make no penalty for accidentally miss-typing a command.

The previous post.

In our PROC_executeCommand() function I've added a new local variable - ok%.  This starts each loop with a value of TRUE, but is set to false whenever the user's command is not valid.

You should only lose health if you have typed a valid command.

The player loses health if the value of ok% is TRUE at the end of the procedure.

Here I've added an 'otherwise' clause to the main case statement which catches the case that the player's input makes no sense at all.  Also, notice that the player's health is decreased by one if the player has made an OK input.
Testing the program.  Notice that the health goes down whenever the player makes a valid move.
Next time:  creating objects the player can interact with.

Full source code follows:

     REM an adventure game in BB4W
     REM by Mr Street

     REM version setting up the world
     REM version moving around the world
     REM version fixing it so you can't walk out of the grid
     REM  - alternative version -
     REM version making the player lose health each turn due to hunger

     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
     REM now some data about our player
     REM player's starting position
x% = 2
     y% = 2
     REM player's health
health% = 50


     LOCAL command$ : REM user's input
     REM the main program
     REM repeat until the player is dead
REM show the player's current position
       REM get some input from the player
INPUT "What now? > " command$
       REM deal with user's input
PROC_executeCommand( command$ )
     UNTIL health% <= 0

DEFPROC_executeCommand( command$ )
     REM deals with the users input
LOCAL com$, gridError$
     LOCAL ok% : REM variable holds TRUE/FALSE whether the player's input was acceptable
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
ok% = TRUE REM assume user's input makes sense
com$ = "n" OR com$ = "north"
         IF y%<SIZE% THEN
y% += 1
           ok% = FALSE

com$ = "s" OR com$ = "south"
         IF y% >1 THEN
y% -= 1
           ok% = FALSE

com$ = "e" OR com$ = "east"
         IF x%<SIZE% THEN
x% += 1
           ok% = FALSE

com$ = "w" OR com$ = "west"
         IF x%>1 THEN
x% -= 1
           ok% = FALSE

"I don't understand you!"
         ok% = FALSE


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


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

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

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