CodeAgeBy Sitezack
Final project: your TetrisTetris on Canvas, inside a real website0 XP
Chapter progress0/6

Lesson 5 of 6

Move without cheating

A move is only valid if it creates no collision: first test the target position with a trial object, only then move.

move returns true if the move happened, false otherwise — the game will know when the piece is stuck.

var test = {x: piece.x + dx, y: piece.y, shape: piece.shape};
if (collides(grid, test)) {
  return false;
}

🎯 Your goal

Write move(grid, piece, dx): if the piece shifted by dx causes no collision, apply the move (piece.x += dx) and return true; otherwise move nothing and return false.

PRO lesson

This advanced chapter is part of CodeAge PRO. Unlock it to keep your adventure going.

Discover PRO