Reputation: 793
I have built a Java game (applet) and have put it on a test website. I can't display the whole code on here as its to big, but the game runs at 20 frames/seconds as it sleeps in the main loop for 50 milliseconds. I have run the game on three computers, two of them are faster then the other on its processor speed and ram. The other one is a small notebook and it doesn't run my game properly. My game is like a breakout game with a paddle and a ball, the problem is, when the ball move it slows down and speeds up a regular Patton (it is extremely noticeable). This happens on the small computer, but not on the other two.
The Game: To make this game I followed this series on YouTube.
So basically:
There are three computers:
With the knowledge above I can rule out then the game is not dependent on the processor speed and must be something else. Could it be because of the notebooks processor, as its two weak to process it? but it is 1.6 GHz, so i don't see how it to week for a little game. I really don't know what to think, any ideas would be appreciated.
Upvotes: 1
Views: 508
Reputation: 467
Instead of just sleeping between cycles, try something like this:
...
final long timeSlice = 100;
while(true) {
long startTime = System.currentTimeMillis() ;
doMyStuff();
long endTime = System.currentTimeMillis();
sleep(Math.max(timeSlice + startTime - endTime, 0))
} // end while()
...
You will end up with each cycle taking about the same time.
Upvotes: 2