Snake is a game where the agent must maneuver a line which grows in length each time food is touched by the head of the segment. The line follows the previous paths taken which eventually become obstacles for the agent to avoid.

The food is randomly spawned inside of the valid window while checking it does not make contact with the snake body.

Valid Actions

Up, down, left, and right. It cannot turn back on itself. Eg. if its moving downwards it cannot move up.

Terminal states (game_over)

If the head of the snake comes in contact with any of the walls or its own body (can occur after only 7 segments) the game is over.


It recieves a positive reward, +1, for each red square the head comes in contact with. While getting -1 for each terminal state it reaches.

class, height=64, init_length=3)[source]

width : int

Screen width.

height : int

Screen height, recommended to be same dimension as width.

init_length : int (default: 3)

The starting number of segments the snake has. Do not set below 3 segments. Has issues with hitbox detection with the body for lower values.



  • snake head x position.
  • snake head y position.
  • food x position.
  • food y position.
  • distance from head to each snake segment.

See code for structure.