Writing an Adventure Game 02

Last time we set up a game grid.  This time we are going to show how to move around the game world.

I've added three new procedures:

PROC_main
PROC_showCurrent
PROC_executeCommand( command$ )

Let's look at each in turn:

PROC_main
This is the main program loop.  In pseudcode:

REPEAT
    SHOW CURRENT POSITION
    GET USER'S INPUT
    DEAL WITH USER'S INPUT
UNTIL PLAYER IS DEAD

PROC_showCurrent
This procedure deals with display the player's current position in the game.  Remember that the player's position is recorded in two variables: x% and y%.

PROC_executeCommand( command$ )
This procedure deals with the player's commands.  If the player types 'north' then the player's y-coordinate should increase by one.  If the player types 'south' then it should decrease.  Similarly, if the player types 'east' and 'west' then the player's x-coordinate should increase or decrease respectively.

Testing the program you should see something like the following:

Typing 'north', 'south', 'east' or 'west' changes the x and y coordinate for the player - effectively moving the player to a new location in the game grid.
The game grid again, in case you have forgotten.

It is perfectly possible for the player to walk off the edge of the game grid.  In which case the program will throw an error message at you.  Try starting at the 'Village' and move 'east' twice.  The first move should take you to the 'fields' space, but the space beyond is not defined.  It will cause an error (for now):
The 'Bad subscript' error means that we are trying to access an area of the computer memory that we have not reserved.  

Next time - how to stop the player walking off the edge of the grid (and causes the error message shown above).


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 let's set up a 'world'
     
DIM World$(3,3) : 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

     PROC_main


     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 get some input from the player
       
INPUT "What now? > " command$
       REM deal with user's input
       
PROC_executeCommand( command$ )
     UNTIL health% <= 0
     ENDPROC


     
DEFPROC_executeCommand( command$ )
     REM deals with the users input
     
com$ = FN_convlc( command$ ) : REM convert to lowercase so it is easier
     
CASE TRUE OF
       WHEN 
com$ = "n" OR com$ = "north" y% += 1
       WHEN com$ = "s" OR com$ = "south" y% -= 1
       WHEN com$ = "e" OR com$ = "east"  x% += 1
       WHEN com$ = "w" OR com$ = "west"  x% -= 1
     ENDCASE
     ENDPROC
     DEF FN_convlc(A$)
     REM converts to lower case
     
SYS "CharLowerBuff", !^A$, LEN(A$)
     = A$

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

    

     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"