Skip to content

Instantly share code, notes, and snippets.

Created June 26, 2012 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2997312 to your computer and use it in GitHub Desktop.
Save anonymous/2997312 to your computer and use it in GitHub Desktop.
Game Loop Attempt
public void runGame(){
Graphics2D gImage = (Graphics2D)canvas.getGraphics();
long lastUpdate = System.nanoTime();
long startTime = System.nanoTime();
long endTime = System.nanoTime();
int counter = 0;
//UPS = updates per second
int UPS = 1000000000 / 25;
while(isRunning){
//run update at 25 updates per second
while(System.nanoTime() - lastUpdate > UPS){
updateGame();
lastUpdate += UPS;
}
endTime = System.nanoTime();
//Code to see if the update method is really running at 25 updates per second
if(endTime - startTime >= 1000000000){
System.out.println(counter);
counter = 0;
startTime = System.nanoTime();
}
//Calculate how much to move entities based on the time between updates
float interpolation = (float)(System.nanoTime() - lastUpdate) / (float)UPS;
puck.interpolate(interpolation);
p1.interpolate(interpolation);
p2.interpolate(interpolation);
checkCollisions();
//render without an upper limit
render(gImage, interpolation);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment