Workshop › Mario-style Platformer › Step 25 of 37
Enemies
What we are doing
The e characters become enemies, just like coins: 28×28 boxes with a speed and a direction dir (-1 is left). For now
they only stand and are drawn.
```js
let enemies
enemies = []
if (line[col] === 'e') enemies.push({ x: x + 2, y: y + 4, w: 28, h: 28, vx: 0, vy: 0, dir: -1 })
ctx.fillStyle = '#7c2d12'
for (const enemy of enemies) {
ctx.fillRect(enemy.x, enemy.y, enemy.w, enemy.h)
}
```
What it means
- An enemy sits 2 pixels in from its tile's left and 4 down, so its bottom (4 + 28) is on the tile's bottom.
- They are drawn dark brown in the world, before the player.
Your turn
- Under
let coins write let enemies; in loadLevel, under coins = [], write enemies = [].
- Under the
'o' line write the 'e' line.
- In
draw, above ctx.fillStyle = '#dc2626', write the enemy drawing and an empty line.
Skills: Arrays and objects, Game states
← Count, and screen versus world Enemies that walk →
All steps
- The sky
- A level made of text
- Draw the tiles
- What is solid?
- Is a box touching anything?
- Find the start
- Draw the player
- Gravity
- Landing on tiles
- Which keys are held?
- Speed up
- Friction
- Walls
- Jump
- Bump your head
- Short hops
- A few frames of grace
- Coyote time
- A camera that follows
- Stop at the ends
- Draw only what is on screen
- Coins
- Collect them
- Count, and screen versus world
- Enemies
- Enemies that walk
- Do not walk off ledges
- Lives and a fresh start
- Falling into a pit
- Enemies hurt
- Stomp!
- Squashed enemies are gone
- Game over
- Play again
- The flag
- Reach the flag
- Build it yourself: jump buffering