【题目】一个启动键、一个打击键,数码管4位,显示数字从0循环显示到99, 每0.5秒增加一个数字,中间随时按下按键,按下按键时若恰好循环数字尾数为3和4,每成功一次计1分,1次循环结束,分值大于10分则显示 (C 分值 倒C) 表示胜利,否则显示FALE表示失败。
【代码】基本实现题目需要,但是碰到一个问题:按键缓存影响计数,如何解决?
#include<stdio.h>
#include<conio.h>
#include<windows.h>
void gotoXY(int x, int y)
{
COORD coord = {(short)x, (short)y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void saymsg(int x, int y, char*s)
{
gotoXY(x,y);
printf("%s",s);
}
void display(int x, int y, int n)
{
char num[16][6][6] = {
{{" ___ "},{"| |"},{"| |"},{"| |"},{"|___|"}},
{{" "},{" |"},{" |"},{" |"},{" |"}},
{{" ___ "},{" |"},{" ___|"},{"| "},{"|___ "}},
{{" ___ "},{" |"},{" ___|"},{" |"},{" ___|"}},
{{" "},{"| |"},{"|___|"},{" |"},{" |"}},
{{" ___ "},{"| "},{"|___ "},{" |"},{" ___|"}},
{{" ___ "},{"| "},{"|___ "},{"| |"},{"|___|"}},
{{" ___ "},{" |"},{" |"},{" |"},{" |"}},
{{" ___ "},{"| |"},{"|___|"},{"| |"},{"|___|"}},
{{" ___ "},{"| |"},{"|___|"},{" |"},{" ___|"}},
{{" ___ "},{"| "},{"| "},{"| "},{"|___ "}},
{{" ___ "},{" |"},{" |"},{" |"},{" ___|"}},
{{" ___ "},{"| "},{"|___ "},{"| "},{"| "}},
{{" ___ "},{"| |"},{"|___|"},{"| |"},{"| |"}},
{{" "},{"| "},{"| "},{"| "},{"|___ "}},
{{" ___ "},{"| "},{"|___ "},{"| "},{"|___ "}},
};
for (int i=0;i<5;i++)
saymsg(x,y+i,num[n][i]);
}
int main()
{
int x = 30, y = 8, key;
int scores = 0;
display(x,y,10);
display(x+6,y,0);
display(x+12,y,0);
display(x+18,y,11);
saymsg(x,y+6,(char*)"回车键开始,空格键计数");
while (1){
if (kbhit()){
key = getch();
if (key==13)
break;
}
}
for (int i=0;i<10;i++){
display(x+6,y,i);
for (int j=0;j<10;j++){
display(x+12,y,j);
if (kbhit()) {
key = getch();
fflush(stdin);
//按键缓存影响计数如何解决?
if(key==32 && (j==3||j==4))
scores++;
key = 0;
//以下两行测试用
gotoXY(x,y+9);
printf("%d",scores);
}
Sleep(500);
}
}
if (scores>10){
display(x+6,y,11);
display(x+12,y,11);
saymsg(x,y+9,(char*)"Victory!\n");
}
else{
for (int i=0;i<4;i++)
display(x+i*6,y,i+12);
saymsg(x,y+9,(char*)"FAIL\n");
}
return 0;
}
关于清理按键缓存除了用
fflush(stdin);
还试了:
rewind(stdin);
setbuf(stdin, NULL);
甚至还试了中断:
union REGS regs;
regs.h.ah = 0x0c;
regs.h.al = 0;
intdos(®s, ®s);
好像都不管用,试了很多个办法都没成功,经过的达人来指点一下吧。
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://hannyang.blog.csdn.net/article/details/125238660
内容来源于网络,如有侵权,请联系作者删除!