Java Main Game Loop

Java Main Game Loop

Eventually youll want to move to something like LWJGL, but let me stress, keep doing what youre doing here for now. It will teach you fundamentals.

Good job on your loop. Looks nice, let me offer a few pointers:

  • Repaint will not render the screen immediately. It tells the RepaintManager to render when its ready. Use invalidate paintImmediately instead. paintImmediately will block execution until the component has been redrawn so you can measure rendering time.

  • Thread.sleep typically has a few milliseconds drift. You should be using it to keep your loop from using too much CPU, but make sure you understand if you sleep 10 milliseconds you might sleep 5 milliseconds or you might sleep 20.

  • Lastly:

    double delta = updateLength / ((double)OPTIMAL_TIME);
    

    If updateLength is less than OPTIMAL_TIME, dont call update. In other words, if delta is less than one, dont update. This tutorial explains why better than I ever could.

Overall, it is a good loop, but there are a few missing aspects to what I have found in experience to be the best loop.

You will eventually want to move to LWJGL or some other java game API, but for now, learn the basics of how game-loops work, and what best suits your needs.

  • Firstly, in answer to one of your points, no. You will do better staying away from

    Thread.sleep()

    this can stray from the real amount of time you set it to sleep.
    e.g. if you set it to sleep for 10 milliseconds, it could sleep the program for 5 to 20 milliseconds.

  • The second problem I cam immediately see is that you do not have any way to stop the game-loop for a custom stop() method. Try

    boolean running = true;
    while (running) {
    // Your Code Here //
    }

  • Thirdly, you may want to consider changing how you use your delta variable. The way in the code below may be a better use and construction for you.

    This is an example of my game-loop that I use in my programs:

    @Override
    public void run() {
    
    long initialTime = System.nanoTime();
    final double timeU = 1000000000 / UPS;
    final double timeF = 1000000000 / FPS;
    double deltaU = 0, deltaF = 0;
    int frames = 0, ticks = 0;
    long timer = System.currentTimeMillis();
    
        while (running) {
    
            long currentTime = System.nanoTime();
            deltaU += (currentTime - initialTime) / timeU;
            deltaF += (currentTime - initialTime) / timeF;
            initialTime = currentTime;
    
            if (deltaU >= 1) {
                getInput();
                update();
                ticks++;
                deltaU--;
            }
    
            if (deltaF >= 1) {
                render();
                frames++;
                deltaF--;
            }
    
            if (System.currentTimeMillis() - timer > 1000) {
                if (RENDER_TIME) {
                    System.out.println(String.format(UPS: %s, FPS: %s, ticks, frames));
                }
                frames = 0;
                ticks = 0;
                timer += 1000;
            }
        }
    }
    

Java Main Game Loop

the simplest way to refresh repainting could be like this:

public class GameLoop extends JPanel {
private final BufferedImage back_buffer;
bool state = true;

public void init() {
        while (state) {
            updatePlayer();
            delay(5);
        }
    }

public void delay(int time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        repaint();
    }

and the repaint function is who update all of the graphics

@Override
    public void paint(Graphics g) {
        super.paint(g);
        
        grapicDoble.setColor(Color.white);
        grapicDoble.fillRect(0, 0, 500, 500);
        game.reset();
        
        g.drawImage(back_buffer, 0, 0, this);
    }

Leave a Reply

Your email address will not be published.