續:一種讓數值在指定範圍內由小到大再變小的生成方法(固定時間內)

#include "stdafx.h"
#include <windows.h>
#include<time.h>
#include <iostream>

using namespace std;

const static int TIME_PER_FRAME = 100;
SYSTEMTIME sys;

const static int TOTAL_TIME = 10000;
const static int CHANGING_TIMES = 4;

const static int RANGE = 80;
const static int FRONT = 100;

int GetCurTime()
{
	GetLocalTime(&sys);
	return sys.wMinute * 60000 + sys.wSecond * 1000 + sys.wMilliseconds;//big enough for this test code
}

int main()
{
	int startTime,endTime;
	int curPassedTime = 0;
	int changedTimes = 0;
	int curValue = 0;
	int TIME_START =  GetCurTime();
	while(curPassedTime < TOTAL_TIME)
	{
		startTime = GetCurTime();
		curPassedTime = startTime - TIME_START;

		if(curPassedTime >  (changedTimes + 1) * TOTAL_TIME / CHANGING_TIMES)
			changedTimes += 1;
		
		int seedTime = curPassedTime -  changedTimes * TOTAL_TIME / CHANGING_TIMES;

		if(seedTime < TOTAL_TIME / (2 * CHANGING_TIMES))
			curValue = FRONT - seedTime * 2 * RANGE / (TOTAL_TIME / CHANGING_TIMES);
		else
			curValue = FRONT - 2 * RANGE + seedTime * 2 * RANGE / (TOTAL_TIME / CHANGING_TIMES);

		cout<< curValue << endl;
		
		endTime = GetCurTime();	
		if(endTime - startTime < TIME_PER_FRAME)
			Sleep(TIME_PER_FRAME - (endTime - startTime));//fixed FPS
	}
}


發佈了32 篇原創文章 · 獲贊 3 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章