Workshop › Zelda-style Dungeon › Step 13 of 22

Ouch

What we are doing

You have 3 hearts. An enemy touching you costs one. Then you are safe for 60 frames, and you and the enemy bounce apart, so it can't keep hitting you.

```js
let hearts
let hurt // frames the player cannot be hurt again

  hearts = 3
  hurt = 0

  if (hurt > 0) hurt -= 1

    if (e.hp > 0 && hurt === 0 && overlap(box(e), box(player))) {
      hearts -= 1
      hurt = 60
      // Both bounce apart, so an enemy cannot stay stuck on top of you.
      const ax = Math.sign(player.x - e.x) || 1
      const ay = Math.sign(player.y - e.y)
      const horizontal = Math.abs(player.x - e.x) >= Math.abs(player.y - e.y)
      for (let i = 0; i < 6; i++) move(player, horizontal ? ax * 4 : 0, horizontal ? 0 : ay * 4)
      e.knock = 10
      e.kx = horizontal ? -ax * 3 : 0
      e.ky = horizontal ? 0 : -ay * 3
    }
```

What it means

Your turn

  1. Above held, write hearts and hurt; in reset, set them above swing.
  2. At the top of update, count hurt down.
  3. In the enemy loop, after the sword block, handle a touch.

Skills: Collision detection, Game states

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