W801学习笔记十八:古诗学习应用——中

现在我们加入交互逻辑------对用户选择的判断。

1、定义游戏的相关变量,如记录正确和错误的数量,运行时间等等。这些都可以作为游戏应用的私有属性。

cpp 复制代码
		u8 isFinished=0;
		u16 correntCount =0;
		u16 wrongCount = 0;
		u32 totalTime=0;

2、处理交互。

根据前边定义过的框架,主线程会不断执行页面的tick方法,并且传递两次执行的间隔。

于是,我们来定义各个按键的逻辑:

i。退出按钮,最先进行判断。按下时退出。

ii。选择按钮,只有当游戏已经结束时有效,按下时重新开始游戏。

iii。ABCD按钮,判断是否选择正确。

iv。up/down 诗词可能很长,通过上下按钮进行滚动。

cpp 复制代码
SceanResult YuWenTS::tick(u32 ticks){
	if(KEY_EXIT) {
		printf("goto top menu from About.\n");
		return SceanResult_EXIT;
	}
	if(isFinished){
		if(KEY_SEL) {
			start();
			return SceanResult_Done;
		}
	
	}else{
		totalTime+=ticks;
		showTime();


			if(checkFinish()){
				return SceanResult_Done;
			}
		
		if(KEY_A | KEY_B | KEY_C | KEY_D){
			if(KEY_A && currentQuestion->ans == 0){
				correct();
				return SceanResult_Done;
			}
			if(KEY_B && currentQuestion->ans == 1){
				correct();
				return SceanResult_Done;
			}
			if(KEY_C && currentQuestion->ans == 2){
				correct();
				return SceanResult_Done;
			}
			if(KEY_D && currentQuestion->ans == 3){
				correct();
				return SceanResult_Done;
			}
			wrong();
		}

		if(KEY_UP && boxHeight> TangshiBoxHeight){
			int oy = offsetY;
			if(offsetY<0 ){
				offsetY +=TangshiBoxLineHeight;
			}else{
				offsetY =0;
			}
			if(oy !=offsetY )
			showTangshi();
		}
		
		if(KEY_DOWN && boxHeight> TangshiBoxHeight){
			int oy = offsetY;
			if(offsetY > TangshiBoxHeight - boxHeight)	{
				offsetY -=TangshiBoxLineHeight;
			}else{
				offsetY = TangshiBoxHeight - boxHeight;
			}
			if(oy !=offsetY )
			showTangshi();
			
		}
		
		ran_max(10);
	}
	return SceanResult_Done;
}

3、对游戏结束的判断。我们设定3分钟结束游戏。

cpp 复制代码
u8 checkFinish(){
		if(totalTime > 180000) {
			finish();
			return 1;
		}

	return 0;
}

4、回答正确时,计数器++,进入下一题。

cpp 复制代码
void YuWenTS::correct(){
	correntCount++;
	showScore();

		createTSQuestion();
		showTSQuetion();
		showTangshi();

}

5、回答错误时,计数器++,显示正确答案,延时,然后进入下一题

cpp 复制代码
void YuWenTS::wrong(){
	wrongCount++;
	showScore();
	showTSAnswer();
	errorDelay(3);

		createTSQuestion();
		showTSQuetion();
		showTangshi();	

}
cpp 复制代码
void YuWenTS::showTSAnswer(){
	Display_String(answerX, answerY, &optionMiss, dataLine(answerIdx));
	for(int i=0;i<4;i++)
	if(currentQuestion->ans !=i)
		Display_Fill_Rectangle2(answerLocX, answerLocY[i]-2, SCREEN_WIDTH - answerLocX, 21 ,BLACK);	
}

6、显示得分。

这里,我们设定答对一题得60分,答错扣20分。这样,如果随便乱选,最终趋于0分。

cpp 复制代码
void finish(){
	isFinished = 1;
	show_status_info(controlInfo);
	optionDeCount.backColor = DBLUE;
	Display_String(Prepare_LOC, &optionDeCount, "   挑 战 结 束   ");
}


void showScore()
{
	int score = correntCount* 60 - wrongCount * 20;
	if(score<0 ) score =0;
	Display_String2(10, 5, &optionScore, "得分: %06d ", score);
	Display_String2(150, 5, &optionCorrentCount, "正确: %04d ", correntCount);
	Display_String2(270, 5, &optionWrongCount, "错误: %04d ", wrongCount);
}

void showTime()
{
	if(lastTotalTime == totalTime/1000){
		return;
	}
	lastTotalTime = totalTime/1000;
		
	if(winMode == 2)
		Display_String2(400, 5, &optionTime, "%02d:%02d", ((180000- totalTime) / 1000) / 60, ((180000- totalTime) / 1000) % 60);
	else
		Display_String2(400, 5, &optionTime, "%02d:%02d", (totalTime / 1000) / 60, (totalTime / 1000) % 60);	
}

现在来看看效果:

W801学习笔记十八:古诗学习应用------中

W801学习笔记十七:古诗学习应用------上

相关推荐
whoarethenext14 分钟前
C/C++ OpenCV 矩阵运算
c语言·c++·opencv·矩阵运算
@老蝴17 分钟前
C语言 — 动态内存管理
android·c语言·开发语言
凤年徐1 小时前
【数据结构初阶】顺序表的应用
c语言·开发语言·数据结构·c++·笔记·算法·顺序表
C_Liu_4 小时前
C语言:字符函数和字符串函数
c语言·开发语言
iCxhust5 小时前
Deepseek给出的8255显示例程
c语言·开发语言·c++·单片机·嵌入式硬件
小智学长 | 嵌入式5 小时前
SOC-ESP32S3部分:28-BLE低功耗蓝牙
网络·单片机·esp32
qq_25929724735 小时前
文件IO流
linux·c语言·物联网
6v6-博客6 小时前
悟饭游戏厅iOS版疑似流出:未测试版
游戏机
czxyvX7 小时前
06-排序
c语言·数据结构·算法·排序算法
逼子格7 小时前
硬件工程师笔记——555定时器应用Multisim电路仿真实验汇总
笔记·单片机·嵌入式硬件·multisim·电路仿真·信号发生器·555定时器