Workshop › Asteroids › Step 11 of 41
Asteroids has no walls: off the right edge you come back on the left, off the top you reappear at the bottom. %, the
remainder, does it, with one trap: in JavaScript -5 % 600 is -5. Adding the size first fixes it.
```js
// Wrap a coordinate around the screen. Plain % keeps the sign in JavaScript (-5 % 600 is -5), so add the size first.
function wrap(value, size) {
return ((value % size) + size) % size
}
ship.x = wrap(ship.x + ship.vx, canvas.width)
ship.y = wrap(ship.y + ship.vy, canvas.height)
```wrap(-5, 600): -5 % 600 is -5, + 600 is 595, 595 % 600 is 595.wrap(605, 600): 5 + 600 is 605, and the second % brings it back to 5.wrap, with the canvas width for x and its height for y.function resetShip() { write the comment and wrap, followed by an empty line.update, replace the two moving lines with the wrapped ones.Skills: Velocity and gravity
← A speed limit Making a bullet →