FinishedOct 2026 · Reinforcement learning
Self-Play Pong: A DQN That Learns by Playing Itself
I wrote Pong from scratch in pygame, then trained a DQN on raw pixels by making it play older copies of itself. In the video it's on both paddles.
The problem
Why build this?
I wanted to see self-play work end to end on something small enough to fully understand. Writing the game myself instead of using Atari meant I controlled the observations, the reward and the opponent.
Approach
How it works
- Built Pong as a Gymnasium env in pygame. Either paddle can be a human, a scripted bot or the trained model, so I can play against whatever I train.
- The agent sees three stacked 84x84 black-and-white frames and picks up, down or stay. It's a small conv net trained with Double DQN, a 200k-transition replay buffer kept on the GPU, and a target network synced every 10k steps.
- One network plays both sides. When it's on the left paddle the frame is mirrored, so it only has to learn one game.
- Its opponent is drawn from a pool of its 5 best past checkpoints. Every 100 episodes the current model is scored against the hard scripted bot and added to the pool. Sides swap every episode.
Outcome
What came out of it
- 5,000 episodes in about 2.6 hours on a Kaggle T4.
- Against the hard scripted bot, the average point margin per eval game went from -4.0 at episode 100 to +4.0 at its best (episode 3,300), and held around +2.9 over the last 1,000 episodes.
- The video is that model playing itself. Human vs AI works too, so you can play it.
Technology
Tools used deliberately
PythonPyTorchDQNpygameGymnasium
Source and evidence
The claims on this page are limited to the project repository, model page, public demo, and resume record.