Using Timing, Please Help!

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 :frowning:

Thank you so very much in advance!

I have just what you need. I did a 1.5 hour presentation today to my school on how to program games in Java, and here is the html I made to guide the presentation. At the top is a link to source to two games I wrote for the presentation so that you can see how to do the stuff you’re talking about.

http://www.gamelizard.com/JavaGameProgrammingPresentation.htm

Enjoy!

Hey I just got your reply!

Thanks so much for the information, Ive only briefly skimmed it so far but I am just about to go through the whole thing, very nice work!

Once again, thank you!

As always, all the timing examples I see are with a set limit on the fps. Doesn’t anyone use a loop that has a set number of game updates and renders as often as possible to the screen? I thought that this was a better way of doing a game loop, am I wrong?

There is one tutorial on dicsrete timing loops by Marcuss Person, that he wrote 2 years ago. Since then, I have seen almost nothing else on that subject. I was wondering what the reason for this is?

they seem more dificult to manage. a fixed FPS is just easier to do.

Hey Kommi, do u have the link for this tutorial?

I explained how to do this here:

http://www.gamelizard.com/forums/viewtopic.php?p=582#582