Hiya everyone!
I have recently been incredibly frustrated with the idea of keeping a consistent rate of updates per second in a Java game.
I have read (or attempted to read) Kevin Glass’ excellent space invaders tutorial as well as the ones about creating the Pong game on Sys-Con, but I think I am a little dumb because I just flat out don’t understand the timing aspect of it.
Here is the basic scenario I am trying to figure out:
You are making a two-player Pong game, with the standard two paddles and a ball.
Every time a button is pressed to move a paddle, the keylistener picks it up and then adds, say the value of 5 or subtracts 5 to the variable paddleMove.
Then, in the next game update, the game logic moves the paddle’s y position accordingly.
This is what I have down so far in my mind, please correct me if its all wrong.
But the problem I am having figuring out is how to have the ball move consistently throughout the game.
The only thing I can think of now is to update the ball’s position one time per loop, but with a drop off in frame rate the ball slows down as well.
I tried thinking about how to implement this where the game updates multiple times before rendering if the frame rate is lagging, but I couldn’t figure it out.
I mean, if the ball would move 5 pixels per update, and the game updated 3 times before rendering, that would move the ball 15 pixels.
But how would the game know exactly how many times to update, or… I don’t know, I am simply confounded.
If anyone could please maybe explain how to implement a standard update per second system in a Pong type game, please do!
I have been unable to grasp this concept since my first day in Java programming
Thank you so very much in advance!