Workshop › OutRun-style Racer › Step 5 of 44
One piece is not a road. The track will be a list of 800 short segments, 200 units each, laid end to end. Later
each segment also gets a curve; for now they are all straight. Nothing changes on screen: we build the track in memory.
```js
const SEG = 200 // length of one road segment, in world units
let segments // the track: { curve } for each segment
let trackLength
// The track is made of stretches: `count` segments bending by `curve` (0 is straight, + right, - left).
function buildTrack() {
segments = []
const add = (count, curve) => {
for (let i = 0; i < count; i++) segments.push({ curve })
}
add(800, 0) // straight for now
trackLength = segments.length * SEG
}
buildTrack()
```segments and trackLength are variables that buildTrack fills.const add = (count, curve) => { ... } is an arrow function, a short way to write a function.for loop runs count times and push adds { curve } (short for { curve: curve }) to the end of the list.trackLength is 800 × 200. The last line builds the track once.const H = canvas.height write the SEG line.DEPTH line leave an empty line, then write the two let lines, the comment and buildTrack.requestAnimationFrame(loop), write buildTrack().Skills: Arrays and objects, Loops
← One piece of road Where we are on the track →