这是一个很难获得冠军的人。
基本上我有以下代码:
for (int x=0; x<width; x++)
{
for (int y=0; y<height; y++)
{
if (world[x][y] == 2) //Find all Planets ('2') in world
{
size[count] = randInt(1024, 512); //Random Size
angle[count] = randFloat(4, 0); //Random Angle
hue[count] = randInt(360, 0); //Random Hue
count++;
}
}
}
world[][]
是一个整数多维数组,当前保存从 0 到 2 的随机放置值。
0 什么都不是, 1 是一个明星而 2 是一个星球。
randInt
和randFloat
在(max,min)
因此,for循环遍历整个world[][]
数组,如果找到行星(2),则执行以下代码。
for循环中的代码应该存储每个行星的值,以便它们的不同值(即大小,角度,色调)都是唯一的。有了这些值,我后来会在世界上绘制所有行星并在绘制它们时我希望能够根据正在绘制的行星来访问这些值,然后相应地使用这些值来更改行星将如何渲染的参数
我不知道怎么做是将所有这些值存储到一个行星(2)中,这样我就可以让行星永久保留我指定的值。这样做的最佳方式是什么?
如果需要更多代码,请告诉我们。
答案 0 :(得分:2)
您可以创建Planet
课程&amp;一个Star
类以更加面向对象的方式来处理它。
例如,您可以使用Map
ArrayList
个Planet
的{{1}}个:{/ p>
public class Planet {
private int size;
private int angle;
private int hue;
public Planet(int size, int angle, int hue) {
this.size = size;
this.angle = angle;
this.hue = hue;
}
// Getters & setters
}
// I'm not sure whether you are considering Stars as Planets in your
// approach, but this is just an example so that you would be able to use
// polymorphism in your code
public class Star extends Planet {
// methods & properties
}
Map<Integer, List<Planet>> map = new HashMap<>();
map.put(1, new ArrayList<>());
map.put(2, new ArrayList<>());
map.put(3, new ArrayList<>());
for (int x=0; x<width; x++)
{
for (int y=0; y<height; y++)
{
if (world[x][y] == 1) //Find all Stars in world
{
// Taking in consideration the fact that Stars are Planets
map.get(1).add(new Star(/*args*/));
}
else if (world[x][y] == 2) //Find all Planets ('2') in world
{
map.get(2).add(new Planet(
randInt(1024, 512),
randFloat(4, 0),
randInt(360, 0)
));
}
}
}