Workshop › Tetris-style Blocks › Step 15 of 32
A piece against a wall often cannot turn in place, and a game that says "no" feels bad. So we also try the turn nudged one or two cells sideways, and take the first place that fits.
```js
const turned = rotate(piece.shape)
// Wall kicks: if the turned piece does not fit, try nudging it sideways.
for (const kick of [0, -1, 1, -2, 2]) {
if (fits(turned, piece.x + kick, piece.y)) {
piece.shape = turned
piece.x += kick
return true
}
}
return false
}
```for (const kick of [0, -1, 1, -2, 2]) tries each offset in turn.kick, and return true
stops the search.return false after the loop.In tryRotate, replace everything under const turned = ... with the lines shown.
Skills: Collision detection, Loops
← Turn with Up Falling by itself →