A simple Tetris engine for OpenAI Gym
Project description
gym-simpletetris
Simple Tetris is a simple Tetris environment built entirely in Python. Several options are provided for modifying the environment and its reward system.
Installation
pip
The preferred installation of gym-simpletetris
is via pip
:
pip install gym-simpletetris
Clone the project
The code is easy to read and modify. If you wish to modify the package to your needs, then follow these instructions:
- Fork the project
- Download it using:
git clone https://github.com/<YOUR-USERNAME>/gym-simpletetris
- Make your changes
- Install it using pip:
cd gym-simpletetris
pip install -e ./
Usage
You can create an environment using gym.make
and supplying the environment id.
import gym
import gym_simpletetris
env = gym.make('SimpleTetris-v0')
obs = env.reset()
episode = 0
while episode < 10:
action = env.action_space.sample()
obs, reward, done, info = env.step(action)
if done:
print(f"Episode {episode + 1} has finished.")
episode += 1
obs = env.reset()
env.close()
Environment
Several options are provided to modify the environment. Arguments can be used at the creation of the environment:
import gym
import gym_simpletetris
env = gym.make('SimpleTetris-v0',
height=20, # Height of Tetris grid
width=10, # Width of Tetris grid
obs_type='ram', # ram | grayscale | rgb
extend_dims=False, # Extend grayscale dimensions
render_mode='rgb_array', # Only supports rgb_array
reward_step=False, # see reward table
penalise_height=False, # see reward table
penalise_height_increase=False, # see reward table
advanced_clears=False, # see reward table
high_scoring=False # see reward table
)
If using an observation type of grayscale or rgb then the environment will be
as an array of size 84 x 84. If using grayscale, then the grid can be returned
as 84 x 84 or extended to 84 x 84 x 1 if entend_dims
is set to True. The rgb
array will always be returned as 84 x 84 x 3.
Note: The environment only supports rendering as rgb_array
at this time but a human
render option is planned in a future update.
Reward Table
Option | Description |
---|---|
reward_step |
Adds a reward of +1 for every time step that does not include a line clear or end of game. |
penalise_height |
Penalises the height of the current Tetris tower at every time step. A negative reward equal to the height of the current tower is added at every time step. |
penalise_height_increase |
Penalises an increase in the height of the Tetris tower. At each time step, if the height increases then a negative reward equal to the increase is given. |
advanced_clears |
Changes the rewards for clearing lines to be similar to the modern Tetris system. The new rewards are 100 for a single line clear, 250 for a double line clear, 750 for a triple line clear and 3000 for a Tetris (quadruple line clear). |
high_scoring |
Changes the reward given for each line clear to 1000. |
Note: penalise_height
and penalise_height_increase
cannot be used together.
If both are enabled then penalise_height will be used.
info
dictionary
The info dictionary returned by each step includes the following keys:
Key | Type | Description |
---|---|---|
time |
int |
The time passed (no. of steps) since the start of the current game |
current_piece |
string |
The letter representing the current piece |
score |
int |
The score of the current game |
lines_cleared |
int |
The number of lines cleared in the current game |
statistics |
dict |
The number of Tetris pieces dispatched by type |
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file gym_simpletetris-0.0.1.tar.gz
.
File metadata
- Download URL: gym_simpletetris-0.0.1.tar.gz
- Upload date:
- Size: 7.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d4ad2abcff15bff69ba505689a1b234b19697e8207c8b06ec7ea902eebca38b2 |
|
MD5 | 0deac53f8437819c0cb7b687df43c3a0 |
|
BLAKE2b-256 | 2daaf56df05bd4dd4696ccf713eb522fd33d955c064b8e1cc67a7734eb10bfcc |