Skip to content

tijaruS/Tetris

Repository files navigation

Tetris

Classic Tetris in C++ — built from scratch with a modular OOP architecture and raylib.

C++ Shell raylib Platform


Overview

A fully functional Tetris engine written in C++ with a clean object-oriented architecture. Each game subsystem — grid, blocks, game logic, colors, rendering — lives in its own module. Audio is integrated via raylib's sound API for sound effects on line clears and game over.

Features

  • Classic Tetris mechanics — all 7 tetrominoes, rotation, line clearing, collision detection, game over
  • Modular OOP design — game logic, grid, blocks, colors, and rendering are fully decoupled
  • Audio system — sound effects for piece placement, line clears, and game over
  • Score tracking — score increments based on number of lines cleared simultaneously
  • Reusable Position & Block classes — clean abstractions for tetromino state and rotation

Architecture

Tetris/
├── main.cpp          # Entry point, game loop
├── game.cpp/.hpp     # Core game logic, state machine, input handling
├── grid.cpp/.hpp     # 20×10 grid, line detection and clearing
├── block.cpp/.hpp    # Base Block class — position, rotation, movement
├── blocks.cpp        # All 7 tetromino definitions (I, O, T, S, Z, J, L)
├── colors.cpp/.hpp   # Color palette for each piece type
├── position.cpp/.hpp # Row/column position abstraction
├── sounds/           # Audio assets (.wav / .mp3)
├── build/            # Compiled binary output
└── build.sh          # Build script

How It Works

The game loop runs at a fixed tick rate managed by raylib's GetTime(). On each tick:

  1. Input is polled for left/right/down/rotate/hard-drop
  2. Block movement is validated against grid boundaries and existing cells
  3. Locking happens when a block can no longer move down — cells are written to the grid
  4. Line clearing scans for full rows, removes them, and shifts rows above down
  5. Score is updated based on lines cleared (1→100, 2→300, 3→500, 4→800)
  6. New block is spawned; game over is triggered if spawn position is occupied

Building

Prerequisites

  • g++ (C++17 or later)
  • raylib installed on your system

Linux / macOS

# Clone the repo
git clone https://github.com/tijaruS/Tetris.git
cd Tetris

# Build using the provided script
bash build.sh

# Run
./build/tetris

Manual build

g++ main.cpp game.cpp grid.cpp block.cpp blocks.cpp colors.cpp position.cpp \
    -o build/tetris -lraylib -lGL -lm -lpthread -ldl -lrt -lX11

Windows (MinGW)

g++ main.cpp game.cpp grid.cpp block.cpp blocks.cpp colors.cpp position.cpp \
    -o build/tetris.exe -lraylib -lopengl32 -lgdi32 -lwinmm

Controls

Key Action
/ Move left / right
Soft drop
Rotate clockwise
Space Hard drop

Author

Surajit Das@tijaruS

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors