有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使不同PC之间的速度(游戏帧率)相同?

在我们学校,在实施我们从计算机科学课上学到的不同概念时,通常会将游戏作为课堂项目。现在我们在我们的机器上开发了我们的游戏,一切似乎都很好,游戏速度正常等等。现在,当我们尝试在学校的计算机上测试我们的游戏时,或者当我们的教授在他自己的计算机上测试我们的游戏时,假设他的计算机与我们开发游戏的单元相比功能强大得多,游戏的速度会发生巨大的变化。。。在大多数情况下,游戏动画的速度会比预期的要快。所以我的问题是,如何在游戏应用程序中防止这种问题?是的,我们使用Java。在我们构建的大多数应用程序中,我们通常使用被动渲染作为渲染技术。提前通知tnx


共 (1) 个答案

  1. # 1 楼答案

    你不应该依赖于游戏逻辑的渲染速度。相反,记录从游戏中最后一个逻辑步骤到当前一个逻辑步骤所花费的时间。然后,如果花费的时间超过了一定数量,你就执行一个游戏步骤(在极少数情况下,计算机速度太慢,以至于应该执行两个步骤,你可能想想出一个聪明的解决方案来确保游戏不会落后)

    通过这种方式,游戏逻辑与渲染逻辑是分开的,你不必担心改变游戏的速度取决于垂直同步是打开还是关闭,或者电脑比你的慢还是快

    一些伪代码:

    // now() would be whatever function you use to get the current time (in
    // microseconds or milliseconds).
    int lastStep = now();
    // This would be your main loop.
    while (true) {
        int curTime = now();
    
        // Calculate the time spent since last step.
        int timeSinceLast = curTime - lastStep;
    
        // Skip logic if no game step is to occur.
        if (timeSinceLast < TIME_PER_STEP) continue;
    
        // We can't assume that the loop always hits the exact moment when the step
        // should occur. Most likely, it has spent slightly more time, and here we
        // correct that so that the game doesn't shift out of sync.
        // NOTE: You may want to make sure that + is the correct operator here.
        //       I tend to get it wrong when writing from the top of my head :)
        lastStep = curTime + timeSinceLast % TIME_PER_STEP;
    
        // Move your game forward one step.
    
    }