Skip to content

siphesihlegema/Pong_AI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

6 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ“ Pong AI Trainer

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.

๐Ÿ“‹ Features

  • 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.

๐Ÿง  How It Works

The Model

The AI uses a Feedforward Neural Network (Perceptron) with the following topology:

  • Input Layer (3 Neurons): Normalized values for Opponent Y, Ball X, and Ball Y.
  • Hidden Layer (5 Neurons): Uses the Sigmoid activation function.
  • Output Layer (1 Neuron): Determines direction (Value > 0.5 = Down, < 0.5 = Up).

The Learning Process

Unlike Reinforcement Learning (which learns from rewards), this agent uses Supervised Learning.

  1. The "Teacher": A simple algorithm calculates the perfect move for every frame (aligning the paddle center with the ball).
  2. Backpropagation: The network compares its own prediction against the "Teacher's" move.
  3. Optimization: It calculates the error and adjusts weights using Gradient Descent.

๐Ÿš€ Getting Started

Prerequisites

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) |

About

Pong AI: The Machine That Teaches Itself ๐Ÿง  Watch a Neural Network master the classic game of Pong in real-time. By speeding up the game 100x, this AI evolves from a random guesser to an unbeatable pro in mere seconds, all built from scratch using pure math and Python.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages