Workshop › Scorched Earth-style Artillery › Step 6 of 23
Left and right turn your barrel, up and down change the power. The angle stays between flat left and flat right, and the power between 2 and 12.
```js
const MAX_POWER = 12
function aimBy(dAngle, dPower) {
const t = tanks[0]
t.angle = Math.max(-Math.PI, Math.min(0, t.angle + dAngle))
t.power = Math.max(2, Math.min(MAX_POWER, t.power + dPower))
}
document.addEventListener('keydown', (event) => {
if (event.key === 'ArrowLeft') aimBy(-0.03, 0)
else if (event.key === 'ArrowRight') aimBy(0.03, 0)
else if (event.key === 'ArrowUp') aimBy(0, 0.25)
else if (event.key === 'ArrowDown') aimBy(0, -0.25)
else return
event.preventDefault()
})
```aimBy changes the angle and power by the given amounts, then keeps them within their limits.-Math.PI is flat left, 0 flat right; everything between points up.else return leaves other keys alone; only the arrow keys are kept from scrolling the page.H, write MAX_POWER.draw, write aimBy and the key listener.Skills: Keyboard and mouse input
← The game loop Angle and power →