Workshop › Tower Defense › Step 2 of 42
A map of tiles
What we are doing
Two loops, one inside the other, visit every tile: for each row, every column. Each tile is painted green, so the
whole map becomes grass.
```js
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS; col++) {
ctx.fillStyle = '#3f6212'
ctx.fillRect(col * TILE, TOP + row * TILE, TILE, TILE)
}
}
```
What it means
- The outer loop counts
row from 0 to 8, the inner one col from 0 to 11 for each row.
- The tile in row
row, column col starts at col * TILE across and TOP + row * TILE down.
Your turn
At the end of the code, after an empty line, write the two loops.
Skills: Drawing on a canvas, Loops
← Measure the map Draw it every frame →
All steps
- Measure the map
- A map of tiles
- Draw it every frame
- The road as corners
- Fill in the road
- Draw the road
- Walking by distance
- Enemies on the road
- Walk
- Off the end of the road
- Here they come
- Towers
- From a click to a tile
- Click to build
- Where can you build?
- Show the gold
- Show before you click
- Show the range
- Pick a target
- Fire!
- A hit hurts
- Homing bullets
- Fizzle out
- Gold for every kill
- Lives
- Health bars
- Build, then defend
- Space starts a wave
- The wave is over
- Stronger every wave
- Closer together
- Out of lives
- No building after the end
- The most waves survived
- The game over screen
- Play again
- The bottom bar
- Click to start
- A second tower: the cannon
- Tower buttons
- Splash damage
- Build it yourself: sell a tower