通过游戏中的按钮逃脱延迟 [英] Escape delay with a button in a game

查看:51
本文介绍了通过游戏中的按钮逃脱延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对游戏的代码有问题。我想知道,如果我按下按钮,如何打破时间延迟。



最后游戏将你的最终得分写在液晶显示器上10秒钟。然后回到等待开始。我希望保持原样 - 我想要包括的唯一一件事是,如果我在10秒钟内按下按钮,我希望它立即回到等待开始。

* ************************************************** ***************************************

功能代码在这里

Hi, i have a problem with a piece of code for a game. I would like to know, how to break the time delay if i press the button.

At the end the game writes your end score on the lcd for 10 seconds. And goes back to the wait for start. I want to keep that as it is - the only thing that i want to include is that if i press the button within those 10 seconds i want it to go immediately back to the wait for start.
**************************************************************************************
Function code is here

void DrawGameScore()
{	//&RFONT_8X12 &RFONT_16X26
	//char key;
	//key = KBD_GetKey();
	char txt[10];
	UG_FontSelect(&RFONT_16X26);
	UG_SetForecolor(C_WHITE);
	switch (level){
		case 1:
			
			UG_PutString(60,130,"Peasant Score:");
			sprintf(txt,"%d",score);
			UG_PutString(120,160,txt);
			_delay_ms(10000);
			break;
		case 2:
			UG_PutString(60,130,"Knight Score:");
			sprintf(txt,"%d",score);
			UG_PutString(120,160,txt);
			_delay_ms(10000);
			break;
		case 3:
			UG_PutString(60,130,"Queen Score:");
			sprintf(txt,"%d",score);
			UG_PutString(120,160,txt);
			_delay_ms(10000);
			break;
		case 4:
			UG_PutString(60,130,"King Score:");
			sprintf(txt,"%d",score);
			UG_PutString(120,160,txt);
			_delay_ms(10000);
			break;
		case 5:
			UG_PutString(60,130,"God Score:");
			sprintf(txt,"%d",score);
			UG_PutString(120,160,txt);
			_delay_ms(10000);
			break;
	}
	//UG_PutString(60,130,"Final Score:");
	
	//sprintf(txt,"%d",score);
	
	
	//UG_PutString(120,160,txt);
	//_delay_ms(1000);
}



****************************************** ***********************************************

当游戏结束时,此功能会根据您的等级绘制得分。在下一个函数中,我调用了drawgamescore函数,它位于dsip_end_score makro中。




*************************************************************************************
When the game is over this function draws your score based on your level. In this next function i am calling drawgamescore function, which is in dsip_end_score makro.

int EndOfGame()
{
	char key;
	//static int state;
	int result = 1;
	key = KBD_GetKey();
	
	display_msg = DSIP_END_SCORE;
	if (key == BTN_OK){
		//display_msg = DISP_REFRESH_ALL;
		//display_msg = DSIP_END_SCORE;
		return result;
	} 
	return result;
}





********************** ****************************************** *

这是主要的游戏功能







****************************************************************
Here is the main game function


void Game()
{
	static int state;
	int result;

	switch (state)
	{
		case 1:	//waiting for game start
			result = WaitForStart();
			if (result == 1) state++;
			break;
		case 2: //Play the game
			result = PlayTheGame();
			if (result == 1) state++;
			break;
		case 3: //Display the score
			result = EndOfGame();
			if (result == 1) state=0;
			break;
		default: //0 or unknown value: reset the game
			state = 1;
			break;
	}
}







谢谢。



我尝试过:



我试过






Thanks.

What I have tried:

I tried

int EndOfGame()
{
	char key;
	static int state;
	int result = 1;
	key = KBD_GetKey();
	
	display_msg = DSIP_END_SCORE;
	if (key == BTN_OK){
		//display_msg = DISP_REFRESH_ALL;
		//display_msg = DSIP_END_SCORE;
		return result;
	} 
	return result;
}
	/*
	display_msg = DSIP_END_SCORE;
	if (key != 0){
		switch (key)
		{
			case BTN_OK:
			return result;
			break;
		}
	} 
	*/
	
	/*
	switch (state)
	{
		case 0:
		display_msg = DSIP_END_SCORE;
		state++;
		break;
		case 1: 
		key = KBD_GetKey();
			if (key != 0){
			switch (key)
			{
				case BTN_OK:
				return result;
				break;
			}
			break;
			}
		
	}
	return result;
	*/
	
	//display_msg = DISP_REFRESH_ALL;
	//display_msg = DSIP_END_SCORE;
	
	//return result;
	
	/*
	char key;
	int result=0;
	//static int state;
	key = KBD_GetKey();
	if (KBD_isKeyStatePressed(BTN_OK))
	{
		display_msg = DSIP_END_SCORE;
		result = 1;
	} else {
		result = 1;
	}
	//return result;
	*/
	/*
	switch (state){
		case 0:
			KBD_flush(); //empty buffer 
			//display_msg = DISP_REFRESH_ALL;
			display_msg = DSIP_END_SCORE;
			return result;
			state++;
			break;
		case 1:
			key = KBD_GetKey(); //kbd read dela background services
			if (key == BTN_OK){
				//display_msg = DISP_REFRESH_ALL;
				display_msg = DSIP_END_SCORE;
				return result;
				break;
			}
			break;
				
		
	}
	return result;
	*/
	/*
	key = KBD_GetKey();
	if (key == BTN_OK) {
		//display_msg = DISP_REFRESH_ALL; 
		return result;
	}else{
	display_msg = DISP_REFRESH_ALL;
	display_msg = DSIP_END_SCORE;
	
	
	//TODO: write the program
	
	return result;
	}*/

推荐答案

你没有提供足够的细节来确定(平台,操作系统等)但是我怀疑你的问题是_delay_ms()阻塞了。这意味着你的代码在延迟完成之前不会运行。



你可以做两件事 - 首先,编写自己的_delay_ms()版本,同时检查按键,如果发生这种情况,请尽早退出。其次,将它包装在您自己的过程中,将整个时间划分为更小的段。因此,您将对_delay_ms()的调用放入一个循环中,该循环也会检查按键。例如:

You don't give enough details to be certain (platform, OS, etc.) but I suspect your problem is _delay_ms() is blocking. What that means is your code does not run until delay is done.

Two things you can do - First, write your own version of _delay_ms() that also checks for a key press and exits early if that happens. Second, wrap it in your own procedure that divides the total time into smaller segments. In that you put the calls to _delay_ms() into a loop that also checks for a key press. For example:
WaitButExitOnKeypress(Milliseconds)
  NumIntervals=Milliseconds/MsPerInterval
  for NumIntervals
    _delay_ms(MsPerInterval) 
    If key pressed exit loop
  next interval
return to caller



如果将MsPerInterval设置为100,这将检查按键每秒10次并退出无论是按下按键还是时间已过期。请注意,延迟可能与您指定的不完全相同,但它足够接近,除非您为间隔使用较大的值,否则人们不会注意到差异。


If you set MsPerInterval to 100 this will check for a key press 10 times per second and exit either when a key is pressed or the time has expired. Note that the delay might not be exactly what you specified, but it will be close enough that a person won't notice the difference unless you use a large value for the interval.


这篇关于通过游戏中的按钮逃脱延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆