Results 1 to 5 of 5
Like Tree3Likes
  • 3 Post By gimbal2

Thread: Game Loop design

  1. #1
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    86
    Rep Power
    0

    Default Game Loop design

    I have been trying to create a simple game in Java and have come to some confusion. I understand that keeping track of processing time is important for some games if the need is to be precise as possible. As I dont need this, I decided that using a timer might be a simpler method.

    Java Code:
    Timer timer = new Timer(1000/60, this);
    I know it wont be precise to the millisecond, but is there anything wrong with implementing a Timer in this method? And then just put in my update and render methods in the ActionPerformed method?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Game Loop design

    Quote Originally Posted by rru96 View Post
    ... is there anything wrong ...
    When you tried it, did you get the desired result?

    Oh and I hope you're aware the 1000/60 will perform integer division and resolve to 15 ms. And that on most (all?) Windows systems, the granularity of javax.swing.Timer is around 14-16 ms.

    db

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    86
    Rep Power
    0

    Default Re: Game Loop design

    Well.... I got the expected result. I know that sometimes there are more than 1 way to get the same result so I guess I just wanted to make sure that what I was doing wasn't 'bad practice'.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: Game Loop design

    Imagine having to do that with every single statement you write, you'll be banned for spamming the forum.

    My advice: don't be afraid to make mistakes; that's where you learn stuff. Don't deny yourself the pleasure of fixing it either, if you dive into this wanting everything to be perfect from the get-go, you're in for a big disappointment and programming will become a huge chore.

    Just go for it, see where you end up.
    DarrylBurke, rru96 and AidoP like this.

  5. #5
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    160
    Rep Power
    2

    Default Re: Game Loop design

    You could use a timer, but I think a better way is to use threads. You can have your class implement Runnable and create a new thread. This thread would have a while loop that updates and paints, but also calculates processing time and sleeps the Thread the desired time for the game to stay at a constant update speed.

    Here is the way I do my game loops:

    Java Code:
                    int FPS = 60;
                    long targetTime = 1000 / FPS;
                    long start;
    		long elapsed;
    		long wait;
    
    		while(isRunning) {
    			start = System.nanoTime();
    
    			tick();
    			repaint();
    
    			elapsed = System.nanoTime() - start;
    			wait = targetTime - elapsed;
    
    			if(wait < 0) {
    				wait = 15;
    			}
    
    			currentFPS = 1000 / wait;
    
    			try {
    				Thread.sleep(wait);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}

Similar Threads

  1. General Concept Question For Game Design
    By New_User in forum New To Java
    Replies: 0
    Last Post: 04-03-2012, 03:29 AM
  2. Java Game Design
    By conker5295 in forum Java Gaming
    Replies: 1
    Last Post: 03-14-2012, 07:33 PM
  3. Simple Android Game Design
    By fresh83 in forum New To Java
    Replies: 1
    Last Post: 05-16-2011, 05:40 AM
  4. game board design
    By tomitu in forum New To Java
    Replies: 12
    Last Post: 02-27-2010, 05:31 PM
  5. Game design & where and how i should learn it
    By helpisontheway in forum New To Java
    Replies: 9
    Last Post: 01-14-2010, 03:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •