我正在开发一款平台游戏,可以从图像中加载等级。它循环通过图像的所有像素并检查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);
}
游戏圈
//线程启动时运行,也启动游戏循环并创建单独的tick和每秒帧数
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;
}
}
}
答案 0 :(得分:2)
原来添加了LWJGL库,然后启用了vsync。
Display.setVSyncEnabled(true);
我也更快地将所有链接列表更改为ArrayLists,When to use LinkedList over ArrayList?