Skip to content
Game 14 Unit 6 of 6 1 hr learning time

The Caverns

Dress the finished game — a title, coloured clues, sound for atmosphere, and a play-again loop — and hide the danger for good.

100% of The Caverns

The game works; now it earns its atmosphere. The last unit removes the build-time readouts, colours each clue, gives the cave a voice in sound, and wraps everything in a title and a replay. The dark is finally dark.

  10 BORDER 0: PAPER 1: INK 7: CLS
  20 LET a$ = "*** THE CAVERNS ***": LET y = 5: GO SUB 9000
  30 PRINT AT 8, 4; "Explore 20 dark rooms."
  40 PRINT AT 9, 4; "Collect 3 treasures to win."
  50 PRINT AT 11, 4; "Beware the pit and the"
  60 PRINT AT 12, 4; "creature that stalks you."
  70 PRINT AT 14, 4; "Listen for clues..."
  80 PRINT AT 18, 4; "Press any key to enter"
  90 PAUSE 0
 100 RANDOMIZE
 110 DIM n(20): DIM s(20): DIM e(20): DIM w(20)
 120 RESTORE 1010
 130 FOR i = 1 TO 20
 140 READ n(i), s(i), e(i), w(i)
 150 NEXT i
 160 LET rm = 1: LET found = 0: LET steps = 0
 170 LET pit = INT (RND * 19) + 2
 180 LET cr = INT (RND * 19) + 2
 190 IF cr = pit THEN GO TO 180
 200 DIM t(3)
 210 LET t(1) = INT (RND * 19) + 2
 220 IF t(1) = pit OR t(1) = cr THEN GO TO 210
 230 LET t(2) = INT (RND * 19) + 2
 240 IF t(2) = pit OR t(2) = cr OR t(2) = t(1) THEN GO TO 230
 250 LET t(3) = INT (RND * 19) + 2
 260 IF t(3) = pit OR t(3) = cr OR t(3) = t(1) OR t(3) = t(2) THEN GO TO 250
 270 CLS
 280 LET a$ = "*** THE CAVERNS ***": LET y = 0: GO SUB 9000
 290 PRINT AT 1, 4; "Treasures: "; found; "/3"
 300 PRINT AT 3, 2; "You are in room "; rm; "."
 310 PRINT AT 5, 2; "Exits: ";
 320 IF n(rm) > 0 THEN PRINT "N ";
 330 IF s(rm) > 0 THEN PRINT "S ";
 340 IF e(rm) > 0 THEN PRINT "E ";
 350 IF w(rm) > 0 THEN PRINT "W ";
 370 LET clue = 0
 380 LET chk = pit: GO SUB 740
 390 IF adj = 1 THEN PRINT AT 7, 2; INK 6; "You feel a cold draft...": LET clue = 1: BEEP 0.3, -10
 400 LET chk = cr: GO SUB 740
 410 IF adj = 1 THEN PRINT AT 8, 2; INK 2; "Something smells terrible...": LET clue = 1: BEEP 0.2, 20
 420 LET gl = 0
 430 FOR i = 1 TO 3
 440 IF t(i) > 0 THEN LET chk = t(i): GO SUB 740: IF adj = 1 THEN LET gl = 1
 450 NEXT i
 460 IF gl = 1 THEN PRINT AT 9, 2; INK 4; "A faint glint nearby...": LET clue = 1: BEEP 0.1, 15
 470 IF clue = 0 THEN PRINT AT 7, 2; "All quiet."
 480 INPUT "Direction (N/S/E/W): "; d$
 490 LET dest = 0
 500 IF d$ = "N" OR d$ = "n" THEN LET dest = n(rm)
 510 IF d$ = "S" OR d$ = "s" THEN LET dest = s(rm)
 520 IF d$ = "E" OR d$ = "e" THEN LET dest = e(rm)
 530 IF d$ = "W" OR d$ = "w" THEN LET dest = w(rm)
 540 IF dest = 0 THEN PRINT AT 12, 2; "You can't go that way!": BEEP 0.1, -5: PAUSE 30: GO TO 270
 550 LET rm = dest
 560 LET steps = steps + 1
 570 IF rm = pit THEN GO TO 800
 580 IF rm = cr THEN GO TO 840
 590 FOR i = 1 TO 3
 600 IF rm = t(i) THEN LET t(i) = 0: LET found = found + 1: PRINT AT 12, 2; INK 4; "You found treasure!": BEEP 0.1, 10: BEEP 0.1, 15: PAUSE 30
 610 NEXT i
 620 IF found = 3 THEN GO TO 880
 630 REM --- Creature moves ---
 640 LET nm = 0
 650 LET d = INT (RND * 4) + 1
 660 IF d = 1 AND n(cr) > 0 THEN LET nm = n(cr)
 670 IF d = 2 AND s(cr) > 0 THEN LET nm = s(cr)
 680 IF d = 3 AND e(cr) > 0 THEN LET nm = e(cr)
 690 IF d = 4 AND w(cr) > 0 THEN LET nm = w(cr)
 700 IF nm = 0 THEN GO TO 650
 710 LET cr = nm
 720 IF cr = rm THEN GO TO 840
 730 GO TO 270
 740 REM --- Is chk adjacent? ---
 750 LET adj = 0
 760 IF n(rm) = chk OR s(rm) = chk OR e(rm) = chk OR w(rm) = chk THEN LET adj = 1
 770 RETURN
 800 CLS: LET a$ = "*** THE CAVERNS ***": LET y = 6: GO SUB 9000
 810 PRINT AT 9, 4; INK 2; "You fell into the pit!"
 820 BEEP 0.5, -15
 830 GO TO 920
 840 CLS: LET a$ = "*** THE CAVERNS ***": LET y = 6: GO SUB 9000
 850 PRINT AT 9, 4; INK 2; "The creature got you!"
 860 BEEP 0.3, -5: BEEP 0.3, -10
 870 GO TO 920
 880 CLS: LET a$ = "*** THE CAVERNS ***": LET y = 6: GO SUB 9000
 890 PRINT AT 9, 4; INK 4; "All treasures found!"
 900 PRINT AT 11, 4; "You escaped the caverns!"
 910 BEEP 0.1, 10: BEEP 0.1, 15: BEEP 0.1, 20
 920 PRINT AT 13, 4; "Steps taken: "; steps
 930 PRINT AT 18, 4; "Press any key to play again"
 940 PAUSE 0
 950 GO TO 10
1000 REM --- Room map: N,S,E,W ---
1010 DATA 0,5,2,0
1020 DATA 0,0,3,1
1030 DATA 0,7,4,2
1040 DATA 0,8,0,3
1050 DATA 1,0,6,0
1060 DATA 0,10,7,5
1070 DATA 3,11,0,6
1080 DATA 4,12,0,0
1090 DATA 0,13,10,0
1100 DATA 6,0,11,9
1110 DATA 7,15,12,10
1120 DATA 8,0,0,11
1130 DATA 9,17,14,0
1140 DATA 0,18,0,13
1150 DATA 11,19,16,0
1160 DATA 0,20,0,15
1170 DATA 13,0,18,0
1180 DATA 14,0,19,17
1190 DATA 15,0,20,18
1200 DATA 16,0,0,19

9000 PRINT AT y, (32 - LEN a$) / 2; BRIGHT 1; a$
9010 RETURN
ZX Spectrum The Caverns title screen: the rules in white on dark blue with 'Press any key to enter'
The finished The Caverns: a title and rules, then a cave with nothing visible but your own position.

Colour and sound as atmosphere

The clues now carry colour and a tone. The draft prints in cyan with a low rumble (line 390), the smell in red with a higher note (line 410), the glint in green with a bright chime (line 460). INK and BEEP were decoration in earlier games; here they are the atmosphere — in a game with no pictures, the colour of a word and the pitch of a beep are how the cave feels dangerous or rich. Each sense gets its own colour and sound, so an experienced player reads the room without even reading the text.

Hiding the danger, and playing again

The debug readout that showed the pit, creature and treasure rooms is gone — the only information left is what you can sense, which is the whole point. The title screen (lines 20–90) sets the scene before you enter, and after any ending, line 950 — GO TO 10 — places fresh hazards and treasures and starts over, so no two games are the same map of danger.

What you built

The Caverns is your first world that is bigger than the screen. You held the cave as a graph in four parallel arrays loaded from DATA, walked it by following links, and built every danger out of one reusable adjacency check. Above all you worked with hidden state: the pit, the creature and the treasures are never drawn, only sensed through the clues they leave next door — a draft, a smell, a glint. Reasoning about a place you cannot see, from indirect evidence, is the idea this game teaches, and it is how every fog-of-war and stealth game since has worked.

Next: Yearfall — where the state you manage is not a place but a stretch of time, and today's choices fall due seasons later.