A Python implementation of the classic Pong game featuring a custom-built Neural Network built from scratch (no ML libraries like TensorFlow or PyTorch).
The AI learns to play in real-time using Supervised Imitation Learning, training itself to mimic a perfect heuristic algorithm.
- Custom Neural Network: A manual implementation of a Multi-Layer Perceptron (MLP) using only NumPy.
- Real-time Training: Toggle between "Training Mode" (accelerated speed) and "Player Mode" (human vs AI) instantly.
- Visualization: See the AI improve as it iterates through thousands of game loops in seconds.
- Clean Architecture: Separates game logic, rendering, and the neural network class.
The AI uses a Feedforward Neural Network (Perceptron) with the following topology:
- Input Layer (3 Neurons): Normalized values for
Opponent Y,Ball X, andBall Y. - Hidden Layer (5 Neurons): Uses the Sigmoid activation function.
- Output Layer (1 Neuron): Determines direction (Value > 0.5 = Down, < 0.5 = Up).
Unlike Reinforcement Learning (which learns from rewards), this agent uses Supervised Learning.
- The "Teacher": A simple algorithm calculates the perfect move for every frame (aligning the paddle center with the ball).
- Backpropagation: The network compares its own prediction against the "Teacher's" move.
- Optimization: It calculates the error and adjusts weights using Gradient Descent.
You need Python installed along with pygame and numpy.
pip install pygame numpy
### ๐ฎ Controls & Game Modes
The game has two distinct modes. You can toggle between them at any time.
| Key | Function |
| :--- | :--- |
| **SPACE BAR** | **Toggle Modes** (Switch between Training and Playing) |
| **UP Arrow** | Move Player Up (Player Mode only) |
| **DOWN Arrow** | Move Player Down (Player Mode only) |