gpt4 book ai didi

JavaGame 角色在绘图时移动不准确?

转载 作者:行者123 更新时间:2023-12-02 06:10:40 25 4
gpt4 key购买 nike

出于个人实践,我正在用Java重新制作Flappy Bird桌面版,我已经成功地完成了所有支柱生成、屏幕、背景移动,但现在我有一个问题,那就是性能。

我有时感觉游戏移动得不那么快,有时会卡住0.5秒什么的,但事实并非如此,当我移动我的小鸟时,它的移动有点奇怪,看起来移动太多了向前然后向后,观看 MP4 格式的 gif:

http://gyazo.com/d7e94c0b772192e5a5dd1d2b61b8c529

可能是什么原因造成的?这可能是我的游戏循环或者我绘制图形的方式吗?我不使用双缓冲,我只是通过在 jframe 中调用 .repaint 来重新绘制我添加的 JPanel 来进行渲染。

游戏循环:

private void gameLoop() {
new Thread() {
private long last;
private long start;
private int wait;
public void run() {
while(game) {
long now = System.nanoTime();
long length = now - lastLoop;
lastLoop = now;
double delta = length / ((double) Constants.OPTIMAL);
lastFps += length;
fps++;
if (lastFps >= 1000000000) {
System.out.println("FPS: " + fps);
lastFps = 0;
fps = 0;
}
update(delta);
render();
this.sleep
}
}
private void sleep() {
try {
Thread.sleep((this.last - System.nanoTime() + Constants.OPTIMAL) / 1000000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}

我的绘图区域:

public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
int translateAmount = this.game.getLevel().getTranslate();
g.translate(-translateAmount, 0);
this.background.render(g2d);
this.game.getLevel().renderLevel(g2d);
g.translate(0, 0);
this.game.getBird().render(g2d);

}

我渲染背景的方式是这样的:我一个接一个地添加背景,如果背景在框架之外,我不会渲染它。

public void render(Graphics2D g) {
Iterator<RepeatedBackground> itr = this.repeats.iterator();
while (itr.hasNext()) {
RepeatedBackground b = itr.next();
if (this.game.getLevel().getTranslate() - b.getX() >= 300) {
itr.remove();
continue;
}
if (b.getX() - this.game.getLevel().getTranslate() < Constants.WIDTH) {
b.render(g);
}
}
}

这就是我移动小鸟的方式(我不使用 delta,使用了一些教程):

private void update(double delta) {
if (System.currentTimeMillis() - this.level.getTime() >= Constants.TRANSLATE_SPEED) {

// move background
this.level.updateTranslate();
this.level.setTime();

// move bird
this.getBird().move();
}
}

public void move() {
this.x += 2 / 1.10;
}

什么可能导致鸟类或背景滞后?我的渲染方式或游戏循环有问题吗?

FPS 总是打印这个:

FPS: 1724172
FPS: 1551857
FPS: 1494378
FPS: 1471987
FPS: 1434095
FPS: 1629905

最佳答案

你的游戏循环让我感到困惑 - 但这并不难;)

基本上,据我了解这些事情,它应该像......

while (gamming) {
now = get current time;
update game state
delta = get current time - now
delay = desired delay - delta
wait for delay
}

您似乎没有考虑 renderupdate 方法“可能”执行所需的时间并计算等待时间这些要求...

现在,我不确定您的 delta 值应该是多少......当前秒的时间?所以我把它从我的例子中省略了......

以下示例为我提供了恒定的 25fps,即使 updaterender 方法中存在随机延迟

现在,请原谅我,我通常在几毫秒内完成工作,这对我可怜、脆弱的头脑来说更简单。

import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TestGameLoop {

public static void main(String[] args) {
new TestGameLoop();
}

public TestGameLoop() {
gameLoop();
}

public static class Constants {

public static final double OPTIMAL = 25; // fps...

}

private boolean game = true;
private long lastLoop;
private long lastFps;
private long fps;

private void gameLoop() {
new Thread() {

private long last;
private long start;
private int wait;

public void run() {

// Calculate the optimal/maximum delay time
// This is converted to nanos so it can be
// used to calculate the actual delay...
long millisPerSecond = TimeUnit.MILLISECONDS.convert(1, TimeUnit.SECONDS);
long optimalDelay = Math.round(millisPerSecond / Constants.OPTIMAL);

optimalDelay = TimeUnit.MILLISECONDS.toNanos(optimalDelay);

// Last start of a "second" loop
long loop = System.nanoTime();
// While gaming...
while (game) {
// Start of this cycle...
long now = System.nanoTime();

// Update the state and render the
// current frame...
update();
render();

// How long did that update take??
long timeTaken = System.nanoTime();
long delta = timeTaken - now;

// Subtract the delay from the maximum delay
long delay = optimalDelay - delta;
if (delay > 0) {
try {
// Sleep expects milliseconds...
delay = TimeUnit.NANOSECONDS.toMillis(delay);
Thread.sleep(delay);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}

// Calculate if we've being running for a second yet...
long loopDelay = TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - loop);
// If the loop has been cycling for a second...
if (loopDelay >= 1) {
// Reset the loop time
loop = System.nanoTime();
System.out.println("FPS = " + fps);
fps = 0;
} else {
// Add another frame to the pile...
fps++;
}
}
}
}.start();
}

public void update() {
try {
Thread.sleep(Math.round(Math.random() * 20));
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}

public void render() {
try {
Thread.sleep(Math.round(Math.random() * 20));
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}

}

关于JavaGame 角色在绘图时移动不准确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21921344/

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