Workshop › Scorched Earth-style Artillery › Step 9 of 23
Every frame gravity adds a little to the downward speed, and the shell moves by its speed. That is all it takes for a
real curve. fly moves a shell one frame and says what it hit: 'away' off the side, 'ground', or null.
When the shell hits something it disappears, and you can aim again.
```js
const GRAVITY = 0.15
// One frame of flight: gravity pulls down. Returns what it hit, or null.
function fly(s) {
s.vy += GRAVITY
s.x += s.vx
s.y += s.vy
if (s.x < 0 || s.x >= W) return 'away'
if (s.y >= groundAt(s.x)) return 'ground'
return null
}
function update() {
if (state !== 'flying') return
const hit = fly(shell)
if (!hit) return
shell = null
state = 'aiming'
}
update()
```y is at least the ground's y there.fly takes the shell as a parameter, so later the computer can try imaginary shells with it too.H, write GRAVITY.aimBy, write fly with its comment and update.loop, call update() before draw().Skills: Velocity and gravity, The game loop