0

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

我有时觉得游戏移动的速度没有那么快,有时会卡住 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);
        }
    }
}

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

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
4

1 回答 1

2

你的游戏循环让我很困惑——但这并不难;)

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

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();
        }
    }

}
于 2014-02-20T23:37:49.530 回答