A classic Tetris game built with Java and Swing GUI, designed to run in Eclipse IDE.
TetrisGame/
├── src/
│ ├── Tetris.java # Main entry point — launches the game window
│ ├── TetrisGame.java # Game panel — rendering, input handling, game loop
│ ├── Board.java # Board logic — grid, line clearing, piece placement
│ └── Tetromino.java # Piece logic — shapes, colors, movement, rotation
└── README.md
- Java JDK 8 or higher
- Eclipse IDE (any recent version)
- Open Eclipse → File → New → Java Project
- Name the project
TetrisGameand click Finish - Right-click the
srcfolder → New → Class for each of the four files:TetrisTetrisGameBoardTetromino
- Paste the corresponding source code into each class file
- Right-click
Tetris.java→ Run As → Java Application
| Key | Action |
|---|---|
← → |
Move left/right |
↑ |
Rotate piece |
↓ |
Soft drop |
Space |
Hard drop |
P |
Pause / Resume |
- Pieces fall from the top of the board one at a time
- Complete horizontal lines to clear them and earn points
- The game ends when a new piece cannot spawn (board is full)
- Score increases based on how many lines are cleared at once:
| Lines Cleared | Points |
|---|---|
| 1 | 100 |
| 2 | 400 |
| 3 | 900 |
| 4 | 1600 |
| Piece | Shape | Color |
|---|---|---|
| I | ████ | Cyan |
| O | ██ | Yellow |
| ██ | ||
| T | ███ | Magenta |
| L | ███ | Orange |
| J | ███ | Blue |
| S | ██ | Green |
| Z | ██ | Red |
The entry point of the application. Creates the JFrame window, adds the game panel, sets window properties, and starts the game loop.
Extends JPanel and implements ActionListener. Responsible for:
- Rendering the board and active piece using
paintComponent - Handling keyboard input (movement, rotation, drop, pause)
- Running the game timer that drives piece descent
- Tracking and displaying the score
Manages the game grid (a 2D integer array). Handles:
- Collision/validity checking for piece placement
- Locking a piece onto the board when it lands
- Detecting and clearing completed lines
- Storing color indices for rendering
Represents a single falling piece. Handles:
- All 7 standard piece shapes (I, O, T, L, J, S, Z)
- Random piece generation
- Movement (translate x/y)
- Clockwise and counter-clockwise rotation
You can easily tweak the following constants in TetrisGame.java:
private static final int BOARD_WIDTH = 10; // Number of columns
private static final int BOARD_HEIGHT = 20; // Number of rows
private static final int CELL_SIZE = 30; // Pixel size of each cellAnd adjust the game speed (in milliseconds) by changing the timer delay:
timer = new Timer(500, this); // Lower = faster┌──────────────────────────────┬─────────────────┐
│ │ Score: 400 │
│ [ Tetris Board ] │ │
│ │ Controls: │
│ │ ← → Move │
│ │ ↑ Rotate │
│ │ ↓ Soft Drop │
│ │ Space Hard Drop │
│ │ P Pause │
└──────────────────────────────┴─────────────────┘
This project is open source and free to use for educational purposes.