I'm doing a game, and I want to update the x and y offsets (integers) of my player image every few seconds or so by the hspeed and vspeed. Sort of like:
1 2 3 4 5 6
if (player.hp>0)
{
//handle movement (I want to do this every 100 milliseconds or so)
player.x = player.x+player.hspeed;
player.y = player.y+player.vspeed;
}
Some comments, handled in Disch link :
On windows at least, you wan't wait for a fixed number of ms precisely, depending on the task scheduler. On my computer, time slices are about 15ms, so i can't do pauses of 17ms exactly for example.
In that case, you must have a sleep() in your loop, and at each iteration, see if the time elapsed since the last frame refresh is at least the period wanted between frames.
You must also handle the case where the program is slower than expected (for hardware or software reasons). So you must also test if the elapsed time since the last refresh is superior to the period between 2 frames, and in that case update your game status for that number of frames before refreshing. That will guarantee that the games stays at the same visible speed, at the cost of missed frames. if you don't do that, your game animation will slow down when the computer takes more time to execute each frame thatn the wanted period between frames