Smarter Decisions
What you'll learn:
- Protect player movement with compound `IF` guards (AND/OR, bounds, hazards).
- Apply collision checks against grid values before updating state.
- Trigger different outcomes (damage, pickups, exits) from a single decision block.
Lesson 13 – Smarter Decisions
Your world now holds walls, hazards, and pickups. Time to make the logic match. This lesson upgrades IF
checks with AND
/OR
, clamps movement, and fires different outcomes depending on what the player touches.
[📷 suggested: screenshot of a grid with walls, hazards, and the player bouncing off obstacles]
The One-Minute Tour
- Combine conditions with
AND
/OR
to keep code readable. - Clamp coordinates so players never leave the map.
- Peek at the destination tile before committing to a move.
- Trigger different responses (lose life, win) based on map values.
Guarding Movement with Compound Checks
NEW
10 PRINT CHR$(147)
20 DIM MAP(9,9)
30 FOR R=0 TO 9:FOR C=0 TO 9:MAP(R,C)=0:NEXT C:NEXT R
40 FOR I=0 TO 9:MAP(0,I)=1:MAP(9,I)=1:MAP(I,0)=1:MAP(I,9)=1:NEXT I
50 MAP(4,4)=3 : REM hazard
60 MAP(6,7)=4 : REM exit
70 PROWX=5:PROWY=5
80 SCORE=0:LIVES=3
90 GOSUB 500
100 GOSUB 400
110 GOSUB 600
120 GOTO 110
400 REM --- DRAW MAP ---
410 FOR R=0 TO 9
420 FOR C=0 TO 9
430 LOC=1024+R*40+C
440 CELL=MAP(R,C)
450 IF CELL=0 THEN POKE LOC,32
460 IF CELL=1 THEN POKE LOC,160
470 IF CELL=3 THEN POKE LOC,120
480 IF CELL=4 THEN POKE LOC,42
490 NEXT C
500 NEXT R
510 RETURN
500 REM --- DRAW PLAYER & HUD ---
510 PRINT CHR$(19)
520 PRINT "SCORE:";SCORE;" LIVES:";LIVES;" ";
530 POKE 1024+PROWY*40+PROWX,64
540 RETURN
600 REM --- INPUT & LOGIC ---
610 GET K$ : IF K$="" THEN RETURN
620 NX=PROWX : NY=PROWY
630 IF K$="W" OR K$=CHR$(145) THEN NY=PROWY-1
640 IF K$="S" OR K$=CHR$(17) THEN NY=PROWY+1
650 IF K$="A" OR K$=CHR$(157) THEN NX=PROWX-1
660 IF K$="D" OR K$=CHR$(29) THEN NX=PROWX+1
670 IF NX<1 OR NX>8 OR NY<1 OR NY>8 THEN RETURN
680 CELL=MAP(NY,NX)
690 IF CELL=1 THEN RETURN
700 POKE 1024+PROWY*40+PROWX,32
710 PROWX=NX : PROWY=NY
720 POKE 1024+PROWY*40+PROWX,64
730 IF CELL=3 THEN GOSUB 7000
740 IF CELL=4 THEN GOSUB 7100
750 RETURN
7000 REM --- HAZARD ---
7010 LIVES=LIVES-1
7020 PRINT : PRINT "OUCH!" : FOR D=1 TO 200:NEXT D
7030 IF LIVES<=0 THEN GOSUB 7200
7040 RETURN
7100 REM --- GOAL ---
7110 SCORE=SCORE+100
7120 PRINT : PRINT "MAZE CLEARED!" : FOR D=1 TO 300:NEXT D
7130 RUN
7200 REM --- GAME OVER ---
7210 PRINT CHR$(147)
7220 PRINT "GAME OVER" : PRINT "FINAL SCORE:";SCORE
7230 PRINT "PRESS Y TO RESTART" : GET R$:IF R$="" THEN 7230 ELSE IF R$="Y" THEN RUN
7240 END
[🎥 suggested: clip showing the player blocked by walls, losing lives on hazards, winning on the goal]
Code Tour
- Lines 40–60: set walls as
1
, hazards3
, exit4
. - Lines 620–660: arrow keys or WASD set candidate coordinates.
- Line 670: bounds check with
OR
; rejects moves outside the inner playfield. - Line 690:
PEEK
the map value; walls (1
) cancel movement. - Lines 730–740: branch into hazard or goal handlers based on the tile value.
Experiment Section
- Add pickups: assign
MAP(R,C)=5
, increment score whenCELL=5
, and set the tile back to floor. - Partial damage: hazards subtract one life only once by marking the tile to another value after contact.
- Multi-exit levels: add a second exit tile and randomize the target.
- Combo check: if
LIVES>0 AND SCORE>=200
grant a shield that ignores the next hazard. - Trigger sounds (
GOSUB 8000
) on collisions using the SID subroutines from Lesson 7.
[📷 suggested: screenshot highlighting HUD updates and hazard feedback]
Concept Expansion
Complex decisions often chain: check bounds, then tile type, then state (lives, keys, timers). Separating the checks keeps them readable and matches the structure of larger engines, where physics, collisions, and events feed off each other.
Game Integration
- Doors and keys: require both
CELL=DOOR
andHAS_KEY=1
before opening. - Traps: spring when
STEP_COUNT MOD 3=0 OR CELL=TRAP
to create rhythm-based hazards. - Boss triggers: fire when ground tile is a switch and score crosses a threshold.
- Cut-scenes:
IF LEVEL=3 AND (CELL=4 OR SCORE>=500) THEN GOSUB 9000
for story beats.
From the Vault
- Paradroid — study deck layouts and how collisions dictate movement.
- Commodore 64 — refresh how screen memory aligns with map coordinates.
Quick Reference
IF X<MIN OR X>MAX THEN RETURN : REM guard prowling off-screen
CELL=MAP(NY,NX) : REM peek destination tile
IF CELL=1 THEN RETURN : REM block walls
IF CELL=3 THEN LIVES=LIVES-1 : REM hazard effect
IF (SCORE>=200 AND LIVES>0) OR KEY=1 THEN ...
- Evaluate guards in order: edges → walls → special tiles.
- Use parentheses with
AND
/OR
to keep intent obvious. - Store tile types as constants (1=wall, 3=hazard, etc.) for clarity.
What You’ve Learnt
- Combined
AND
/OR
logic to clamp and validate moves. - Checked grid contents before updating state, enabling real collision responses.
- Routed outcomes (damage, win state) through dedicated handlers.
- Set up the decision backbone your maze mini-game will rely on next.
Next lesson: Building Worlds — generate larger maps with nested loops and patterns.