Home / Commodore 64 / Phase 0 / Lesson 10
Lesson 10 of 64

Logic & Motion

What you'll learn:

  • Combine grid data with player movement and collision checks.
  • Use compound `IF` conditions to guard edges and hazards.
  • Introduce a simple game loop that reads input and updates the map.
16% Complete

Lesson 10 – Logic & Motion

Yesterday you built a map; today you bring it to life. We’ll wire up input, movement, and collision logic so your hero respects walls, avoids hazards, and marches across the grid you just created.

[📷 suggested: screenshot of a player @ navigating a walled grid]


The One-Minute Tour

  • Store the map and the player coordinates in arrays/variables.
  • Detect intended movement by reading keys with GET.
  • Check the target cell before moving; block or punish as needed.
  • Redraw only the tiles that changed to keep the board responsive.

Wiring Up the Playground

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(3,4)=3 : REM hazard
60 MAP(7,7)=4 : REM goal
70 PROWX=5:PROWY=5
80 GOSUB 500
90 GOSUB 400
100 GOSUB 600
110 GOTO 100

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,35
470 IF CELL=3 THEN POKE LOC,88
480 IF CELL=4 THEN POKE LOC,42
490 NEXT C
500 NEXT R
510 RETURN

500 REM --- DRAW PLAYER ---
510 POKE 1024+PROWY*40+PROWX,64
520 RETURN

600 REM --- INPUT & LOGIC ---
610 GET K$ : IF K$="" THEN RETURN
620 NX=PROWX : NY=PROWY
630 IF K$="W" THEN NY=PROWY-1
640 IF K$="S" THEN NY=PROWY+1
650 IF K$="A" THEN NX=PROWX-1
660 IF K$="D" THEN NX=PROWX+1
670 IF MAP(NY,NX)=1 THEN RETURN
680 IF NX<1 OR NX>8 OR NY<1 OR NY>8 THEN RETURN
690 POKE 1024+PROWY*40+PROWX,32
700 PROWX=NX : PROWY=NY
710 POKE 1024+PROWY*40+PROWX,64
720 IF MAP(NY,NX)=3 THEN GOSUB 7000
730 IF MAP(NY,NX)=4 THEN GOSUB 7100
740 RETURN

7000 REM --- HAZARD ---
7010 PRINT : PRINT "OUCH!" : FOR T=1 TO 200:NEXT T
7020 RETURN

7100 REM --- GOAL ---
7110 PRINT : PRINT "WINNER!" : FOR T=1 TO 200:NEXT T
7120 RUN

[🎥 suggested: clip showing the @ character moving with WASD, refusing to cross walls]


Code Tour

  • Lines 10–70: initialise the 10×10 map with border walls, a hazard (X), and a goal (*). Player starts at centre.
  • Line 80: draws static map tiles.
  • Line 90: draws the player in the current position.
  • Line 100: polls input and executes logic before looping.

Inside the logic routine:

  • Determine candidate coordinates (NX, NY).
  • Check for walls (MAP=1) or map edges before moving.
  • Clear the previous player tile and draw the new one.
  • React to hazards/goals with simple subroutines.

Experiment Section

  • Swap to arrow keys by checking CHR$(157) (left), CHR$(29) (right), CHR$(17) (down), CHR$(145) (up).
  • Multiple hazards: scatter extra MAP(R,C)=3.
  • Lives counter: add LIVES=3 and decrement when hitting hazards; reset only after lives reach zero.
  • Move draws into a subroutine GOSUB 520 to centralise rendering updates.
  • Animate hazards: every loop, toggle their characters to show flicker.

[📷 suggested: grid with multiple hazard tiles and a lives counter]


Concept Expansion

Input + logic + rendering equals the classic game loop: read → simulate → draw. Later you’ll capture the loop inside a GOSUB, then upgrade timers with TI or raster interrupts, but the structure will stay familiar.


Game Integration

  • Patrolling enemies: track enemy coordinates and check collisions against the player.
  • Keys and doors: add a second map for items, then only allow moving onto goal tiles when a key flag is set.
  • Scoring: increment SCORE whenever the player steps onto pickups.
  • Fog of war: keep a VISITED(,) array; only draw cells that have been discovered.

From the Vault

  • Gridlocked — revisit the map lesson if you need a refresher.
  • Paradroid — study how grid logic powers classic C64 decks.

Quick Reference

REM Movement skeleton
GET K$: IF K$="" THEN RETURN
NX=PX: NY=PY
IF K$="A" THEN NX=PX-1
IF MAP(NY,NX)=1 THEN RETURN
IF NX<1 OR NX>MAX THEN RETURN
POKE SCREEN+PY*40+PX,32
PX=NX: PY=NY
POKE SCREEN+PY*40+PX,PLAYER_CHAR
  • Always calculate the candidate tile before applying it.
  • Check both grid contents and edges for safety.
  • Clear and redraw only what changes to keep the loop fast.

What You’ve Learnt

  • How to merge arrays with movement logic for real-time control.
  • Compound IF checks that prevent walking into walls or off the board.
  • Redrawing just the affected tiles instead of the entire map.
  • Built the core game loop that powers many C64 titles.

Next lesson: Text Tricks — slice strings, print dynamic HUDs, and prepare for menus.