Workshop › Zelda-style Dungeon › Step 2 of 22

The hero

What we are doing

The hero is a green square, 22 pixels, standing in the middle of the P tile. findIn finds a character in a room. The hero also remembers which way it faces (down at first); the sword will need that. reset starts a new game.

```js
const SIZE = 22 // the player's and the enemies' bodies
let player

function reset() {
  const start = findIn(ROOMS[0][0], 'P')
  player = { x: start.col * T + (T - SIZE) / 2, y: start.row * T + (T - SIZE) / 2, dir: [0, 1] }
  enter(0, 0)
}

function findIn(lines, ch) {
  const row = lines.findIndex((line) => line.includes(ch))
  return { row, col: lines[row].indexOf(ch) }
}

  ctx.fillStyle = '#16a34a'
  ctx.fillRect(player.x, player.y, SIZE, SIZE)

reset()
```

What it means

Your turn

  1. Under TOP, write SIZE; under room, write player.
  2. Above enter, write reset and findIn.
  3. In draw, draw the hero before ctx.restore(); at the bottom, call reset() instead of enter(0, 0).

Skills: Game states, Functions

All steps

  1. A room
  2. The hero
  3. The game loop
  4. Walk
  5. Walls
  6. A touch pad
  7. Next room
  8. The sword
  9. Enemies
  10. Strike!
  11. They chase you
  12. They wander
  13. Ouch
  14. Blinking and hearts
  15. Keys and hearts
  16. A world that remembers
  17. The locked door
  18. A timer
  19. Game over
  20. The stairs out
  21. Best time
  22. Build it yourself: cleared rooms