DYSTOPIANPIXEL
PHASELOCK / NEXTBASIC / DYSTOPIAN PIXEL

Stage 6 — Introduce the logical 20×16 tile grid

Build a top-down phase puzzle game, one hardware-tested development stage at a time.

MILO in the Blue and Amber Phase Grid
Concept artwork · PHASELOCK development tutorial through v0.13.

With the movement animation working, the next major architectural step is to stop treating x and y as the game state. Pixel coordinates are good for smooth animation, but puzzle rules need stable tile coordinates.

Two coordinate systems

CoordinatePurposeCurrent example
playerTileX / playerTileYAuthoritative logical game position. Used for collision, phase rules, scoring, visited tiles and enemies.Start at (9,7).
x / yTransient hardware-sprite position used while animating between tiles.Start at (144,112).

The relationship is currently straightforward because the grid origin is pixel (0,0) and each tile is 16 pixels square:

Current tile-to-pixel mapping

x = playerTileX * tileSize
y = playerTileY * tileSize
tileSize = 16

Why 20×16?

The hardware sprite coordinate area used by the current test is 320 pixels wide by 256 pixels high. Dividing by 16 gives 20 columns and 16 rows. Therefore the valid logical coordinates are X = 0 to 19 and Y = 0 to 15.

Logical-grid dimensions

320 / 16 = 20 tile columns
256 / 16 = 16 tile rows

DIM levelMap(19,15)

NextBASIC array bounds are written using the highest valid index, so levelMap(19,15) provides entries 0–19 by 0–15: 20×16 logical tile positions.

Initialisation changes

v0.10 grid state

2150 LET tileSize=16
2152 LET gridWidth=20
2154 LET gridHeight=16
2156 LET playerTileX=9
2158 LET playerTileY=7
2160 LET x=playerTileX*tileSize
2162 LET y=playerTileY*tileSize
2165 LET moveDelay=2
2167 LET poseDelay=2
2168 DIM levelMap(19,15)

Approve the destination before animating

The directional procedures no longer decide whether a pixel coordinate is inside the screen. Instead they request a logical move. attemptMove calculates the target tile, checks the logical boundaries, and calls animateMove only if the destination is legal.

v0.10 logical movement approval

6000 DEFPROC attemptMove(tileDx,tileDy,pStart)
6010 LOCAL targetTileX
6020 LOCAL targetTileY
6030 LOCAL moveAllowed
6040 LET targetTileX=playerTileX+tileDx
6050 LET targetTileY=playerTileY+tileDy
6060 LET moveAllowed=1
6070 IF targetTileX<0 OR targetTileX>=gridWidth THEN LET moveAllowed=0
6080 IF targetTileY<0 OR targetTileY>=gridHeight THEN LET moveAllowed=0
6090 IF moveAllowed=1 THEN PROC animateMove(tileDx,tileDy,pStart)
6100 IF moveAllowed=1 THEN LET playerTileX=targetTileX
6110 IF moveAllowed=1 THEN LET playerTileY=targetTileY
6120 IF moveAllowed=1 THEN LET x=playerTileX*tileSize
6130 IF moveAllowed=1 THEN LET y=playerTileY*tileSize
6140 IF moveAllowed=1 THEN PROC showPlayerTile()
6150 ENDPROC

The recalculation of x and y on lines 6120–6130 is intentional. During animation x/y are advanced one pixel at a time. At the end of the move they are normalised from the authoritative tile coordinate. This prevents cumulative pixel drift if the animation code is ever changed.

Direction procedures become simple requests

Direction → tile delta → directional pattern block

5000 DEFPROC moveUp()
5010 PROC attemptMove(0,-1,0)
5020 ENDPROC

5100 DEFPROC moveDown()
5110 PROC attemptMove(0,1,8)
5120 ENDPROC

5200 DEFPROC moveLeft()
5210 PROC attemptMove(-1,0,12)
5220 ENDPROC

5300 DEFPROC moveRight()
5310 PROC attemptMove(1,0,4)
5320 ENDPROC

Debugging the logical state

v0.10 prints the current logical tile coordinate on the third text row. This is intentionally a development aid, not a proposed final HUD element.

Temporary tile-coordinate display

8000 DEFPROC showPlayerTile()
8010 PRINT AT 2,0;"TILE X:";playerTileX;" Y:";playerTileY;"   "
8020 ENDPROC

Hardware acceptance checks for v0.10

  • Every successful key press still moves exactly one tile.
  • The displayed logical coordinate changes by exactly one on the expected axis.
  • Moving away from a tile and returning to it restores exactly the same pixel position.
  • The v0.9 lean/travel/recover animation still looks correct.
  • The player cannot move past X 0 or X 19.
  • The player cannot move past Y 0 or Y 15.
  • A rejected boundary move does not animate or alter the logical coordinate.