The Way to Programming
The Way to Programming
I have a question for you and I’m sorry if I’m wasting your time.
I made a game in C++(I’m in high school so I don’t know a lot of things).
My problem is that when I try to move my character in right it works if I press the key one time, but if I hold it , the character have a little bit of delay.
How can I remove that delay?
if(kbhit()){ a=getch(); if((a=='A' && joc[hM-1][lM]!='*' && joc[hM-1][lM]!='#') || (a=='a' && joc[hM-1][lM]!='*' && joc[hM-1][lM]!='#')){ joc[hM][lM]=' '; hM--; joc[hM][lM]=minge;} else if((a=='D' && joc[hM+1][lM]!='*' && joc[hM+1][lM]!='#') || (a=='d' && joc[hM+1][lM]!='*' && joc[hM+1][lM]!='#')){ joc[hM][lM]=' '; hM++; joc[hM][lM]=minge;}
Possible that you’re trying to update the character movement too frequently? I’m not really familiar with C++ (I’m a Java developer) so this is simply a guess Smile but I Googled a bit and it seems that you might need to check the character at a fixed interval instead of reading off the input stream for as long as you have data in the stream.
http://stackoverflow.com/questions/13613431/how-to-use-kbhit-with-delay-without-waiting
http://www.cprogramming.com/fod/kbhit.html
Sharing whole project
#include#include #include using namespace std; unsigned int i ,j , hM , lM , cron=0 , Gcron=0 , k , viata=2 , culoare , d , prevent=0 , scor , urca=20 , re; int nrtrap=0; char a , minge=257; char joc[17][14]; void gotoxy(short x, short y) { COORD pos = {x, y}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);} void draw(){ gotoxy(0,0); for(j=0;j<=13;j++){ for(i=0;i<=16;i++){ if(joc[i][j]=='_' || joc[i][j]=='-' || joc[i][j]=='"') culoare=14; else if(joc[i][j]==minge) culoare=12; else if(joc[i][j]=='*') culoare=14; else if(joc[i][j]==3) culoare=12; else if(joc[i][j]=='#') culoare=10; else if(joc[i][j]=='|' && i!=0 && i!=16) culoare=11; else if(joc[i][j]=='|') culoare=14; SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), culoare); cout< 0) || (a=='s' && sfori>0)){ prevent=0; sfori--; sf=1; while(joc[hM][lM-sf]!='*' && joc[hM][lM-sf]!='_' && joc[hM][lM-sf]!='#'){ joc[hM][lM-sf]='|'; sf++; draw();} while(joc[hM][lM-1]=='|'){ joc[hM][lM]=' '; lM--; joc[hM][lM]=minge; draw();} }}/* aci se termina */ cron++; Gcron++; if((joc[hM][lM+2]=='*' && cron==urca) || (joc[hM][lM+2]=='#' && cron==urca)) primu=1; else if(Gcron>=8 && joc[hM][lM+1]!='*'){ joc[hM][lM]=' '; lM++; joc[hM][lM]=minge; Gcron=0; scor++; if(prevent==1){ joc[hM][lM]=' '; viata--; d=n; if(capcana[n]==0){ d=n-1; if(d==0) d=4;} lM=lT[d]-1; hM=hT[d]; scor--; }} else if(joc[hM][lM+1]=='*' && cron==urca){ joc[hM][lM]=' '; lM--; joc[hM][lM]=minge;} /* urca trapele */ if(cron==urca){ cron=0; trap++; if(lV==0){ existV=0; hV=0; lV=0;} if(lSF==0){ existSF=0; hSF=0; lSF=0;} if(existV==1){ lV--; joc[hV][lV]=3;} if(existSF==1){ lSF--; joc[hSF][lSF]='|';} for(k=1;k<=4;k++){ if(existenta[k]==1){ joc[hT[k]][lT[k]]=' '; joc[hT[k]-1][lT[k]]=' '; joc[hT[k]+1][lT[k]]=' '; lT[k]--; if(capcana[k]==1){ joc[hT[k]][lT[k]]='*'; joc[hT[k]-1][lT[k]]='*'; joc[hT[k]+1][lT[k]]='*';} else if(capcana[k]==0){ joc[hT[k]][lT[k]]='#'; joc[hT[k]-1][lT[k]]='#'; joc[hT[k]+1][lT[k]]='#';} }} if(prevent==1){ viata--; d=n; if(capcana[n]==0){ d=n-1; if(d==0) d=4;} lM=lT[d]-1; hM=hT[d]; }} /* */ /* spawn trap */ if(trap==3){ nrtrap++; nrtrapC++; shansa=rand()%100+1; shansaC=rand()%100+1; if(u!=0) n++; if(n==5) n=1; existenta[n]=1; lT[n]=12; hT[n]=rand()%13+2; if(shansaC>20 || nrtrap<5){ capcana[n]=1; joc[hT[n]][lT[n]]='*'; joc[hT[n]-1][lT[n]]='*'; joc[hT[n]+1][lT[n]]='*'; } else if(nrtrapC>1) { capcana[n]=0; joc[hT[n]][lT[n]]='#'; joc[hT[n]-1][lT[n]]='#'; joc[hT[n]+1][lT[n]]='#'; nrtrapC=0;} else{ capcana[n]=1; joc[hT[n]][lT[n]]='*'; joc[hT[n]-1][lT[n]]='*'; joc[hT[n]+1][lT[n]]='*'; } if(nrtrap%15==0){ hV=hT[n]; lV=lT[n]-1; joc[hV][lV]=3; existV=1;} if(shansa<21 && shansaC>20 && nrtrap>1 && nrtrap%15!=0 && existSF!=1){ hSF=hT[n]; lSF=lT[n]-1; joc[hSF][lSF]='|'; existSF=1;} u=1; trap=0;} /* */ /* mige */ if(primu==0 && re==0){ hM=hT[1];lM=11; joc[hM][lM]=257;} /* */ if(lM==0){ viata--; d=n; if(capcana[n]==0){ d=n-1; if(d==0) d=4;} lM=lT[d]-1; hM=hT[d]; } SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), 12); gotoxy(18,0); cout<<"Vieti:"; cout<
Since you are working on windows, you should try using the GetKeyState function inside a loop that runs over a specified interval:
#include#include #include using namespace std; int main(void) { bool shouldExit = false; do{ if(GetKeyState(VK_LEFT)<0){ cout<<"left key pressed!\n"; //movePlayerToLeft(); }else if(GetKeyState(VK_RIGHT)<0){ cout<<"right key pressed!\n"; //movePlayerToRight(); }else if(GetKeyState(VK_UP)<0){ shouldExit = true; } Sleep(100); }while ( !shouldExit ); }
The above example should work.
you can change Sleep(100) so as to make the program respond faster or slower.
Sign in to your account