我有这行代码:
int[][] coordinates = new int[][]{{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}};
if (yg.radius == 1) {
coordinates = new int[][]{{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}};
} else if (yg.radius == 2) {
coordinates = new int[][]{{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, 1}, {-1, -1}, {1, -1}};
} else if (yg.radius == 3) {
coordinates = new int[][]{{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, {2, 0}, {0, 2}, {-2, 0}, {0, -2}};
} else if (yg.radius == 4) {
coordinates = new int[][]{{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, {2, 0}, {0, 2}, {-2, 0}, {0, -2}, {2, 2}, {-2, 2}, {-2, -2}, {2, -2}};
} else if (yg.radius == 5) {
coordinates = new int[][]{{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, {2, 0}, {0, 2}, {-2, 0}, {0, -2}, {2, 2}, {-2, 2}, {-2, -2}, {2, -2}, {3, 0}, {0, 3}, {-3, 0}, {0, -3}};
} else if (yg.radius == 6) {
coordinates = new int[][]{{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, {2, 0}, {0, 2}, {-2, 0}, {0, -2}, {2, 2}, {-2, 2}, {-2, -2}, {2, -2}, {3, 0}, {0, 3}, {-3, 0}, {0, -3}, {3, 3}, {-3, 3}, {-3, -3}, {3, -3}};
} else if (yg.radius == 7) {
coordinates = new int[][]{{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, {2, 0}, {0, 2}, {-2, 0}, {0, -2}, {2, 2}, {-2, 2}, {-2, -2}, {2, -2}, {3, 0}, {0, 3}, {-3, 0}, {0, -3}, {3, 3}, {-3, 3}, {-3, -3}, {3, -3}, {4, 0}, {0, 4}, {-4, 0}, {0, -4}};
} else if (yg.radius == 8) {
coordinates = new int[][]{{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, {2, 0}, {0, 2}, {-2, 0}, {0, -2}, {2, 2}, {-2, 2}, {-2, -2}, {2, -2}, {3, 0}, {0, 3}, {-3, 0}, {0, -3}, {3, 3}, {-3, 3}, {-3, -3}, {3, -3}, {4, 0}, {0, 4}, {-4, 0}, {0, -4}, {4, 4}, {-4, 4}, {-4, -4}, {4, -4}};
}
for (int[] coordinate : coordinates) {
Hit(coordinate[0], coordinate[1], yg.damage);
}
(是的,这都是我手写的😭) 现在,问题是。我怎样才能自动做到这一点?我的意思是我不想将这一切手动写入 yg.radius 20。
我期望有一个函数将半径作为参数,并返回坐标数组。 也许我甚至做错了,我只需要一个数组,为我提供圆形效果中的像素。 预先感谢!
下面可以帮助你,你可以尝试一下吗?
int[][] getCoordinatesInCircle(int radius) {
int[][] coordinates = new int[4 * radius * radius + 1][]; // Allocate space for all coordinates
int index = 0;
for (int x = -radius; x <= radius; x++) {
for (int y = -radius; y <= radius; y++) {
if (x * x + y * y <= radius * radius) { // Check if within circle
coordinates[index++] = new int[]{x, y};
}
}
}
return coordinates; }
#happycoding #ng