Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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 1933 times)
0 Members and 1 Guest are viewing this topic.
Offline Zenon

Innocent Bystander





« 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!
Legends of Yore - The Casual Retro Roguelike
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.

 

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

The first screenshot will be displayed as a thumbnail.

pw (18 views)
2014-07-24 01:59:36

Riven (17 views)
2014-07-23 21:16:32

Riven (14 views)
2014-07-23 21:07:15

Riven (17 views)
2014-07-23 20:56:16

ctomni231 (45 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (51 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org 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‑gaming.org
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!