在我们学校,将游戏作为课程项目来实现我们从计算机科学课程中学到的不同概念是很常见的.现在我们在我们的机器上开发了游戏,一切似乎都很好,游戏速度正常等等.现在,当我们尝试在我们学校的计算机上测试我们的游戏时,或者当我们的教授在他自己的计算机上测试我们的游戏时,让我们说他的计算机与我们开发游戏的单位相比非常强大,游戏的速度发生了巨大的变化. ..在大多数情况下,游戏动画的发生速度比预期的要快.所以我的问题是,你如何防止游戏应用程序中出现这种问题?是的,我们使用Java.在我们构建的大多数应用程序中,我们通常使用被动渲染作为渲染技术.tnx提前!
您不应该依赖渲染速度来获得游戏逻辑.相反,跟踪从游戏中的最后一个逻辑步骤到当前步骤所花费的时间.然后,如果花费的时间超过一定数量,你执行一个游戏步骤(在极少数情况下,计算机速度太慢,应该发生两个步骤,你可能想要一个聪明的解决方案,以确保游戏不会不落后.
这样,游戏逻辑与渲染逻辑分开,您不必担心游戏更改速度,具体取决于垂直同步是打开还是关闭,或者计算机是否比您的更慢或更快.
一些伪代码:
// 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.
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2901 次 |
| 最近记录: |