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

Memory Games

What you'll learn:

  • Store tile, enemy, and vocabulary tables with `DATA` and `READ`.
  • Reset the read pointer with `RESTORE` to replay levels or scripts.
  • Populate arrays from DATA so maps and text live outside your code.
19% Complete

Lesson 12 – Memory Games

Typing maps by hand is a drag. DATA lets you stash worlds, vocab lists, and enemy positions inside your program and pull them out with READ. Today we’ll load tile maps, vocabulary, and entity tables straight from DATA statements.

[📷 suggested: screenshot of a maze drawn from DATA lines]


The One-Minute Tour

  • DATA stores comma-separated values; READ consumes them sequentially.
  • RESTORE rewinds the pointer so you can re-read the same block.
  • Combine DATA with loops to fill arrays, screen RAM, or string tables quickly.

Filling a Tile Map from DATA

NEW
10 PRINT CHR$(147)
20 DIM MAP(9,9)
30 FOR R=0 TO 9
40 FOR C=0 TO 9
50 READ MAP(R,C)
60 NEXT C
70 NEXT R
80 FOR R=0 TO 9
90 FOR C=0 TO 9
100 LOC=1024+R*40+C
110 IF MAP(R,C)=1 THEN POKE LOC,35 ELSE POKE LOC,32
120 NEXT C
130 NEXT R
140 DATA 1,1,1,1,1,1,1,1,1,1
150 DATA 1,0,0,0,0,0,0,0,0,1
160 DATA 1,0,0,0,0,1,1,0,0,1
170 DATA 1,0,1,1,0,0,0,0,0,1
180 DATA 1,0,0,1,0,1,1,1,0,1
190 DATA 1,0,0,0,0,0,0,1,0,1
200 DATA 1,1,0,0,1,0,0,1,0,1
210 DATA 1,0,0,0,1,0,0,0,0,1
220 DATA 1,0,0,0,0,0,0,0,0,1
230 DATA 1,1,1,1,1,1,1,1,1,1

Walls use 1 (rendered as #); floors are 0 (spaces). Changing the DATA lines redraws the maze instantly.


Resetting the Stream

After reading through a list you can rewind:

NEW
10 DATA 10,20,30
20 READ A:READ B:READ C
30 PRINT A;B;C
40 RESTORE
50 READ A:READ B:READ C
60 PRINT A;B;C

RESTORE returns the pointer to the first DATA line. Without it, the second set of READs would trigger ?OUT OF DATA ERROR.


#endregion

Populating Vocabulary Tables

NEW
10 DIM VB$(3),OB$(3)
20 FOR I=0 TO 2
30 READ VB$(I),OB$(I)
40 NEXT I
50 FOR I=0 TO 2
60 PRINT "VERB:";VB$(I);" OBJECT:";OB$(I)
70 NEXT I
80 DATA GO,NORTH,TAKE,LAMP,LOOK,ROOM

Pairing verbs and objects via DATA keeps your parser extensible—just append another line to add a command.


Loading Enemy Waves from DATA

NEW
10 PRINT CHR$(147)
20 DIM EX(4),EY(4)
30 FOR I=0 TO 4
40 READ EX(I),EY(I)
50 NEXT I
60 FOR I=0 TO 4
70 POKE 1024+EY(I)*40+EX(I),81
80 NEXT I
90 END
1000 DATA 10,5,15,8,18,12,22,6,28,10

Each pair defines an enemy coordinate. Call RESTORE and READ a different dataset to spawn Wave 2.


Experiment Section

  • Move the DATA lines off to a higher range (e.g., starting at 5000) to keep them tidy.
  • Store colour values alongside characters: read four values per tile (char, colour, enemy, item).
  • Build a room index: first READ the width/height, then the tiles, so you can support different map sizes.
  • Use DATA to preload dialogue text and cycle through it with READ on demand.
  • Combine with Lesson 11 by loading verb/object synonyms from DATA instead of hardcoding them.

[🎥 suggested: clip swapping between two DATA-defined maps via RESTORE]


Concept Expansion

DATA is BASIC’s ROM cartridge. Later you’ll compress maps, read from disk, and eventually store assets in machine code, but the structure remains the same: sequential streams of bytes consumed by loops.


Game Integration

  • Level packs: switch DATA blocks between rounds so each stage has unique walls and hazards.
  • Item tables: store item names, scores, and descriptions, reading them into arrays at start-up.
  • Music cues: load SID register sequences from DATA to play scripted tunes.
  • Cut-scenes: READ dialogue lines into a buffer and RESTORE when replaying the scene.

From the Vault

  • Gridlocked — slot the new DATA arrangement into the map lesson.
  • Paradroid — note how decks resemble precomputed tables.

Quick Reference

DATA 1,2,3         : REM store a list
READ A             : REM fetch next item
RESTORE            : REM rewind the pointer
READ A$,B$         : REM strings and mixed types allowed
  • READ values in the same order you wrote them.
  • Use RESTORE before re-reading the same block.
  • Keep DATA near related code or separated by sections for clarity.

What You’ve Learnt

  • Packed maps, vocab, and coordinates into DATA statements.
  • Consumed sequential data with READ and rewound with RESTORE.
  • Populated arrays and screen RAM without hand-entering every tile.
  • Prepared your engine for multiple levels, dialogue, and scripted content.

Next lesson: Smarter Decisions — tighten collision checks and branch logic to keep your worlds coherent.