两段代码的kbhit()函数是一样用的,可是第一个长按后松开也是判断为按下,第二段代码就没有问题怎么回事?玄关
#include<bits/stdc++.h>
#include<windows.h>
#include<stdio.h>
#include<conio.h>
#include<time.h>
using namespace std;
void C(int wr,int wg,int wb,int br,int bg,int bb) {
printf("\033[38;2;%d;%d;%dm\033[48;2;%d;%d;%dm",wr,wg,wb,br,bg,bb);
}
void S(int x,int y)
{
COORD pos;
pos.X=y,pos.Y=x;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
struct ss{
int x,y;
};
int gs=1;
ss f[1]={{27,2}};
ss d[1]{{4,64}};
string ma[1][30]{
{
{"#####################################################################################################"},
{"# #"},
{"# #"},
{"# #"},
{"# [] #"},
{"# ######################### #"},
{"# #"},
{"# #"},
{"# ################ #"},
{"# #"},
{"# #"},
{"# ############# #"},
{"# #"},
{"# #"},
{"# #"},
{"# #"},
{"# #"},
{"# ##################### #"},
{"# #"},
{"# #"},
{"# #"},
{"# #"},
{"# #"},
{"# #"},
{"# ############ #"},
{"# #"},
{"# #"},
{"# #"},
{"# !! #"},
{"####################################################################################################"}
}
};
int bou=0;
bool bou2;
char con;
int main(){
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwInMode, dwOutMode;
GetConsoleMode(hIn, &dwInMode);
GetConsoleMode(hOut, &dwOutMode);
dwInMode |= 0x0200;
dwOutMode |= 0x0004;
SetConsoleMode(hIn, dwInMode);
SetConsoleMode(hOut, dwOutMode);
CONSOLE_CURSOR_INFO cursor_info={1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
for(int g=0;g<gs;g++){
for(int i=0;i<30;i++){
for(int j=0;j<104;j++){
if(ma[g][i][j]=='!'){
C(255,0,0,255,0,0);
}
cout<<ma[g][i][j];
if(ma[g][i][j]=='!'){
C(255,255,255,0,0,0);
}
}
cout<<endl;
}
bou=0;
bou2=0;
while(d[g].x!=f[g].x||d[g].y!=f[g].y){
//这里
if(_kbhit()){
con=getch();
}
else con=' ';
S(f[g].x,f[g].y);
cout<<' ';
if(con=='w'&&bou2==0&&bou==0){
bou2=1;
bou=0;
}
if(bou2==1&&bou<10){
if(ma[g][f[g].x-1][f[g].y]=='#'){
bou2=0;
bou=0;
}
else {
bou++;
f[g].x--;
}
}
else {
bou2=0;
bou=0;
}
if(bou2==0&&ma[g][f[g].x+1][f[g].y]!='#'){
f[g].x++;
bou2=0;
bou=1;
}
if(con=='a'&&ma[g][f[g].x][f[g].y-1]!='#'){
f[g].y--;
}
if(con=='d'&&ma[g][f[g].x][f[g].y+1]!='#'){
f[g].y++;
}
S(f[g].x,f[g].y);
cout<<'*';
Sleep(50);
S(31,0);
}
system("cls");
}
return 0;
}
#include<bits/stdc++.h>
#include<windows.h>
#include <conio.h>
#include<ctime>
using namespace std;
void S(int x,int y)
{
COORD pos;
pos.X=y,pos.Y=x;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
string ma[18]={
{"#######################################################"},
{"# #"},
{"########## #"},
{"# #"},
{"# ############### #"},
{"# #"},
{"# #"},
{"###### ############# #"},
{"# ########"},
{"# #"},
{"###################### ############### #"},
{"# #"},
{"# #"},
{"# #################### #"},
{"# #"},
{"# ####### ########### #"},
{"# #"},
{"#######################################################"}
};
int bou=0;
bool bou2;
int x=16,y=1;
char role;
char con;
int bou3=0;
bool bou23;
int x2=16,y2=53;
char role2;
int main(){
cout<<"抓捕者(字符):";
cin>>role;
system("cls");
cout<<"逃亡者(字符):";
cin>>role2;
system("cls");
for(int i=0;i<18;i++){
cout<<ma[i];
cout<<endl;
}
while(1){
clock_t over=clock();
S(20,0);
cout<<(double)over/CLOCKS_PER_SEC<<"秒\n";
if(_kbhit()){
con=getch();
}
else con=' ';
S(x,y);
cout<<' ';
S(x2,y2);
cout<<' ';
if(con=='w'&&bou==0&&bou2==0){
bou2=1;
}
if(bou2&&bou<4){
if(ma[x-1][y]=='#'){
bou=0;
bou2=0;
}
else{
bou++;
x--;
}
}
else {
bou2=0;
bou=0;
}
if(bou==4){
bou2=0;
bou=0;
}
if(bou2==0&&ma[x+1][y]==' '){
bou=1;
x++;
}
if(con=='a'&&ma[x][y-1]==' '){
y--;
}
if(con=='d'&&ma[x][y+1]==' '){
y++;
}
S(x,y);
cout<<role;
if(con=='i'&&bou3==0&&bou23==0){
bou23=1;
}
if(bou23==1&&bou3<4){
if(ma[x2-1][y2]=='#'){
bou3=3;
bou23=0;
}
else{
bou3++;
x2--;
}
}
else {
bou23=0;
bou3=0;
}
if(bou3==4){
bou3=0;
bou23=0;
}
if(bou23==0&&ma[x2+1][y2]==' '){
x2++;
bou3=1;
}
if(con=='j'&&ma[x2][y2-1]==' '){
y2--;
}
if(con=='l'&&ma[x2][y2+1]==' '){
y2++;
}
S(x2,y2);
cout<<role2;
S(12,0);
cout<<" ";
S(12,0);
Sleep(100);
if(x==x2&&y2==y){
system("cls");
cout<<"抓捕者获胜。";
cout<<"用时:" <<endl<<cout<<(double)over/CLOCKS_PER_SEC<<"秒";
return 0;
}
}
return 0;
}