Tetris Piece Rotation Algorithm

前端 未结 15 854
傲寒
傲寒 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:31

    This is how I did it recently in a jQuery/CSS based tetris game.

    Work out the centre of the block (to be used as a pivot point), i.e. the centre of the block shape. Call that (px, py).

    Each brick that makes up the block shape will rotate around that point. For each brick, you can apply the following calculation...

    Where each brick's width and height is q, the brick's current location (of the upper left corner) is (x1, y1) and the new brick location is (x2, y2):

    x2 = (y1 + px - py)
    
    y2 = (px + py - x1 - q)
    

    To rotate the opposite direction:

    x2 = (px + py - y1 - q)
    
    y2 = (x1 + py - px)
    

    This calculation is based on a 2D affine matrix transformation. If you are interested in how I got to this let me know.

提交回复
热议问题