在我们学校,将游戏作为课堂项目来实现我们从计算机科学课程中学到的不同概念是很常见的。现在我们在我们的机器上开发了我们的游戏,一切似乎都工作正常,游戏速度正常等等。现在,当我们尝试在学校的计算机上测试我们的游戏时,或者当我们的教授在他自己的计算机上测试我们的游戏时,假设他的计算机比我们开发游戏的设备强大得多,游戏的速度就会发生巨大的变化。 ..在大多数情况下,游戏动画的发生速度比预期的要快。那么我的问题是,如何在游戏应用程序中防止此类问题?是的,我们使用 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.
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)