Workshop › Tetris-style Blocks › Step 11 of 32
Try, then move
What we are doing
tryMove(dx, dy) asks fits about the new place first and moves only if the answer is yes. It returns whether it
moved.
```js
function tryMove(dx, dy) {
if (!fits(piece.shape, piece.x + dx, piece.y + dy)) return false
piece.x += dx
piece.y += dy
return true
}
```
What it means
dx and dy say how many columns right and rows down: left is (-1, 0), down is (0, 1).
- If the piece would not fit at the new place, return
false without changing anything.
- Otherwise move it (
+= adds) and return true.
Your turn
Write tryMove under fits, after an empty line.
Skills: Collision detection, Functions
← Blocks are in the way Move with the arrows →
All steps
- The well
- The board as rows of numbers
- Draw one cell
- Draw the whole board
- The number is the color
- A piece is a small grid
- A list of shapes
- All seven pieces
- Does it fit inside the well?
- Blocks are in the way
- Try, then move
- Move with the arrows
- Turn a matrix
- Turn with Up
- Wall kicks
- Falling by itself
- A random piece at the top
- Land and lock
- One function starts a game
- When the pile reaches the top
- Game Over on screen
- Play again
- Clear full rows
- Points for lines
- The side panel
- Levels
- Faster every level
- A fair bag of pieces
- The next piece
- Drop it
- The ghost piece
- Build it yourself: hold a piece