Skip to content

anonymous /loop.java

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.