gpt4 book ai didi

Java游戏帧率下降

转载 作者:行者123 更新时间:2023-11-30 03:52:36 29 4
gpt4 key购买 nike

我正在开发一个从图像加载关卡的平台游戏。它循环遍历图像的所有像素,并检查 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;
}
}
}

最佳答案

结果添加了 LWJGL 库,然后启用垂直同步就成功了。

Display.setVSyncEnabled(true);

我还将所有 LinkedList 更改为 ArrayList,因为它更快,When to use LinkedList over ArrayList?

关于Java游戏帧率下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24045737/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com