-
controlling game speed
Im making a snake game and i need some help figuring out how to control the snake speed, and make it the same speed on all machines. i want the snake to move a couple times every second, or something like that, but i need a function to measure time less than 1 second. i think i could then test the time twice, subtract them, and see if the diff is a certain amount and if it is move the snake. does this sound about right. thanks for the help
Jesse
-
yup.. sounds right.
have a look at time(), GetTime(), timeGetTime(), or functions similar to that. if you're using windows you could even set a time to go off every half second to update the snake position.. there are quite a few ways of doing this.
hope that helps!
U.
-
i tried time(), and it will only return whole seconds, it looks like gettime() does the same, only w/ hour min, sec, and i couldn't find any info on timegettime()
can some help me out some more?
-
timeGetTime() returns the number of milliseconds (as a DWORD)since Windows started. You need to include <mmsystem.h> and link to winmm.lib. Here's an example:
Code:
DWORD StartTime=0, EndTime=0;
//in your loop
EndTime=timeGetTime();
if(EndTime-StartTime>1000)
{
//do stuff
StartTime=EndTime; //reset start time variable
}
-
What header is timeGetTime() in? I'm pretty sure it's time.h, right?
-
It's in the windows multimedia headers. You'll also need to link to the multimedia library. Following code will do both..
#include <windows.h>
#pragma comment( lib, "winmm.lib" )
EDIT: Alternatively, if you don't want to use the pre-processor directive, you can link to the winmm.lib under project settings.