A gymnasium-based RL environment for learning the snake game.
snakegame_10_clear.mp4
You can make a game with a grid size larger than 5x5.
Action Space | Observation Space |
---|---|
Discrete(4) | (board_size , board_size ) |
git clone https://github.com/helpingstar/gym-snakegame.git
cd gym-snakegame
pip install -e .
import gym_snakegame
import gymnasium as gym
env = gym.make(
"gym_snakegame/SnakeGame-v0", board_size=10, n_channel=1, n_target=1, render_mode='human'
)
obs, info = env.reset()
for i in range(10000):
action = env.action_space.sample()
obs, reward, terminated, truncated, info = env.step(action)
if terminated or truncated:
obs, info = env.reset()
env.close()
board_size
: The size of a square board. The board has the shape(board_size, board_size)
.n_target
: The number of targets placed on a board.n_channel
: The number of channels of the observation. The observation has the shape(n_channel, board_size, board_size)
.
Observation Space : Box(0.0, board_size ** 2 + 1, (n_channel, board_size, board_size), uint32)
- 1 : Like the rendering format, all information is expressed in one channel.
- 2 : The snake and item channels are divided.
- 0 : snake
- 1 : item
- 4 : Channels are divided in the following order.
- 0 : snake's head
- 1 : snake's body
- 2 : snake's tail
- 3 : item
0
: empty1 ~ board_size ** 2
: snake body1
: head- largest number : tail
board_size ** 2 + 1
: target
Action Space : Discrete(4)
0
: down1
: right2
: up3
: left
When the Agent takes an Invalid Action, the Agent continues in the direction it was previously going.