Maya Grab [Day 11] – Main game loop

Yesterday I’ve made a one day break before I move on to the next thing what I had in mind. It’s the main game loop. It’s very simple and it controls all states of the game. I’ve commented it very well because it consists mostly of calling subroutines. To test if everything is working correctly I’ve had to make also a dummy interpreter and room drawing routine. Let’s see the code:

4015 DIM PI$(IS): DIM RF(RS)
4020 PR=0: PT=0: PC=0: PP=0

Last time I’ve said, that the game can be restarted by jumping to line 4000; well, it caused an error, because arrays can’t be defined more than once in a program. The solution is a little bit dirty, I’ve swapped these two lines and I’m restarting the game now by jumping to line 4020.

18500 :
18505 REM – WAIT ———————- 
18510 :
18515 FOR I=0 TO 2000: NEXT
18520 RETURN

Simple routine which causes a short delay; will be used only when the title screen is shown.

19000 :
19005 REM – WAIT FOR KEY ————–
19010 :
19015 GET X$: IF X$=“” THEN 19015
19020 RETURN

Another simple routine which waits for a keypress; will be used only when the RIP screen is shown.

30000 :
30005 REM *****************************
30010 REM * ROOM DRAWING              *
30015 REM *****************************
30020 :
30025 PRINT CHR$(19);
30030 PRINT “[CRD][CRL]”;
30035 PRINT “ROOM:”;PR;” FLAGS:”;RF(PR)
30040 RETURN
30500 :
30505 REM – ROOM 00 ——————-
30510 :
30515 PRINT CHR$(19);
30520 PRINT “[CRD][CRL]”;
30525 PRINT “TITLE”
30530 RETURN
31000 :
31005 REM – ROOM 01 ——————-
31010 :
31015 PRINT CHR$(19);
31020 PRINT “[CRD][CRL]”;
31025 PRINT “RIP”
31030 RETURN

A dummy room drawing routine which draws for the title and rip game states appropriate strings, and for all other rooms in the game it’s number and flags. That’s all the info I need for now.

20000 :
20005 REM *****************************
20010 REM * INTERPRETER               *
20015 REM *****************************
20020 :
20025 REM * MODIFIES:
20030 REM * PR  – PLAYER ROOM
20035 REM * PC  – PLAYER COMMANDS
20040 REM * PP  – PLAYER POINTS
20045 REM * PI$ – PLAYER INVENTORY
20050 REM * RF  – ROOM FLAGS
20055 :
20060 REM * PARAMETERS:
20065 REM * A$ – USER COMMAND
20070 :
20075 REM * RETURNS:
20080 REM * A$ – MESSAGE TO SHOW
20085 REM * A  – INTERPRETER RESPONSE:
20090 REM *    – 1) REDRAW ROOM
20095 REM *    – 2) CHANGE ROOM
20100 REM *    – 3) PLAYER DIED
20105 REM *    – 4) SHOW MESSAGE
20110 REM *    – 5) SHOW INVENTORY
20115 :
20120 PR=PR+1: A$=IM$(4): A=2
20125 RETURN

A dummy interpreter which simply moves the player to the next room every time the player types something. This routine will be the only one which can modify the game parameters. It receives the command in A$, and returns a message in A$ and in A what should the game loop do or redraw next.

5000 :
5005 REM ******************************
5010 REM * MAIN GAME LOOP             *
5015 REM ******************************
5020 :
5025 IF PR=0 THEN 5100: REM TITLE
5030 IF PR=1 THEN 5200: REM RIP
5035 GOTO 5300        : REM ROOMS
5100 :
5105 REM – TITLE SCREEN —————
5110 :
5115 GOSUB 10500: REM INIT SCREEN
5120 GOSUB 30500: REM DRAW TITLE
5125 GOSUB 18500: REM WAIT
5130 PR=2       : REM SET ROOM TO 2
5135 GOSUB 10500: REM INIT SCREEN
5140 GOSUB 11500: REM DRAW FRAME
5145 GOTO 5300  : REM ROOMS
5200 :
5205 REM – RIP SCREEN —————–
5210 :
5215 GOSUB 31000: REM DRAW RIP
5220 GOSUB 12500: REM DRAW ITEMS
5225 GOSUB 13500: REM DRAW DIRECTIONS
5230 GOSUB 14000: REM CLEAR INPUT
5235 GOSUB 16000: REM DRAW RIP MESSAGE
5240 GOSUB 15000: REM CLEAR MESSAGE
5245 GOSUB 16500: REM DRAW STATS
5250 GOSUB 19000: REM WAIT FOR KEY
5255 GOTO 4020  : REM RESTART GAME
5300 :
5305 REM – ROOMS ———————-
5310 :
5315 GOSUB 30000: REM DRAW ROOM
5320 GOSUB 12500: REM DRAW ITEMS
5325 GOSUB 13500: REM DRAW DIRECTIONS
5330 GOSUB 15000: REM CLEAR MESSAGE
5335 GOSUB 15500: REM DRAW MESSAGE
5340 GOSUB 14000: REM CLEAR INPUT
5345 GOSUB 14500: REM WAIT FOR INPUT
5350 GOSUB 20000: REM INTERPRETER
5355 ON A GOTO 5415,5420,5425,5430,5435
5400 :
5405 REM – PARSE INTERPRETER RESPONSE –
5410 :
5415 GOSUB 30000: GOTO 5330: REM ROOM 1
5420 GOTO 5300             : REM ALL  2
5425 GOTO 5200             : REM ALL  3
5430 GOTO 5330             : REM MSG  4
5435 GOSUB 17000: GOTO 5330: REM INV  5

Nothing much to say about this one; everything is in the comments. There are only three states in the game: title, rip and rooms. The 5025-5035 lines are also too much, the loop can handle everything, but I’ve left it for security reasons if I call the line 5000 again by accident. All three states are handled separately (event if rip and rooms states have many things in common), and after the interpreter finishes interpreting the command, the results are handled. What’s missing here is one more result when the player completes the game. It will be implemented later.

Program size: 14226 bytes, on disk 14478 bytes or 57 blocks
Free BASIC memory: 24685 bytes
Used BASIC commands: ON

Source code

* * * * *