Workshop › Pong › Step 24 of 26
Playing alone? The computer takes the right paddle: when the ball comes toward it, it moves to meet it, but a bit slower than you, so it can be beaten.
```js
const AI_SPEED = 4 // slower than the player, so the computer can be beaten
let twoPlayers = false
if (twoPlayers) {
if (keys.ArrowUp) right.y -= PADDLE_SPEED
if (keys.ArrowDown) right.y += PADDLE_SPEED
} else if (ball.vx > 0) {
const target = ball.y + BALL / 2 - PADDLE_H / 2
right.y += clamp(target - right.y, -AI_SPEED, AI_SPEED)
}
```vx > 0).target is where the paddle's top should be so its middle meets the ball.clamp(..., -AI_SPEED, AI_SPEED) limits each move to 4 pixels, slower than your 6.PADDLE_SPEED, write AI_SPEED; under let state, write let twoPlayers = false.update, replace the two arrow-key lines with the if (twoPlayers) ... else if ... block.Skills: Game states
← Faster every hit One player or two →