Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (405)
games submitted by our members
Games in WIP (289)
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  
  FPS and UPS / General Animation Looping  (Read 1303 times)
0 Members and 1 Guest are viewing this topic.
Offline Zenon

JGO Visitor




« Posted 2010-12-21 22:42:44 »

After reading Java for Dummies and understanding the fundamentals of programming, I've wanted to move onto to game making. After a bit of looking around for tutorials, I have found that there are 20,000 ways to program the animation loop, some work, some don't, some suck, others don't, and I can't quite decide what's going to be best for me. I've read some tutorials, but with everyone disagreeing on the subject, it's hard to form a basis. So how do people on this forum program the basic animation loop for their games?
Offline Gudradain
« Reply #1 - Posted 2010-12-21 23:22:14 »

Mostly with a
1  
2  
3  
while(true){
     //execute your game code here
}


Here is an example : Basic Game
Offline dbotha

Senior Newbie





« Reply #2 - Posted 2010-12-21 23:27:59 »

Take a look at this article if you haven't already, it will give you a good starting point.
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline philfrei
« Reply #3 - Posted 2010-12-22 10:45:56 »

The following is from the article link in the previous post.

Quote
example code of the game loop in it’s most simplest form:

1  
2  
3  
4  
5  
6  
    bool game_is_running = true;

    while( game_is_running ) {
        update_game();
        display_game();
    }

The problem with this simple loop is that it doesn’t handle time, the game just runs.

I'm in a minority, but I think an entirely reasonable alternative is to consider using a single java.util.Timer to run the game loop. It's not that hard to set up an "Observer" design pattern to subscribe and unsubscribe all objects or processes that need animation ticks. I started an example in the Tutorials section but haven't had time to make the revisions to put it in an easier-to-read format. One big plus: new loops start at regular intervals without any need to further manage the amount of code that runs within each animation cycle (as long as you don't try to animate 10,000 objects and start overshooting the cycle time). Seems to scale well.

If you don't want to deal with the Observer pattern, this is even simpler, and gives a steady 40 updates a second (well actually a little less, but still reasonably steady):
1  
2  
Timer timer = new Timer();
timer.schedule(new RemindTask(), 25, 25);
and
1  
2  
3  
4  
5  
6  
class RemindTask() extends TimerTask {
   public void Run() {
      updateGame();
      yourJPanelOrWhatever.repaint();
   }
}


It's not that hard to set it up so that you can turn the Timer on or off as needed. And the repaints occur often enough you probably won't need to add any more of them to the response code for the various listeners.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (58 views)
2013-05-17 21:29:12

alaslipknot (67 views)
2013-05-16 21:24:48

gouessej (97 views)
2013-05-16 00:53:38

gouessej (94 views)
2013-05-16 00:17:58

theagentd (105 views)
2013-05-15 15:01:13

theagentd (96 views)
2013-05-15 15:00:54

StreetDoggy (142 views)
2013-05-14 15:56:26

kutucuk (164 views)
2013-05-12 17:10:36

kutucuk (163 views)
2013-05-12 15:36:09

UnluckyDevil (173 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.091 seconds with 21 queries.