Giter VIP home page Giter VIP logo

gym-snakegame's Introduction

gym-snakegame

A gymnasium-based RL environment for learning the snake game.

snakegame5x5

You can make a game with a grid size larger than 5x5.

Action Space Observation Space
Discrete(4) (size, size)

Installation

git clone https://github.com/helpingstar/gym-snakegame.git
cd gym-snakegame
pip install -r requirements.txt
pip install -e .

Colab

!git clone https://github.com/helpingstar/gym-snakegame.git
%cd gym-snakegame
!pip install -r requirements.txt
!pip install -e .

Usage

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()

parameter

  • 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

Observation Space : Box(0.0, board_size ** 2 + 1, (board_size, board_size), uint32)

  • 0 : empty
  • 1 ~ board_size ** 2 : snake body
    • 1 : head
    • largest number : tail
  • board_size ** 2 + 1 : target

You can change the dtype or shape by using the wrapper below.

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

Action Space : Discrete(4)

  • 0 : down
  • 1 : right
  • 2 : up
  • 3 : left

gym-snakegame's People

Contributors

helpingstar avatar lvhao54 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.