Back to projects

Autonomous Snake Game Agent

Deep Q-Learning trained to play Snake

Deep Q-LearningPygameRLPython

Reinforcement Learning in Action

Training an agent to play Snake is a classic RL problem. I implemented Deep Q-Learning (DQN) with experience replay to stabilize training.

State Representation

The agent perceives its environment through a simplified 11-dimensional state vector covering:

  • Danger directions (straight, right, left)
  • Current moving direction
  • Food relative position

Model Architecture

A Feed-Forward Neural Network using PyTorch linear layers. The reward function penalizes collisions while rewarding eating food. Within a few hundred epochs, the agent transitions from erratic loops to highly strategic, space-filling maneuvers.