Workshop › Zelda-style Dungeon › Step 15 of 22
Walking onto a key k picks it up; walking onto a heart h gives back a heart (up to 3). The tile becomes floor.
Keys are yellow, hearts red, and the top strip counts your keys.
```js
let keysHeld
keysHeld = 0
// Things on the tile under the middle of the player.
const col = Math.floor(cx / T)
const row = Math.floor(cy / T)
const here = tiles[row][col]
if (here === 'k' || here === 'h') {
if (here === 'k') keysHeld += 1
else hearts = Math.min(3, hearts + 1)
tiles[row][col] = '.'
}
if (ch === 'k') {
ctx.fillStyle = '#eab308'
ctx.fillRect(x + 10, y + 8, 12, 16)
}
if (ch === 'h') {
ctx.fillStyle = '#e11d48'
ctx.fillRect(x + 9, y + 9, 14, 14)
}
ctx.fillStyle = 'white'
ctx.font = 'bold 16px sans-serif'
ctx.textAlign = 'left'
ctx.fillText('Keys ' + keysHeld, 100, 30)
```cx, cy) is the one it stands on.Math.min(3, hearts + 1) never goes above 3 hearts.tiles[row][col] to floor is why the room was copied into arrays.hearts, write keysHeld; in reset, set it to 0.update, before the swing countdown, pick up what is under the hero.Skills: Game states, Collision detection
← Blinking and hearts A world that remembers →