【发布时间】:2013-06-30 15:38:57
【问题描述】:
我正在使用这些游戏的常用物理规则制作一个类似落沙的游戏。
它适用于我从文件加载关卡这一事实。
因此,要检查一个地方是否为空,它会检查已加载级别上的RGB 值。
但由于某种原因,它返回几乎每个单元格(除了相同的几个单元格)都被某些东西占用,即使它不是。
物理是通过最初加载一个文件,然后修改它来处理的。
文件类型为PNG格式,BufferedImage类型为TYPE_INT_ARGB
这是主类中的所有物理处理代码。
public static void updateParticles()
{
for(int i=0;i<100;i++)
{
for(int j=0;j<75;j++)
{
int rgb = levels.getRGB(i,j);
int sx = getMappedCoordX(levels.getRGB(i,j));
int sy = getMappedCoordY(levels.getRGB(i,j));
try{
if(materialRGBmap[(i*32)+j]==solRGB)
{
int[] coords = physicsRules.getSolidMovableStatic(getFree(i,j+1),getFree(i-1,j),getFree(i+1,j),getFree(i-1,j+1),getFree(i+1,j+1),i*8,j*8);
levels.setRGB(i,j,map_null.getRGB(0,0));
levels.setRGB(coords[0]/8,coords[1]/8,rgb);
}
if(materialRGBmap[(i*32)+j]==liqRGB)
{
int[] coords = physicsRules.getLiquidMovableStatic(getFree(i,j+1),getFree(i-1,j),getFree(i+1,j),i*8,j*8);
levels.setRGB(i,j,map_null.getRGB(0,0));
levels.setRGB(coords[0]/8,coords[1]/8,rgb);
}
if(materialRGBmap[(i*32)+j]==gasRGB)
{
int[] coords = physicsRules.getGasMovableStatic(getFree(i,j+1),getFree(i-1,j),getFree(i+1,j),getFree(i+1,j),i*8,j*8);
levels.setRGB(i,j,map_null.getRGB(0,0));
levels.setRGB(coords[0]/8,coords[1]/8,rgb);
}
}catch(Exception e){
System.out.println("lolcat error");
}
}
}
}
这是PhysicsRules 中的代码。 (如上图变量physicsRules)
public int[] getLiquidMovable(boolean onGround, boolean leftFree, boolean rightFree, int x, int y)
{
return onGround?(rand.nextInt(2)==0 && leftFree?new int[]{x-8, y}:(rightFree?new int[]{x+8,y}:new int[]{x,y})):new int[]{x,y+8};
}
public static int[] getLiquidMovableStatic(boolean onGround, boolean leftFree, boolean rightFree, int x, int y)
{
return onGround?(Srand.nextInt(2)==0 && leftFree?new int[]{x-8, y}:(rightFree?new int[]{x+8,y}:new int[]{x,y})):new int[]{x,y+8};
}
public int[] getSolidMovable(boolean onGround, boolean leftFree, boolean rightFree, boolean leftUFree, boolean rightUFree, int x, int y)
{
return onGround?(rand.nextInt(2)==0 && leftFree && leftUFree?new int[]{x-8, y}:(rightFree && rightUFree?new int[]{x+8,y}:new int[]{x,y})):new int[]{x,y+8};
}
public int[] getSolidMovableStatic(boolean onGround, boolean leftFree, boolean rightFree, boolean leftUFree, boolean rightUFree, int x, int y)
{
return onGround?(Srand.nextInt(2)==0 && leftFree && leftUFree?new int[]{x-8, y}:(rightFree && rightUFree?new int[]{x+8,y}:new int[]{x,y})):new int[]{x,y+8};
}
public int[] getGasMovable(boolean leftFree, boolean rightFree, boolean upFree, boolean downFree, int x, int y)
{
int dir = rand.nextInt(4);
return dir==0 && upFree?new int[]{x,y-8}:(dir==1 && leftFree?new int[]{x+8,y}:(dir==2 && downFree?new int[]{x,y+8}:(dir==3 && rightFree?new int[]{x-8,y}:new int[]{x,y})));
}
public static int[] getGasMovableStatic(boolean leftFree, boolean rightFree, boolean upFree, boolean downFree, int x, int y)
{
int dir = Srand.nextInt(4);
return dir==0 && upFree?new int[]{x,y-8}:(dir==1 && leftFree?new int[]{x+8,y}:(dir==2 && downFree?new int[]{x,y+8}:(dir==3 && rightFree?new int[]{x-8,y}:new int[]{x,y})));
}
【问题讨论】:
-
请在您的问题中缩进代码。目前很难阅读。 (老实说,我鼓励你接受一般的空白......)
-
您的标题指的是“物理”代码,但您的文字清楚地表明该问题与模拟物理无关,与文件访问或初始化内存有关代表。
-
该代码用于通过最初加载文件然后对其进行编辑来处理物理。
-
@BottleFact:但是您的编码问题与物理或模拟无关,因此您标题的这一部分是无关紧要且不必要的。只是一个帮助你的建议,因为一个更清晰的问题通常会得到更好更快的答案。此外,您将希望摆脱代码中的幻数。还可以考虑使用一点数学来防止此问题发生。
-
1) 为了尽快获得更好的帮助,请发帖 SSCCE。 2) 对代码块使用一致且符合逻辑的缩进。代码的缩进是为了帮助人们理解程序流程。
标签: java physics rgb bufferedimage