Welcome to the TDD Tetris project. This repository demonstrates how to build a simple Tetris game in Python using Test-Driven Development (TDD) in tandem with GitHub Copilot or a similar AI-assisted coding tool.
- Overview
- Features and Final Game Description
- Requirements
- Setup Instructions
- Windows
- macOS and Linux
- Running the Tests
- Running the Program
- First 5 Test Cases
- Next Features to Implement
- Using TDD with Copilot
- Continuous Integration
- Language: Python
- Testing Framework: Pytest
- Goal: Provide a skeleton Tetris game where participants learn TDD by writing tests first, watching them fail, then using Copilot to implement code until the tests pass.
When fully implemented, this Tetris clone should include:
- A 20×10 grid representing the Tetris board
- Standard Tetris shapes (I, O, T, S, Z, J, L) spawning at the top
- Movement (left, right, down) and rotation for each piece
- Line clearing when a row is completely filled
- Score and level tracking, with speed increasing after certain cleared lines
- Game over detection when no space is left to place a new piece
Initially, the game is incomplete. You will use TDD and Copilot to build it step by step.
- Python 3.8 or later and pip
- Git if you plan to clone this repository
- A code editor or IDE with optional Copilot integration
- Open a command prompt or PowerShell in the project folder
- Create a virtual environment by running: python -m venv venv
- Activate the virtual environment: For Command Prompt: venv\Scripts\activate For PowerShell: .\venv\Scripts\activate
- Install dependencies: pip install -r requirements.txt
- Open a terminal in the project folder
- Create a virtual environment by running:
python3 -m venv venv - Activate the virtual environment:
source venv/bin/activate - Install dependencies:
pip install -r requirements.txt
If there is no requirements file, simply install the pytest package by running pip install pytest.
Within the project folder and with your virtual environment activated, run this command:
pytest
All tests in the tests directory will run. You will initially see many failures, which is expected. Use TDD to implement the features until these tests pass.
Currently, the repository does not have a dedicated main script to start the game loop. However, you can create one (for example, main.py) and run:
python main.py
Alternatively, you can develop a main loop or a graphical interface once you have the core logic. For now, focus on building out the Tetris features with TDD. Once you are ready, you can integrate all parts into a playable game.
Below are the initial tests you will encounter:
-
test_board_initialization
Tests that a Board is created with rows, columns, and a 2D grid of the correct size. -
test_board_is_cell_empty
Ensures that a newly initialized Board reports its cells as empty. -
test_board_place_block
Checks that placing a block changes a cell from empty to occupied. -
test_piece_creation
Verifies that creating a Piece with type I has four segments. -
test_piece_rotation
Confirms that rotating a piece changes its shape.
These tests will fail at first. You will fix them by writing just enough code to pass each test, then move on to the next.
After you have passed the initial tests, you can continue with your TDD approach to implement additional features. Remember, always write your tests first for each feature, let them fail, and then write the actual implementation. Some suggestions include:
- Implement piece movement (left, right, down)
- Complete piece rotation logic (including boundary checks)
- Detect and clear full lines, shifting everything above down
- Track scoring and level progression
- Implement a game over state when new pieces cannot spawn
- (Optional) Create a graphical or text-based interface for gameplay
The key is to expand your Tetris logic step by step, always guided by failing tests that you then make pass.
- Review the failing test to see which assertion fails.
- Open the corresponding source file (for example, board.py or pieces.py) and the file containing the pertinent tests.
- Begin implementing the required class or method by adding a docstring or partial signature. You can reference to a given file and a given function (test) to define that you want to make this piece pass.
- Let Copilot suggest code; accept or refine the suggestions.
- Re-run the tests to confirm the fix.
- Continue until all tests pass.
Example flow:
- You see test_board_initialization fail because Board is missing rows, columns, or grid.
- In board.py, implement the Board class with the required attributes.
- Re-run tests until the failure is resolved.
- Move to the next test.
This project uses GitHub Actions for continuous integration. The pipeline:
- Triggers on pushes to
main
and pull requests - Runs on Ubuntu latest
- Sets up Python 3.13
- Installs project dependencies
- Runs all tests in the
tests/
directory
To see the pipeline status, click the badge above or check the Actions tab in the repository.
- Python 3.13
- pip package manager
- pytest
python -m pip install -r requirements.txt
pytest tests/