Back to Blog

A Crow Showed Up In A Tetris Game

Celest KimCelest Kim

Video: A Crow Showed Up In A Tetris Game by Taught by Celeste AI - AI Coding Coach

Watch full page →

A Crow Showed Up In A Tetris Game

This playful twist on classic Tetris introduces unpredictable elements that disrupt the usual gameplay. Imagine the wind blowing pieces sideways and a crow swooping in to snatch your falling blocks mid-air, adding chaos and challenge to the familiar puzzle mechanics.

Code

class TetrisGame {
  constructor() {
    this.board = this.createBoard(20, 10);
    this.currentPiece = this.spawnPiece();
    this.crows = [];
    this.windDirection = 0; // -1 for left, 1 for right, 0 for none
  }

  createBoard(rows, cols) {
    return Array.from({ length: rows }, () => Array(cols).fill(0));
  }

  spawnPiece() {
    // Returns a new Tetris piece at the top center
    return { x: 4, y: 0, shape: [[1,1],[1,1]] }; // example: square piece
  }

  applyWind() {
    if (this.windDirection !== 0) {
      this.currentPiece.x += this.windDirection;
      // Ensure piece stays within board boundaries
      this.currentPiece.x = Math.max(0, Math.min(this.currentPiece.x, 10 - this.currentPiece.shape[0].length));
    }
  }

  crowSwoop() {
    // A crow swoops in and steals the current piece mid-air
    if (Math.random() < 0.1) { // 10% chance each tick
      this.crows.push({ x: this.currentPiece.x, y: this.currentPiece.y });
      this.currentPiece = null; // piece stolen
      console.log('A crow stole your piece!');
    }
  }

  update() {
    if (!this.currentPiece) {
      if (this.crows.length >= 5) {
        console.log('Game Over: The crows took the entire board!');
        return;
      }
      this.currentPiece = this.spawnPiece();
    } else {
      this.applyWind();
      this.crowSwoop();
      if (this.currentPiece) {
        this.currentPiece.y += 1; // piece falls down
      }
    }
  }
}

// Usage example
const game = new TetrisGame();
setInterval(() => game.update(), 500);

Key Points

  • Introducing dynamic environmental effects like wind can add unexpected challenges to classic games.
  • Random events, such as a crow stealing pieces, create emergent gameplay and increase difficulty.
  • Managing game state carefully allows for smooth integration of new mechanics without breaking core gameplay.
  • Visual and audio cues can enhance player awareness of chaotic elements like swooping crows.
  • Creative twists on familiar games keep players engaged by surprising them with novel interactions.