Game Loop Attempt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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