Classic Tetris in C++ — built from scratch with a modular OOP architecture and raylib.
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.
- 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
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
The game loop runs at a fixed tick rate managed by raylib's GetTime(). On each tick:
- Input is polled for left/right/down/rotate/hard-drop
- Block movement is validated against grid boundaries and existing cells
- Locking happens when a block can no longer move down — cells are written to the grid
- Line clearing scans for full rows, removes them, and shifts rows above down
- Score is updated based on lines cleared (1→100, 2→300, 3→500, 4→800)
- New block is spawned; game over is triggered if spawn position is occupied
g++(C++17 or later)- raylib installed on your system
# Clone the repo
git clone https://github.com/tijaruS/Tetris.git
cd Tetris
# Build using the provided script
bash build.sh
# Run
./build/tetrisg++ main.cpp game.cpp grid.cpp block.cpp blocks.cpp colors.cpp position.cpp \
-o build/tetris -lraylib -lGL -lm -lpthread -ldl -lrt -lX11g++ main.cpp game.cpp grid.cpp block.cpp blocks.cpp colors.cpp position.cpp \
-o build/tetris.exe -lraylib -lopengl32 -lgdi32 -lwinmm| Key | Action |
|---|---|
← / → |
Move left / right |
↓ |
Soft drop |
↑ |
Rotate clockwise |
Space |
Hard drop |
Surajit Das — @tijaruS