Workshop › Lunar Lander › Step 3 of 31
Somewhere among the hills there must be a flat place to land: three points in a row get the same height. That is the pad, drawn as a green bar.
```js
let pad // { x1, x2, y }: the flat landing pad
// Random hills, with one flat stretch: the pad.
const width = 2
const start = 1 + Math.floor(Math.random() * (points - 2 - width))
const y = 250 + Math.random() * 70
for (let i = start; i <= start + width; i++) ground[i] = y
pad = { x1: start * STEP, x2: (start + width) * STEP, y }
ctx.fillStyle = '#22c55e'
ctx.fillRect(pad.x1, pad.y - 2, pad.x2 - pad.x1, 4)
```start is a random point from 1, chosen so the pad fits before the right edge.start to start + width to the same random y.pad keeps the pad's left and right x in pixels and its y. It is drawn as a green bar 4 pixels high on that y.let ground ... write let pad with its comment; above function makeGround() { write the comment line.makeGround, under the ground = ... line, write the five pad lines.draw, under ctx.fill(), write the two green lines.Skills: Arrays and objects, Drawing on a canvas
← Draw the hills The height at any x →