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

Lesson 3 of 6

Detect collisions

The heart of the game: collides(grid, piece) says whether the piece overlaps a filled cell or leaves the grid.

Walk every shape cell worth 1: its grid position is (piece.y + r, piece.x + c). Out of bounds (bottom, left, right) or cell at 1 → collision.

var row = piece.y + r;
var col = piece.x + c;
if (row >= grid.length || col < 0 || col >= grid[0].length) {
  return true;
}

🎯 Your goal

Write collides(grid, piece): return true if a 1-cell of the piece leaves the grid (bottom, left, right) or lands on a filled cell, otherwise false.

PRO lesson

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

Discover PRO