v0.6: real PNG textures (head with eyes, scaled body, corners, tapering tails, apple, board); renderPieces classifier + tests

This commit is contained in:
2026-09-09 22:06:09 +01:00
parent 4e39e1912a
commit eacad97240
24 changed files with 179 additions and 30 deletions
+25 -1
View File
@@ -34,6 +34,30 @@ function turn(current, desired) {
return desired
}
// Classify every segment as a texture piece (head/body/corner/tail + compass side),
// so the page can render each cell with the right PNG. Returns one name per segment.
// Sides are screen compass (y grows down): e/w/n/s = right/left/up/down.
function renderPieces(snakeCells, dir) {
const D = { '1,0': 'e', '-1,0': 'w', '0,-1': 'n', '0,1': 's' }
const opp = { e: 'w', w: 'e', n: 's', s: 'n' }
return snakeCells.map((s, i) => {
if (i === 0) return 'head_' + D[dir.x + ',' + dir.y]
const prev = snakeCells[i - 1]
const next = snakeCells[i + 1]
const from = D[(prev.x - s.x) + ',' + (prev.y - s.y)] // side toward head
if (!next) return 'tail_' + opp[from] // tip points away from body
const to = D[(next.x - s.x) + ',' + (next.y - s.y)] // side toward tail tip
if ((from === 'e' || from === 'w') && (to === 'e' || to === 'w')) return 'body_h'
if ((from === 'n' || from === 's') && (to === 'n' || to === 's')) return 'body_v'
// corner: name = the two sides it connects; pairs are alphabetically sorted
const pair = [from, to].sort()
if (pair[0] === 'e' && pair[1] === 'n') return 'corner_en'
if (pair[0] === 'n' && pair[1] === 'w') return 'corner_nw'
if (pair[0] === 'e' && pair[1] === 's') return 'corner_es'
return 'corner_sw'
})
}
// Rotate a direction 90°: clockwise order right->down->left->up. Always ±90°, never a reversal.
function rotateDir(dir, clockwise) {
const order = [{ x: 1, y: 0 }, { x: 0, y: 1 }, { x: -1, y: 0 }, { x: 0, y: -1 }]
@@ -56,4 +80,4 @@ function spawnFood(snake, N) {
return { x, y }
}
module.exports = { initialSnake, step, turn, rotateDir, spawnFood }
module.exports = { initialSnake, step, turn, renderPieces, rotateDir, spawnFood }