How to make the speed (frame rate in your game) the same for different PCs?

It is customary in our school to create games as cool projects in the implementation of various concepts that we learn from our computer science classes. Now we have developed our games on our machine, and everything seems to be working fine, the speed of the game is normal, and so on. Now, when we try to test our games on our school computer or when our professor tests our games on his own computer, let's say that his computer is much more efficient than the one in which we developed our games, the speed of the game changes dramatically ... in In most cases, game animation will occur as fast as expected. So my question is, how do you prevent a similar problem in gaming applications? And yes, we use Java. Usually we use passive rendering as a rendering method in most of the applications we created. tnx in advance!

+2
source share
4 answers

You should not rely on rendering speed for your game logic. Instead, track the time spent from the last logical step in the game to the current. Then, if the time spent exceeds a certain amount, you complete the game step (in rare cases, when the computer is so slow that two steps should happen, you might want to develop an intelligent solution to ensure that the game does not lag).

Thus, the logic of the game is separated from the visualization logic, and you do not need to worry about changing the speed of the game depending on whether vertical synchronization is turned on or off, or if the computer is running slower or faster than yours.

Some pseudo codes:

// 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. } 
+6
source

You may find this article interesting: Fix your time theme .

+2
source

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

Perhaps you could just update the ceratin variables whose animation depends on every couple of milliseconds?

0
source

You can also use javax.swing.Timer to control the speed of the animation. There is a trivial example in this answer .

0
source

Source: https://habr.com/ru/post/1303023/


All Articles