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

The Warehouse

Lay a level out as DATA strings, parse them into a 2D array, and colour each cell — the array is the world, the screen only its picture.

17% of Crates

Crates begins with the warehouse, and the warehouse is a 2D array — like Sonar's grid, but this time the array holds the whole world: walls, floor, targets, crates. And the level is written as text you can sketch by hand, then parsed into the grid.

  90 RESTORE 900
 100 READ w, h
 120 LET sr = INT ((22 - h) / 2) + 1
 130 LET sc = INT ((32 - w) / 2)
 150 DIM g(h, w)
 160 CLS
 170 PRINT AT 0, 10; "*** CRATES ***"
 200 FOR r = 1 TO h
 210 READ r$
 220 FOR c = 1 TO w
 230 LET q$ = r$(c TO c)
 240 IF q$ = "W" THEN LET g(r,c) = 1: PRINT AT sr+r-1, sc+c-1; PAPER 1; INK 0; " "
 250 IF q$ = " " THEN PRINT AT sr+r-1, sc+c-1; PAPER 7; INK 0; " "
 260 IF q$ = "." THEN LET g(r,c) = 2: PRINT AT sr+r-1, sc+c-1; PAPER 2; INK 0; " "
 270 IF q$ = "C" THEN LET g(r,c) = 3: PRINT AT sr+r-1, sc+c-1; PAPER 6; INK 0; " "
 280 IF q$ = "P" THEN LET pr = r: LET pc = c: PRINT AT sr+r-1, sc+c-1; PAPER 7; INK 4; "P"
 290 NEXT c
 300 NEXT r
 900 DATA 5,5
 901 DATA "WWWWW"
 902 DATA "W . W"
 903 DATA "W C W"
 904 DATA "W P W"
 905 DATA "WWWWW"
The warehouse drawn from DATA: blue walls around a red target, a yellow crate, and the green player P
The level is a 5x5 grid of coloured cells — drawn from DATA, backed by a 2D array.

A level drawn as text

Lines 900–905 are the level. The first DATA line gives width and height; the next five are strings — "WWWWW", "W . W" — where each character is a cell: W wall, space floor, . target, C crate, P player. Writing a level as strings means you can see the room in the source and edit it like a picture, which is why DATA holds text here, not the bare numbers it held in Three in a Row. Lines 120–130 centre it on screen from the dimensions.

Parsing into the grid

DIM g(h,w) (line 150) is the world. The nested loop (200–300) reads each row string, pulls out each character with the slice r$(c TO c) you met in Cipher, and writes a number into the array: 1 wall, 2 target, 3 crate. Each cell is also painted with a PAPER colour as it is read. The player is the exception — its position is kept in pr, pc (line 280), because the player is a cursor that moves over cells, not a cell type itself.

Data, then display

This is the idea the whole game rests on: the array is the authority, the screen is just its picture. The DATA strings are the level design, g(r,c) is the live state, and the PRINT statements are the rendering. When a crate moves later, the array changes first and the screen follows. Sonar and The Caverns read their worlds; here you will read and write this one.

Next: put a player on the grid and let them move.