Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (756)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (842)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to handle time in a very basic game  (Read 1589 times)
0 Members and 1 Guest are viewing this topic.
Offline cmorris975

Senior Newbie

« Posted 2005-10-20 16:52:18 »

Ok, I'm 6 weeks into a Java I class so my knowledge is pretty limited at this point, but as I mess around with the Java that I do know it becomes clear that I need to somehow manage the passing of time in my little java games.  In order to keep my pentium 4 computer from racing through any program I write.  Can anyone give me a clue as to how I can start to learn how to do this?  I'm at the level where I'm making a box move across a playing field, I would like to slow down the redraws.

Thanks for any help!

Offline cborders

Junior Devvie

« Reply #1 - Posted 2005-10-20 18:05:15 »

You could do a Thread.sleep(n) where n is the number of milliseconds you want the thread to wait before continuing.
Offline darkprophet

Senior Devvie

Go Go Gadget Arms

« Reply #2 - Posted 2005-10-20 18:19:03 »

In an ideal world, you would measure how long 1 frame is and use the time difference from each frame as a fourth dimension in your equations. However, this isn't an ideal world, however, if you dont use physics too much (i.e. alot of equations that rely on time and whatnot) then you can use the above method. Otherwise, I would go with what cborders said and limit your FPS using sleep or yield to 60 and just assume that your FPS will always be 60.

The above 60fps situation is ideal for consoles because consoles slow down if they can't achieve 60fps and render every frame, this makes sure that the game stays on track, however, PC's dont do that and your going to have to assume a minimum system requirements...

Thats my take on things, use as necessary.


Friends don't let friends make MMORPGs.

Blog | Volatile-Engine
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cmorris975

Senior Newbie

« Reply #3 - Posted 2005-10-20 19:55:23 »

Thanks for the replies guys Smiley  Will mess around with thread.sleep tonight....

Offline JAW

Senior Devvie

Medals: 2

« Reply #4 - Posted 2005-10-22 09:55:27 »

For very basic things I used a fixed Thread.sleep(20) or such. The processing time is so small, like up to 5 ms, so that
a fixed sleeping time does it. I am speaking of things like Pong or Breakoid and very basic graphics quality.

The second level of timing is the synchronized framerate. You decide on a time per frame, something like 50 ms (20 FPS)
or better. 33 ms for 30 FPS, 20 ms for 50 FPS, etc. Then you take the time at the beginning and end of your game loop
and calculate the time passed, then you go sleep for the remaining time.

Say your game uses 7 ms and your desired time per frame is 20, you would sleep 13 ms. One important thing is, that you at least sleep a few millis, say a minimum of 3 to 5 ms. This gives other threads and garbage collection some time to act.
This method goes down when the pc is too slow and you dont get the frame down in the desired time. The game will then slow down.

The last option is dynamic framerate. At the beginnig of each frame, you take the time. So you can measure the time that passed since the last frame. Then you do all updating based on the passed time. You need to do a sleep of 5 ms or something fixed, and everything else goes as fast or slow as it goes.

The main difference is, with a fixed framerat you can use fixed speed. 5 pixels per frame movement with 30 FPS will always be 150 pixels movement per second. When using a dynamic framerate, you can have more or less FPS and have to do dynamic movement based on the time, so that you end up with 150 pixels per second. You must calculate something like movement per ms and multiply with the ms passed since last frame.

Important: On some Systems, especially Windows, System.currentTimeMillis() is not accurate. You should use System.nanoTime() to take you timing. nanoTime gives you ms * 1.000.000 so in order to get ms times, you need to divide (time2 - time) / 1.000.000.

Pages: [1]
  ignore  |  Print  

DesertCoockie (54 views)
2018-05-13 18:23:11

nelsongames (86 views)
2018-04-24 18:15:36

nelsongames (76 views)
2018-04-24 18:14:32

ivj94 (761 views)
2018-03-24 14:47:39

ivj94 (93 views)
2018-03-24 14:46:31

ivj94 (645 views)
2018-03-24 14:43:53

Solater (104 views)
2018-03-17 05:04:08

nelsongames (187 views)
2018-03-05 17:56:34

Gornova (428 views)
2018-03-02 22:15:33

buddyBro (1088 views)
2018-02-28 16:59:18
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!