开发者

How to make the speed (frame rate of your game) the same across different PC?

In our school, it is common to build games as class projects in implementing the different concepts we learn from our computer science classes. Now we developed our games in our machine and everything seems to work fine, game speed is normal and so on. Now, when we try testing our games in our school's computer, or when our professor test our games in his own computer, let's say his computer is much powerful compare to the unit where we developed our games, the speed of the game changes dramatically... in most cases the game animation would happen so fast than expected. So my question, how do you prevent this kind of problem in game applications? And yeah, we use Java. We usually use passive rendering as the rendering technique in most applicati开发者_JAVA百科ons that we built. tnx in advance!


You shouldn't rely on the speed of rendering for your game logic. Instead, keep track of the time spent since the last logical step in the game to the current one. Then if the time spent has exceeded a certain amount, you execute a game step (in rare cases, where the computer is so slow that two steps should have happened, you may want to come up with a smart solution for making sure the game doesn't lag behind).

This way, game logic is separate from rendering logic, and you don't have to worry about the game changing speeds depending on whether vertical sync is on or off, or if the computer is slower or faster than yours.

Some pseudo-code:

// 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.

}


The best way to achieve portability would be to issue some Thread.sleep() commands with the same delay between drawing of each frame. Then the results would be consistent accross every system. Since you are using passive rendering, i.e. drawing your animation directly in paint, calling Thread.sleep() from there may not be the best idea...

Maybe you could just update ceratin variables your animation depends on every couple of milliseconds?


You can also use javax.swing.Timer to govern the animation rate. There's a trivial example in this answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜