Domanda

I'm working on a simple Java 2D game.

I wrote a certain game-loop for the game. Using this game-loop, the game sometimes runs in moderate (constant, I think) speed, and sometimes when I start it, it runs in slow (constant) speed.

I learned to implement this game-loop from the following article: Here

It's code:

(I changed it's code to Java. You can find this loop in the article under the title "FPS dependent on Constant Game Speed").

public void run(){

    next_game_tick = System.currentTimeMillis();

    while(true){

        updateGame(); // This is actually a bunch of methods.

        repaint();

        next_game_tick = next_game_tick + skip_ticks;
        sleep_time = next_game_tick - System.currentTimeMillis();

        if(sleep_time<0)sleep_time=2;
        try {
            Thread.sleep(sleep_time);
        } catch (InterruptedException e) {}

        next_game_tick = System.currentTimeMillis();

    }

}

As I said, this loop wasn't always running the game at an appropriate speed, so I looked at the other game-loop implementations suggested in the above article.

The following implementation, only based on it's description, seemed like the most suitable for my game:

int TICKS_PER_SECOND = 50;
int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
int MAX_FRAMESKIP = 10;

long next_game_tick = System.currentTimeMillis();
int loops;

bool game_is_running = true;
while( game_is_running ) {

    loops = 0;
    while( System.currentTimeMillis() > next_game_tick && loops < MAX_FRAMESKIP) {
        update_game();

        next_game_tick += SKIP_TICKS;
        loops++;
    }

    repaint();
}

(I changed it's code to Java. You can find this loop in the article under the title "Constant Game Speed with Maximum FPS").

My problem is, I don't fully understand it. I read it's description in the article, and understood that this game-loop might be the best for my game, but didn't understand fully how it works.

I would appreciate if someone explained this game-loop to me, so I would fully understand it before trying to implement it in my game.

(EDIT: I understand the concept of a game-loop and how it works, but not this game loop specifically and everything it does).

Thank you for your help

È stato utile?

Soluzione 2

next_game_tick stores the moment at which the next game tick is suppose to happen. You have two different times. The real time that you can get with System.currentTimeMillis(), and the game time which is equal to SKIP_TICKS times the number of iterations of your loop plus the time at which the game started. In the loop the update method is called until the game time has caught up with the real time. Each call of update_game advances the "clock" of the game by SKIP_TICKS ms.

Altri suggerimenti

The game loop you found is actually way outdated. You might want to search for a more up-to-date tutorial. For your game try the following:

First define the game loop:

class GameLoop extends Runnable {
  public void run() {
    updateGame();
    repaint();
  }
}

And call it like this:

static final long TARGET_FPS = 60;
static final long TICK_RATE = 1000 / TARGET_FPS;

// ... in main:
final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(new GameLoop(), TICK_RATE, TimeUnit.MILLISECONDS);

Your GameLoop will now be called about as often as requested, while not wasting CPU time on waiting, you can find more information why in the documentation of scheduleAtFixedRate. If you use the proper Java classes, coding usually becomes much easier.

As a side note: there is no need to update the game if the update is not then drawn to the screen. You should try to code your calculations to not require intermediate steps, because that's just a waste of CPU time.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top