Moving objects in my game

In my game are ducks. Atm I’m moving them some pixels in every loop. MY problem is, that the ducks are moving faster on new computers, and on old computers they’re moving slowly. How could i fix that?

And btw if you see a mistake in my code pls post suggestions

https://github.com/ritonda66/EntenShooter/blob/master/src/org/shooter/gamePanel.java

You have to seperate logic-updates from rendering, or set your rendering to a fixed rate.

you can calculate the delta (time since last update) and then base the movement off of that.

basically you take the current time each frame, then get the difference from the last frame.

Using that I do something along these lines :


public void update(int delta)
{
//set a cap on speed
if(delta>80)delta = 80;
x+=vx*delta/10;
y+=vy*delta/10;
}

This is not tailored to your code in any way, its just a generic idea, if you have any questions feel free to ask.

-h3ckboy

Looks like you are only sleeping for 10ms every frame. In a case for example, on a faster computer, one frame may take 1ms or less while on a slower computer, it may take 3ms. One solution is implementing a fixed rate game loop. Basically, you make it sleep for same amount of time every frame by using calculation of how long a frame took. If the frame finishes earlier than the time you specified, you put it to sleep for the difference. There are plenty of resources on this community.