Snake environment for OpenAI Gym, written in Python 3.
Run it like a Gym environment:
from snake.env import SnakeEnv
env = SnakeEnv()
for episode in range(10):
observation = env.reset()
while True:
env.render()
action = env.action_space.sample()
observation, reward, done, info = env.step(action)
if done:
break
Play with it yourself!
env = SnakeEnv()
env.interactive()
Initialization parameters
- state_size: the game is composed of nxn blocks, and size specifies n.
- snake_size: size of the snake
- reward_eat: reward for eating a dot/fruit.
- reward_win: reward for winning (max score).
- reward_lose: reward for losing.
- reward_time: time discount reward
- fps: frames per second (for rendering).
- unit: specifies how big each block is (for rendering).
Default: state_size=20, snake_size=3, reward_eat=1, reward_win=10, reward_lose=-1, reward_time=-0.01, fps=10, unit=10
By default the snake starts at the midpoint and goes upwards.
0: Do nothing
1: Up
2: Down
3: Left
4: Right
Returns an nxn numpy array.
- Empty blocks are represented by 0's
- Snake body is represented by 1's
- The reward is represented by 2.
env.step also returns a dictionary named 'info'.
If the game has ended, (i.e. done == True), then you can inspect info
if done: print(info['cause of death'])
To examine the cause of death. It takes on one of two string values: 'out of bounds' or 'self collision'.
PyGame Learning Environment also has a snake environment with other games as well.
YuriyGuts's snake for AI has examples and GPU support.