0

個人的な練習のために、Java を使用してデスクトップ用の Flappy bird を作り直しています。すべての柱の生成、画面、背景の移動を行うことができましたが、パフォーマンスの問題が 1 つあります。

ゲームがそれほど速く動いていないと感じたり、0.5 秒程度動かなくなったりすることもありますが、そうではありません。鳥を動かすと、少し変な動きをし、前に進みすぎているように見えます。後ろに、MP4 形式の gif を見てください。

http://gyazo.com/d7e94c0b772192e5a5dd1d2b61b8c529

何が原因でしょうか?これは私のゲーム ループですか、それともグラフィックの描画方法ですか? 私はダブル バッファリングを使用しません。追加した JPanel を再描画する jframe で .repaint を呼び出すだけでレンダリングします。

ゲームループ:

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

}

背景をレンダリングする方法は次のとおりです。背景を 1 つずつ追加し、背景がフレームの外側にある場合はレンダリングしません。

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
}

renderandupdateメソッドが実行に「かかる」時間を考慮しておらず、これらの要件に基づいて待機時間を計算していないようです...

さて、あなたのdelta値がどうあるべきかわかりません...現在の秒までの時間?? だから私はそれを私の例から除外しました...

update次の例では、メソッドとrenderメソッドにランダムな遅延があっても、一定の 25 fps が得られます。

申し訳ありませんが、私は通常、ミリ秒単位で作業を行っています。

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 に答える