Level Discoverystart here Intermediatethe architecture Expertin depth
Batch 4 · Volcano
Chapter 4

Destroy the landscape

Like in Worms: every shot digs a real hole in the scenery. We'll discover the magic trick to “punch a hole” in an image — and you'll get to dig yourself!

1Scenery that keeps its scars

Until now, the landscape was a nice fixed background. Now, every ball that lands tears off a piece of terrain. The holes stay: by the end of the round, the battlefield is all pockmarked.

2The magic: the canvas “eraser”

The canvas knows how to draw… but also how to erase in a shape! We switch to destination-out mode: everything we “draw” then becomes an eraser. Draw a circle → a perfect round hole in the terrain.

src/objects/Terrain.js (digging a crater)
ctx.globalCompositeOperation = 'destination-out'; // eraser mode
ctx.arc(x, y, radius, 0, 6.28);             // a circle…
ctx.fill();                                   // …that erases!
Try it! Click (or drag your finger) on the hill to dig. Look: you can make caves, bridges, overhangs… exactly like in Worms.

Dig the terrain!

Tip: aim under a bump to dig a cave and leave a “bridge” above.

3Solid or not? The game's rule

How does the game know whether a ball has hit the ground? No need to store a huge image in memory. We keep: the ground height at each spot, and the list of craters. A point is solid if:

The rule. “I'm below the surface of the ground AND I'm in no crater.” Otherwise, it's empty space.

This little rule is enough to create caves and overhangs: a crater dug under a bump leaves the matter above it “floating”.

empty (crater) solid ↑ overhang that “floats”
Below the surface but inside a crater → empty. The bump above still holds.
For the curious. Testing “am I in a crater?” = comparing the distance to each crater's centre with its radius. That's the Pythagorean theorem! (dx² + dy² ≤ radius²).

4The details that make it feel real

★Well done, you've been all the way round!

You now roughly know how a web game comes to life:

a loop that redraws 60×/s, a bit of maths for the physics and the scenery, a networked referee to play together, and drawing tricks to smash it all up. And above all: we move forward step by step, each one tested before the next.

The pros' secret. Nobody writes a big game in one go. You make a small version that works, you improve it, again and again. Exactly the 4 batches of this notebook.

↩ Back to home

← Chapter 3 Chapter 5 →