A gymnasium-based RL environment for learning the snake game.
You can make a game with a grid size larger than 5x5.
Action Space | Observation Space |
---|---|
Discrete(4) | (size , size ) |
git clone https://github.com/helpingstar/gym-snakegame.git
cd gym-snakegame
pip install -r requirements.txt
pip install -e .
!git clone https://github.com/helpingstar/gym-snakegame.git
%cd gym-snakegame
!pip install -r requirements.txt
!pip install -e .
import gym_snakegame
import gymnasium as gym
env = gym.make('gym_snakegame/SnakeGame-v0', size=15, n_target=1, render_mode='rgb_array')
# env = gym.wrappers.RecordVideo(env, video_folder='./video_folder', episode_trigger=lambda x: x % 200 == 0)
observation, info = env.reset()
for i in range(100000):
action = env.action_space.sample()
obs, reward, terminated, _, info = env.step(action)
if terminated:
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.
Observation Space : Box(0.0, board_size ** 2 + 1, (board_size, board_size), uint32)
0
: empty1 ~ board_size ** 2
: snake body1
: head- largest number : tail
board_size ** 2 + 1
: target
You can change the dtype
or shape
by using the wrapper below.
gymnasium.experimental.wrappers.ReshapeObservationV0
.gymnasium.experimental.wrappers.DtypeObservationV0
import numpy as np
import gymnasium as gym
from gymnasium.experimental.wrappers import ReshapeObservationV0, DtypeObservationV0
import gym_snakegame
env = gym.make("gym_snakegame/SnakeGame-v0", board_size=5, n_target=1)
# Box(0, 26, (5, 5), uint32)
env = ReshapeObservationV0(env, (1, env.get_wrapper_attr('board_size'), env.get_wrapper_attr('board_size')))
# Box(0, 26, (1, 5, 5), uint32)
env = DtypeObservationV0(env, dtype=np.float32)
# Box(0.0, 26.0, (1, 5, 5), float32)
Action Space : Discrete(4)
0
: down1
: right2
: up3
: left