Modern tetris python library, implemented in C++ for speed. Requires SDL2 to render, if you dont want rendering choose the tetris_c_nore version
Project description
Python 2 Player Tetris Library implemented in C++
Original title: Tetris with instant soft drop delay(branch python library)
Following tetris guidelines, implemented in C++ for speed, actions are taken simultaneously, players turn-based with matching keys instead piece, can be used to train AI/whatever. with instant softdrop after specified time and short softdrop like DAS (not implemented in python lib yet)
Separated from my tetris/pylibtest
Usage Brief view
import tetris
x=tetris.Container() # game object
y=x.copy() # deep copy if needed
x.reset() # reset state
x.seed_reset(n) # reset with rng seed
state=x.get_state() # shape (500,) numpy array, 1D so easier to use mp shared memory, more info below
shapes=x.get_shapes() # (7,4,4,4) 7 pieces 4 rotations and 4x4 shape
x.step(ac1,ac2)
Render
Renderer(mode,size)
or
Renderer().create_window(mode,size)
mode 1: pixel size=size
mode 2: screen width=size
mode 3: screen height=size
r=tetris.Renderer(1,30)
r.render(x)
# close window
r.close()
Manually Install dependency:
If you don't want to render/install sdl2 use my tetris_c_nore library instead
Modify setup.py for include and lib directories of SDL2 if needed
Windows:
Visual Studio: vcpkg install sdl:x64-windows
MinGW: Link
Mac/Linux:
On Mac install brew(package manager for mac)
Install sdl2(sometimes libsdl2-dev) with package manager
In Depth Info
Game Step
action is scalar int
0:hold
1:hard drop
2:cw
3:ccw
4:left tap
5:right tap
6:left das
7:right das
8:soft drop
9:180
States
p1_state=state[0:232]
p2_state=state[232:464]
sum attack state[464]
, negative is to p1
individual attacks state[465:]
Reward
state[0] is reward
127 is lose, 126 is win, otherwise
if harddrop (action==1)
if lines_cleared
rew=10 * (attack+ b2b *2)+2*lines
rew+=b2b_not_broke
else rew=wallkick
Python for extracting
lines_cleared = (reward%10)//2
atk = reward//10 # b2b rewards 2 atk but is 1 in-game
wallkick=reward%5 if action!=1 else 0
b2b_not_broken=reward%5 if action==1 else 0
game doesn't auto reset on game over, check those signals and call reset on your own
States
i piece goes to -2 so the value is +2 by default, if you want to draw current piece subtract that back
x = state[1]
Board is 30 high, bottom 21 is returned, y spawns at 9 normally but 8 if occupied, otherwise game over.
Value returned to python starts at 8 so pieces spawn at 1 normally, could be negative if you navigate the piece off screen
y = state[2]
Distance to bottom
softdropdist = state[3]
Default is 10 actions will force harddrop
action_count = state[4]
Garbage cleared from last action, use for reward etc
garbage = state[5]
hold_used = state[6]
rotation = state[7]
active = state[8]
Draw active piece on current location(you have to check x negative yourself):
board[x-2,y]=shape[active][rotation]
Held can be -1 meaning not used
held_piece = state[9]
next_pieces = state[10:15]
Cheat data(hidden queue)
hidden_queue=state[15:22]
Less cheating because you can manually count the bag:
hidden_queue=sort(state[15:22])
Board state[22:232].reshape(21,10)
Feedback
Any ideas for improvements are welcome.
State is mostly boolean/binary so it's possible I can optimise the code further to increase speed as use cases probably will be AI training that needs millions of iterations.
if you want to read/change the code, tetrisboard is main logic including clear rewards, and env.cpp is the python wrapper+rendering+returning state
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
Built Distributions
Hashes for tetris_c-0.1-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd0ca72bf50aeb7deb452c4ddfbd5883275dc62a2a034d91cb8ff70273181461 |
|
MD5 | d7a7afdbb7781a56b7ab197b8d722d2b |
|
BLAKE2b-256 | fa9dbd7e24b004a3af95fd01aaa17846c5c5762a58111d6db57630a263e70b30 |
Hashes for tetris_c-0.1-cp312-cp312-macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e9274513c84e0416d68bd5ab7dd667b068b25dbeeb933d77652f024952bbc852 |
|
MD5 | 57045e1fdf982a84f17427e75c874662 |
|
BLAKE2b-256 | 10868d49884c2e598f335d74afe1f399b39137cef9a77818175f132cd19a7d9f |
Hashes for tetris_c-0.1-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 04145930f1abd52b85f609572cd3acd06f840ecbdaef88b04e528a458aef8b20 |
|
MD5 | 7da60974d1e8c17b94b25638486a463e |
|
BLAKE2b-256 | 7bcd3a278ba7f4a21e5594f3de0192476f29f97b7ea25108622ed49191764a1d |
Hashes for tetris_c-0.1-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f5090a306ff2c545fb9b9be20f05e17dc581805af30f5530daaa999cc01d0175 |
|
MD5 | 9cf52ddec126ee4b72782e5c4208e619 |
|
BLAKE2b-256 | bfce2505f35c7ddfb929537dc2aeb07e66f884f336d66f5f1a0af37c19425869 |