Tetris Piece Rotation Algorithm

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

    There is a limited amount of shapes, so I would use a fixed table and no calculation. That saves time.

    But there are rotation algorithms.

    Chose a centerpoint and rotate pi/2.

    If a block of a piece starts at (1,2) it moves clockwise to (2,-1) and (-1,-2) and (-1, 2). Apply this for each block and the piece is rotated.

    Each x is the previous y and each y - the previous x. Which gives the following matrix:

    [  0   1 ]
    [ -1   0 ]
    

    For counterclockwise rotation, use:

    [  0  -1 ]
    [  1   0 ]
    

提交回复
热议问题