DEV Community

Cover image for 🎮 Building TETRIS with Amazon Q Developer CLI
Antrixsh Gupta
Antrixsh Gupta

Posted on

🎮 Building TETRIS with Amazon Q Developer CLI

Reimagining a Retro Classic with the Power of Conversational AI

"What if creating a classic game didn’t require lines of manual code, but just smart prompts?"

For the AWS Build Games Challenge, I set out to recreate one of the most iconic games in history—TETRIS—using the Amazon Q Developer CLI. What started as an experiment turned into a full-blown retro revival, powered entirely by conversational coding.

🎮 Why TETRIS?
Tetris is timeless. It's a game with simple rules, addictive mechanics, and infinite replayability. More importantly, it’s a perfect fit for AI-assisted development: it has clear game logic, visual feedback, and challenging mechanics like collision detection, rotation logic, and grid updates.

🤖 Effective Prompting Techniques I Discovered
Here are a few prompt patterns that worked like magic:

  • “Create a Tetris board with a 2D array and grid snapping logic.”

This led to a clean grid-based layout with collision-aware placement.

  • “Generate rotation logic for tetrominoes with wall kick handling.”

Amazon Q handled edge collision logic like a seasoned dev.

  • “Add a scoring system that gives 100 points per line cleared.”

Straightforward and accurate—no refactor needed!

  • “Build controls for left, right, soft drop, and rotation using keyboard input.”

Integrated seamlessly with pygame for smooth gameplay.

🧠 How Amazon Q Handled Classic Programming Challenges

🧠 Classic Challenge 🛠️ Prompt or Request 🤖 Amazon Q's Response Summary
Piece Rotation "Generate rotation logic for tetrominoes with wall kick handling." Used matrix transpose + reverse method.
Line Detection "Detect and clear full lines on the board." Loop checking for full rows with a clean overwrite logic.
Game Over Logic "End game when new piece collides at spawn location." Implemented top-row check and graceful game end.
Grid Drawing "Draw grid and update display for each frame using pygame." Automated drawing using pygame.draw.rect.
Score Updates "Show score on screen and update per line cleared." Added dynamic score overlay rendered live on screen.

⚙️ Dev Automation That Saved Me Hours
Instead of spending hours on:

  • Manual debugging of piece collisions
  • Writing the draw-loop boilerplate
  • Creating a scoring overlay
  • Managing piece spawning logic

…I used smart prompting like:

“Detect when the current piece cannot move down and freeze it in place.”

And Q instantly gave me the code + comments to integrate it directly.

🧩 Interesting Code Examples
1. Tetromino Definitions

// 7 classic pieces in matrix form
const PIECES = [
  [[1,1,1,1]],                // I
  [[1,1],[1,1]],              // O
  [[0,1,0],[1,1,1]],          // T
  [[0,1,1],[1,1,0]],          // S
  [[1,1,0],[0,1,1]],          // Z
  [[1,0,0],[1,1,1]],          // J
  [[0,0,1],[1,1,1]]           // L
];

Enter fullscreen mode Exit fullscreen mode

Why it matters: keeping shapes as 1/0 matrices lets every other function stay generic.

  1. One-Liner Rotation
function rotatePiece(piece) {
  // Transpose + reverse rows →  90° clockwise
  return piece[0].map((_, i) => piece.map(row => row[i]).reverse());
}

Enter fullscreen mode Exit fullscreen mode

AI insight: Q generated the transpose-reverse trick, which avoids nested loops.

  1. Move Validation Helper
function isValidMove(piece, dx, dy, shape = piece.shape) {
  for (let y = 0; y < shape.length; y++) {
    for (let x = 0; x < shape[y].length; x++) {
      if (!shape[y][x]) continue;
      const nx = piece.x + x + dx;
      const ny = piece.y + y + dy;
      if (
        nx < 0 || nx >= COLS || ny >= ROWS ||
        (ny >= 0 && board[ny][nx])
      ) return false;
    }
  }
  return true;
}

Enter fullscreen mode Exit fullscreen mode

Why it’s neat: a single routine handles wall bounds, floor contact, and collisions.

  1. Line Clearing & Scoring
// inside placePiece()
for (let y = ROWS - 1; y >= 0; y--) {
  if (board[y].every(cell => cell !== 0)) {
    board.splice(y, 1);                    // remove filled row
    board.unshift(Array(COLS).fill(0));    // add empty row on top
    score += 100;
    y++;                                   // re-check same index
  }
}
scoreElement.textContent = score;

Enter fullscreen mode Exit fullscreen mode

Why it’s efficient: splice + unshift swaps rows without rebuilding the whole grid.

  1. Main Game Loop
function gameLoop() {
  if (isValidMove(currentPiece, 0, 1)) {
    currentPiece.y++;          // move down
  } else {
    placePiece();              // freeze & spawn new
  }
  draw();                      // render frame
}

setInterval(gameLoop, 500);     // 2 frames per second

Enter fullscreen mode Exit fullscreen mode

A minimalist loop: gravity, collision handling, and rendering—all in ~10 lines.

🔗 Check the Complete Code
Explore the full Tetris game source code on my GitHub repository – clean, modular, and AI-assisted from start to finish!

🖼️ Screenshots of the Final Creation
Here’s how the final game looks in action:

Image description

Final Thoughts
Using Amazon Q Developer CLI to build Tetris has been both nostalgic and futuristic. It proved that AI can be a true coding partner—handling everything from low-level math to game loop architecture. I never imagined I could build something playable so quickly without writing every line of logic manually.

If you're a developer—even one with minimal game dev experience—I highly recommend giving this challenge a try. You’ll be surprised at how quickly creativity flows when AI does the heavy lifting.


🔗 Follow Me

If you liked this project, feel free to connect:

Thanks for reading!

Top comments (0)