This is a module I wrote last year to use with my tetris game and ai. I cleaned it up a little and added lots of documentation but was wondering if it's ok for python-forums completed scripts section since it's just a module. The GUI and AI modules still need allot more cleaning up and have no documentation but I thought this could be useful for someone wanting to create their own tetris game or AI.

It was meant to be exactly according to tetris guidelines and I think it is. Has SRS and wall kicks and so allows for things like the T-spin.

You can scroll down to the part that's commented out and says testing to see how it's used.

I left out gravity as I thought that should be implemented by the code that uses this module. Would it be better to have it in here rather? Any comments welcome.

The Random Generator generates the set of all seven tetriminos permuted randomly to form a "bag". Tet's from the bag are then fed to a queue. When the bag runs out another set of all seven tet's are generated and permuted randomly again.

Effect:

- It is impossible to recieve the same piece more than twice in a row. - It's also impossible to recieve a "snake" of 'S' and 'Z' tetriminos longer than 4. - same tet's will be a maximum of 12 tet's apart, meaning the longest you'll have to wait for the next 'I' piece for example, is 12 pieces down the queue.

mino - A single block/cell that is part of a tetrimino form. - BTW all tetriminos are made up of exactly 4 minos.

playing field (or just field) - The 10 x 22 grid in which the tetriminoes are moved and placed. - The visible field should be 20 rows but the actual playing field should be between 22 and 24 rows to allow for a vanish zone (hidden zone).

bounding box (or just box) - One of three different sized grids which contain the form of a tetrimino in a certain orientation. - This simplifies the effect of the way tetriminoes rotate around a point in the SRS system. - Bouding box may fall out of playing field but minos may not.

Super Rotation System (SRS) - Used in most modern tetris games according to tetris guidelines - In SRS tetriminos rotate around a point and incorporate wall kicks. - SRS ultimately allows moves such as the T-spin.

wall kick - essential part of SRS. - "O" doesn't rotate so has no wall kick abilities. - "I" has it's own set of wall kicks - All other tets share a common set of wall kicks. wall_kick_data derived from: http://tetrisconcept.net/wiki/SRS

def __init__(self, width=10, height=22): self.playing_field = [["" for x in range(width)] for y in range(height)] self.field_width = width self.field_height = height self.box = None # box will contain a value from tetriminoes variable. # box grid is seperate from playing field grid. # Use field_state() method to get a single grid # with playing field and box combined.

self.box_coords = None # box_coords will contain (x, y) of the cell at box[0][0]. # Spawn coords for any box is (3, 0).

self.tet_state = None # tet_state describes what is in the bounding box. # eg. ["I", 0] for tetrimino "I" at orientation 0. # tet_state = None when no box is in the field.

I'll try get the GUI to an acceptable state to post also but will probably leave my naive ai implementation out. Shouldn't spoil the fun anyway . I've been thinking for the longest time about how to make an ai that does tspin moves, too long so I gave up. But if someone could achieve that I would bow at their feet. The best one I've seen (or my favourite atleast) plays perfect tetris (performs nothing but "tetrises") but I think even that wouldn't compare.