gpt4 book ai didi

Java 俄罗斯方 block - 旋转

转载 作者:行者123 更新时间:2023-12-01 18:51:08 24 4
gpt4 key购买 nike

我正在用 Java 制作俄罗斯方 block ,并正在致力于旋转一 block 。

首先,我只是旋转条形部件。

我觉得我现在的做法不仅有问题,而且完全是矫枉过正。但我不知道还能怎么做。

首先,我有一个键监听器,它将 int[]rotatedCoords 设置为 calcRotation("right")...如果向左旋转,则 rotationsCounter+= 1; 将被递减。

    if (keycode == KeyEvent.VK_D) {

int[] rotatedCoords = calcRotation("right");
rotationsCounter+=1;
clearCurrPosition();
rotate(rotatedCoords);
System.out.println(rotationsCounter);
if (rotationsCounter == 4) {
rotationsCounter = 0;
}
System.out.println(rotationsCounter);
}

calcRotation(String right or left) 获取 Piece 中所有 4 个 Tiles 的当前坐标,并将它们发送到 int[] getRotation(String shape, String Direction, int[] current X coords, int[] current Y 坐标)

public int[] calcRotation(String direction) {   

for (int i = 0; i < tile.length; i++) {
currPositionX[i] = tile[i].getX();
currPositionY[i] = tile[i].getY();
System.out.println(currPositionX[i] + ", " + currPositionY[i]);
}
return getRotation("Bar", direction, currPositionX, currPositionY);
}

然后 getRotation[] 根据选择的旋转方向(右或左)、它的形状以及它所在的旋转计数器(0 度、90 度、180 或 270...)设置新坐标

if (direction == "right") {
if (shape == "Bar") {
if (rotationsCounter == 0) {
currXs[0] += 1;
currYs[0] += -1;

currXs[1] += 0;
currYs[1] += 0;

currXs[2] += -1;
currYs[2] += 1;

currXs[3] += -2;
currYs[3] += 2;

rightRotate1 = new int[] {currXs[0], currYs[0], currXs[1], currYs[1], currXs[2], currYs[2], currXs[3], currYs[3]};
}
if (rotationsCounter == 1) {
... etc

然后坐标(pieceRotations)将被适当设置:

        //handle on left rotations
if (direction == "right") {
if (shape == "Bar") {
if (rotationsCounter == 0) {
pieceRotations = rightRotate1;
}
if (rotationsCounter == 1) {
pieceRotations = rightRotate2;
}
if (rotationsCounter == 2) {
pieceRotations = rightRotate3;
}
if (rotationsCounter == 3) {
pieceRotations = rightRotate0;
}
}
}
if (direction == "left") {
if (shape == "Bar") {
if (rotationsCounter == 0) {
pieceRotations = rightRotate3;
}
if (rotationsCounter == 1) {
pieceRotations = rightRotate0;
}
if (rotationsCounter == 2) {
pieceRotations = rightRotate1;
}
if (rotationsCounter == 3) {
pieceRotations = rightRotate2;
}
}
}
return pieceRotations;
}

最后,将使用正确的坐标调用 rotate(rotatedCoords) 来旋转所有图 block ...

public void rotate(int[] rotatedCoordinates) {
int counterX = 0, counterY = 1;
if (movePieceValid()) {
for (int i = 0; i < tile.length; i++) {
tile[i].setLocation(rotatedCoordinates[counterX], rotatedCoordinates[counterY]);
counterX+=2;
counterY+=2;
}
} else {
for (int i = 0; i < tile.length; i++) {
tile[i].setLocation(currPositionX[i], currPositionY[i]);
}
}
}
<小时/>

因此,我目前根据每个形状的当前位置计算左或右的新坐标的方法显然是多余的。有没有我可以遵循的一般指南来大大简化这一过程?我想不出另一种方法来获取每个形状的位置?

最佳答案

只有这么多件。尝试这样:

public abstract class Piece {
public abstract void rotate(Direction dir);
}

public class BarPiece extends Piece {
public void rotate(Direction dir) {
// implement
}
}

public class TPiece extends Piece {
// ...
}

public class LeftSPiece extends Piece {
// ...
}

对于特殊来说似乎有点脏,但是一直做数学会很慢,而且因为可能的部分只有这么多......

关于Java 俄罗斯方 block - 旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15884051/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com