Java Tetris轮换

我知道这已被问了很多,但我想知道如何旋转俄罗斯方块片? 我已经做了一个漫长而糟糕的解决方案(约170行代码),但应该有更简单的方法。

我的俄罗斯方块由4个块组成,这些块都知道它们在矩阵中的位置(行和列)。 矩阵本身是字符型的,因此4个字块都是字母。 它看起来像这样:

...... ..T... .TTT.. ...... 

我试图通过计算中间行和列并将其用作原点来模拟我的矩阵作为坐标系,然后尝试应用我发现的这个简单算法:90度旋转(x,y)=( – y,x)

它似乎只有在我的作品位于矩阵的中心时才有效。 我不知道我应该做什么,我一整天都在想这个。 这是我的方法:

 public void rotatePiece(ArrayList random) { int distance = 0; // how far is the origo for (int i=0; i < 4; ++i) board[random.get(i).getRow()][random.get(i).getColumn()] = '.'; // erases the current location of the piece for (int i=0; i < 4; ++i) { distance = Math.abs(random.get(i).getColumn()-middleColumn); if (random.get(i).getColumn() < middleColumn) random.get(i).setColumn(random.get(i).getColumn()+(distance*2)); // "throws" the location of the block to the other side of the origo else random.get(i).setColumn(random.get(i).getColumn()-(distance*2)); int help = random.get(i).getColumn(); random.get(i).setColumn(random.get(i).getRow()); // (x, y) = (-y, x) random.get(i).setRow(help); } for (int i=0; i < 4; ++i) board[random.get(i).getRow()][random.get(i).getColumn()] = random.get(0).getStyle(); // saves the new location of the piece in the matrix 

我建议为每个块组定义四种状态。

 enum ROTATION { UP, DOWN, LEFT, RIGHT; ROTATION rotateLeft() { switch(this) { case UP: return LEFT; case LEFT: return DOWN; case DOWN: return RIGHT; case RIGHT: return UP; } return null; // wont happen; } ROTATION rotateRight() { ROTATION r = this; // wow I'm lazy, but I've actually seen this in production code! return r.rotateLeft().rotateLeft().rotateLeft(); } } abstract class Brick { Point centerPos; ROTATION rot; abstract List pointsOccupied(); } class TBrick extends Brick { List pointsOccupied() { int x = centerPos.x(); int y = centerPos.y(); List points = new LinkedList(); switch(rot) { case UP: points.add(new Point(x-1,y); points.add(new Point(x,y); points.add(new Point(x+1,y); points.add(new Point(x, y+1); break; case Down: points.add(new Point(x-1,y); points.add(new Point(x,y); points.add(new Point(x+1,y); points.add(new Point(x, y-1); break; // finish the cases } } } 

您可以使用旋转矩阵 。

您需要适当地设置旋转的原点,这可能意味着翻译片段相对于运动场的位置(例如,原点位于中心),应用旋转矩阵然后将其平移在比赛场地坐标上的正确位置。

最简单,计算速度最快的方法是使用预计算它们。

这意味着俄罗斯方块将看起来像

 class TetrisBlock { String position[4]; int curPos = 0; void rotateLeft() { curPos++; if (curPos > 3) curPos = 0; } .... } 

然后你可以定义类似的东西

 class TetrisTBlock extends TetrisBlock { ... // in constructor position={"....\nT.\nTTT.\n....", ".T..\nTT..\nT.\n.....", // I guess you get the idea 

您可以为每种类型的块执行此操作,然后您还可以添加成员以在板上添加/删除它们。

如果你优化你会离开角色….

我认为最好的方法是硬编码。 考虑到每个图形都不同,每个图形的旋转相位也不同。 并且对于每个旋转阶段 – 确定您需要释放网格的哪些部分(避免碰撞)。 对于视觉表示,请检查此项