Hey guys i’m new here.
I have been working on a Block breaker clone for desktop as a project to get me used to the best practices and designs idioms for Java games. I have a first version which worked great on my more powerful laptop but when I went to my Net book I found it stuttered badly.I have seen all your different game loops but with no definitive answer on the best I was wondering if you guys could suggest improvements for my attempt at it
private void gameLoop() {
// Regenerate the game objects for a new game
// ......
state = State.PLAYING;
// Game loop
long beginTime, timeTaken, timeLeft;
while (true) {
beginTime = System.nanoTime();
if (state == State.GAMEOVER) break; // break the loop to finish the current play
if (state == State.PLAYING) {
// Update the state and position of all the game objects,
// detect collisions and provide responses.
gameUpdate();
}
// Refresh the display
repaint();
// Delay timer to provide the necessary delay to meet the target rate
timeTaken = System.nanoTime() - beginTime;
timeLeft = (UPDATE_PERIOD - timeTaken) / 1000000L; // in milliseconds
if (timeLeft < 10) timeLeft = 10; // set a minimum
try {
// Provides the necessary delay and also yields control so that other thread can do work.
Thread.sleep(timeLeft);
} catch (InterruptedException ex) { }
}
}
As a side question is it better to leave Swing to handle the screen or should I handle that myself?
Thanks guys
AbtractChaos