俄罗斯方块旋转算法 [英] Tetris Piece Rotation Algorithm
问题描述
表示和旋转俄罗斯方块游戏的各个部分的最佳算法(和解释)是什么?我总是觉得片段旋转和表示方案令人困惑.
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 confusing.
大多数俄罗斯方块游戏似乎在每次旋转时都使用一种天真的重新制作方块数组":
Most tetris games seem to use a naive "remake the array of blocks" at each rotation:
http://www.codeplex.com/Project/ProjectDirectory.aspx?ProjectSearchText=俄罗斯方块
但是,有些使用预先构建的编码数字和位移来表示每个片段:
However, some use pre-built encoded numbers and bit shifting to represent each piece:
http://www.codeplex.com/wintris
有没有一种方法可以使用数学来做到这一点(不确定这是否适用于基于单元的板上)?
Is there a method to do this using mathematics (not sure that would work on a cell based board)?
推荐答案
形状的数量是有限的,所以我会使用一个固定的表格,不计算.这样可以节省时间.
There is a limited amount of shapes, so I would use a fixed table and no calculation. That saves time.
但是有旋转算法.
选择一个中心点并旋转 pi/2.
Chose a centerpoint and rotate pi/2.
如果一块棋子从 (1,2) 开始,它会顺时针移动到 (2,-1) 和 (-1,-2) 和 (-1, 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.
每个 x 是前一个 y,每个 y - 前一个 x.这给出了以下矩阵:
Each x is the previous y and each y - the previous x. Which gives the following matrix:
[ 0 1 ]
[ -1 0 ]
逆时针旋转,使用:
[ 0 -1 ]
[ 1 0 ]
这篇关于俄罗斯方块旋转算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!