Tetris Piece Rotation Algorithm

前端 未结 15 882
傲寒
傲寒 2020-11-30 17:57

What are the best algorithms (and explanations) for representing and rotating the pieces of a tetris game? I always find the piece rotation and representation schemes confu

15条回答
  •  悲哀的现实
    2020-11-30 18:17

    I derived a rotation algorithm from matrix rotations here. To sum it up: If you have a list of coordinates for all cells that make up the block, e.g. [(0, 1), (1, 1), (2, 1), (3, 1)] or [(1, 0), (0, 1), (1, 1), (2, 1)]:

     0123       012
    0....      0.#.
    1####  or  1###
    2....      2...
    3....
    

    you can calculate the new coordinates using

    x_new = y_old
    y_new = 1 - (x_old - (me - 2))
    

    for clockwise rotation and

    x_new = 1 - (y_old - (me - 2))
    y_new = x_old
    

    for counter-clockwise rotation. me is the maximum extent of the block, i.e. 4 for I-blocks, 2 for O-blocks and 3 for all other blocks.

提交回复
热议问题