1-
for(pause=0;pause<100000;pause++) |
Most likely the compiler will optimize it anyway. So instead of processing 100000 commands it only performs a single command.
A computer can handle hundreds of million commands (depended of speed of each different computer), that means your 100000... I think it only takes less than 0.1 second...
And the solution? Let's think about a proper function. Why don't you use a function? That is a good knowledge, because you should know the function
"Sleep()".
Sleep(milliseconds) - Sleeps the program for "milliseconds
E.g : Sleep(2578) //The program will sleep for 2.578 seconds
Suppose you want to keep the input screen for 5 seconds... The right code is "Sleep(5000);. But you also want to handle keys... That's an another problem.
A method :
- Create a "Sleep" time variable.
- Then, determine the update speed. You can set Sleep(10) //Sleeps for 0.01 second - 100 frames per second
- Then subtract the time variable. (Eg In this case subtract 10 milliseconds..)
- If the time variable is lower than 0 (Time out), break the loop...
You also want "If any proper key is pressed, the program will reset the time interval"? That is easy, reset the time interval.
And the code example :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
#define TIME_INTERVAL 4000 //4 seconds
int nMilliseconds = TIME_INTERVAL;
..........................
while (nMilliseconds > 0)
{
if(GetAsyncKeyState(VK_UP)!=0)
{
[...]
//Reset example : nMilliseconds = TIME_INTERVAL
}
if(GetAsyncKeyState(VK_DOWN)!=0)
{
[...]
}
if(GetAsyncKeyState(VK_LEFT)!=0)
{
[...]
}
if(GetAsyncKeyState(VK_RIGHT)!=0)
{
[...]
}
Sleep(15); //Sleeps for 15 milliseconds (0.015 sec)
nMilliseconds = nMilliseconds - 15;
}
|
2-
ClearScreen();
for(MAP=0;MAP<14;MAP++)
{
cout << map[MAP] << endl;
}
|
Making a function for this specific code is better.