Browse Source

refactoring:if-else for other input to switch for visibility

main
fdai7184 2 years ago
parent
commit
054f296503
  1. 30
      src/input.c

30
src/input.c

@ -8,12 +8,12 @@ int kbhit(void);
char getch(void); char getch(void);
int main(int argc, char ** argv){
/*int main(int argc, char ** argv){
printf("wasd or arrows to control snake, q to quit, p to pause game."); printf("wasd or arrows to control snake, q to quit, p to pause game.");
while(1){ while(1){
getInput(); getInput();
} }
}
}*/
/** getInput gets the userinput and reacts to input of w,a,s,d, arrow keys, p or q /** getInput gets the userinput and reacts to input of w,a,s,d, arrow keys, p or q
* utilises getch() and kbhit, no parameters, no returns * utilises getch() and kbhit, no parameters, no returns
@ -24,7 +24,6 @@ void getInput(){
if(key == '\033'){ if(key == '\033'){
getch(); getch();
char key = getch(); char key = getch();
switch(key){ switch(key){
case 'A': case 'A':
printf("up"); printf("up");
@ -41,27 +40,30 @@ void getInput(){
} }
} }
else{ else{
if(key=='w'){
switch(key){
case 'w':
printf("W"); printf("W");
}
else if(key=='a'){
break;
case 'a':
printf("A"); printf("A");
}
else if(key=='s'){
break;
case 's':
printf("S"); printf("S");
}
else if(key=='d'){
break;
case 'd':
printf("D"); printf("D");
}
else if(key=='q'){
break;
case 'q':
printf("quit"); printf("quit");
}
else if(key=='p'){
break;
case 'p':
printf("\033[31mgame paused, p to continue"); printf("\033[31mgame paused, p to continue");
printf("\033[0m"); printf("\033[0m");
pause(); pause();
printf("\033[32mgame continued"); printf("\033[32mgame continued");
printf("\033[0m"); printf("\033[0m");
break;
} }
} }

Loading…
Cancel
Save