当前位置:网站首页>The latest tank battle 2022 - full development notes-3

The latest tank battle 2022 - full development notes-3

2022-07-06 13:27:00 Programmer rock

8、 ... and 、 Create a tour loop

8.1 Create the main loop of the game

int main(void) {
	init();

	createMyTank();
	createEnemyTank();

	while (1) {
		updataMap();
		updataAllTanks();
	}
	

	system("pause");
	return 0;
}

8.2 Use time counting to optimize the loop

The cycle structure of the previous step , Keep refreshing , It seriously consumes the performance of the system , It must be improved . The easiest way , Use time counting to process . Be careful , Use it directly Sleep It will affect the timely response of the game .

bool updata; //  Whether to update ,  And in init Function is initialized to true

int main(void) {
	init();

	createMyTank();
	createEnemyTank();

	int time = 0;
	while (1) {
		time += getDelay();
		if (time >= 20) {
			updata = true;
			time = 0;
		}
		
		if (updata) {
			updata = false;

			BeginBatchDraw();
			updataMap();
			updataAllTanks();
			EndBatchDraw();
		}
	}
	
	system("pause");
	return 0;
}

原网站

版权声明
本文为[Programmer rock]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060916109961.html