Problem with game loop

I’m trying to replicate my code from in Python with Java to make a snake game. Upon setting up the basics of the game, a helpful person from IRC found me a thread which ra4king posted in that gave the exact details of a perfect game loop.

This is, however, where my problem started. I implemented all the code and then ran it. Immediately I noticed the window lagging and taking some time to move it around or actually do anything and the FPS that was being printed was 6000-8000, strange? This loop is here, http://www.java-gaming.org/topics/about-on-my-main-loop/26222/msg/229028/view.html#msg229028

My code is in the following - https://dl.dropbox.com/u/102063272/Snake.zip

Note: I am running this in Ubuntu, but when Xardov ran it on Windows 7 it runs perfectly fine at 800-1600 FPS.

My understanding is that the Java 2D is slower in Linux, with latest Java version (7) being fastest. What version of Java are you using?

Don’t bother implementing r4king’s/rivens GameLoop lol, that’s for a real time game, not for a 2D game loop, I also tried implementing it and it didn’t work for shit lol.

Gabriel why are you still here? It pains me to say this, but I have had similar issues using the busy loop idea where it would update too much. Best advice I can give for a very simple game is to just call Thread.sleep(1000/60). I have used that and not had any issues. Your game will go up or down 1-2 fps due to java’s timing issue. If you want to keep game play consistent implement a veritable gameloop which is not too hard just do some googlfooing.

Quick fix for the WINDOWS timing issue would be to simply start an endlessly sleeping daemon thread. If you’re using LWJGL, update to the latest version which does it for you.

I don’t see how ‘realtime’ excludes ‘2d’. A framerate throttler either works reliably or it doesn’t.

My game-loop is behind LWJGL’s Display.sync(…), so it probably is less shit than you make it out to be…

http://java-game-lib.svn.sourceforge.net/viewvc/java-game-lib/trunk/LWJGL/src/java/org/lwjgl/opengl/Sync.java?revision=3803&view=markup

I’m just trying to implement some code so that I get constant slowed movement in a snake game. The way I imagine to achieve this would be to do:

long initialTime = System.currentTimeMillis();
while (running) {
    if (System.currentTimeMillis() - initialTime >= 1000) {
        render();
        update();
        initialTime = System.currentTimeMillis();
    }
}

However I still get the super laggy window problem.