【发布时间】:2014-06-04 19:15:41
【问题描述】:
我正在开发一个从图像加载关卡的平台游戏。它循环遍历图像的所有像素并检查 RGB 值是否与某个对象匹配,例如硬币的黄色,墙壁的白色。
一旦玩家死亡,关卡就会重新加载,但是当场景中还有炮塔时,游戏的帧率会下降。
循环遍历所有像素的方法。
// Loads the requested level based on a image
public void loadLevel(BufferedImage Image)
{
clearLevel();
int imageWidth = Image.getWidth();
int imageHeight = Image.getHeight();
currentLevel = Image;
for(int Par1 = 0; Par1 < imageHeight; Par1++)
{
for(int Par2 = 0; Par2 < imageWidth; Par2++)
{
int currentPixel = Image.getRGB(Par1, Par2);
int redValue = (currentPixel >> 16) & 0xff;
int greenValue = (currentPixel >> 8) & 0xff;
int blueValue = (currentPixel) & 0xff;
if(redValue == 255 && greenValue == 255 && blueValue == 255)
{
addObject(new Block((Par1 * 32), (Par2 * 32), 0, 0, 0, Identifier.Block));
}
if(redValue == 255 && greenValue == 0 && blueValue == 0)
{
addObject(new Block((Par1 * 32), (Par2 * 32), 1, 0, 0, Identifier.Lava));
}
if(redValue == 255 && greenValue == 0 && blueValue == 255)
{
addObject(new Block((Par1 * 32), (Par2 * 32), 5, 0, 8, Identifier.Platform));
}
if(redValue == 0 && greenValue == 255 && blueValue == 255)
{
addObject(new Block((Par1 * 32), (Par2 * 32), 4, 1, 8, Identifier.Platform));
}
if(redValue == 255 && greenValue == 255 && blueValue == 0)
{
addObject(new Block((Par1 * 32), (Par2 * 32), 2, 0, 0, Identifier.Coin));
}
if(redValue == 0 && greenValue == 255 && blueValue == 0)
{
addObject(new Block((Par1 * 32), (Par2 * 32), 3, 0, 0, Identifier.Flag));
}
if(redValue == 0 && greenValue == 0 && blueValue == 255)
{
addObject(new Player((Par1 * 32), (Par2 * 32), this, Identifier.Player));
}
if(redValue == 255 && greenValue == 175 && blueValue == 0)
{
addObject(new Turret((Par1 * 32), (Par2 * 32), this, Identifier.Turret));
}
}
}
Main.currentState = Main.State.Game;
}
// Reload level
public void reloadLevel()
{
loadLevel(currentLevel);
}
游戏循环
// 在线程启动时运行,同时启动游戏循环并创建单独的滴答声和每秒帧数行
public void run()
{
try
{
preInitialisation();
initialisation();
LogHandler.log("Initialisation complete.");
}
catch(FileNotFoundException Error)
{
Error.printStackTrace();
}
catch(FontFormatException Error)
{
Error.printStackTrace();
}
catch(IOException Error)
{
Error.printStackTrace();
}
requestFocus();
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double nanoSeconds = 1000000000 / amountOfTicks;
double deltaValue = 0;
long currentTime;
long loopTimer = System.currentTimeMillis();
while(isRunning)
{
currentTime = System.nanoTime();
deltaValue += (currentTime - lastTime) / nanoSeconds;
lastTime = currentTime;
while(deltaValue >= 1)
{
tick();
deltaValue--;
temporaryTicks++;
}
render();
temporaryFrames++;
if(System.currentTimeMillis() - loopTimer > 1000)
{
loopTimer += 1000;
absoluteFrames = temporaryFrames;
absoluteTicks = temporaryTicks;
temporaryTicks = 0;
temporaryFrames = 0;
}
}
}
【问题讨论】:
-
这里提供必要的代码,请勿链接外部资源。
-
好的,谢谢你的建议,编辑后:)
-
完全不相关,但从长远来看可能会有所帮助:看看 maven(或类似的东西)来标准化你的项目,让任何人都更容易下载、构建和避免提交你的 3rd 方库
-
会做的,谢谢你的建议。
-
感谢编辑。您能否再解释一下“帧速率下降”的含义?你的动画速度变慢了吗?一段时间后会好转吗?
标签: java image frame-rate