You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

148 lines
2.6 KiB

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<termios.h>
int kbhit(void);
char getch(void);
/*int main(int argc, char ** argv){
printf("wasd or arrows to control snake, q to quit, p to pause game.");
while(1){
getInput();
}
}*/
/** 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
*/
void getInput(){
if(kbhit()){
char key= getch();
if(key == '\033'){
getch();
char key = getch();
switch(key){
case 'A':
printf("up");
break;
case 'B':
printf("down");
break;
case 'C':
printf("right");
break;
case 'D':
printf("left");
break;
}
}
else{
switch(key){
case 'w':
printf("W");
break;
case 'a':
printf("A");
break;
case 's':
printf("S");
break;
case 'd':
printf("D");
break;
case 'q':
printf("quit");
break;
case 'p':
printf("\033[31mgame paused, p to continue");
printf("\033[0m");
pause();
printf("\033[32mgame continued");
printf("\033[0m");
break;
}
}
}
}
/** getTInput with parameters to test if working correctly
* returns char, parameter: char
*/
char getTInput(char key){
if(key == '\033'){
return 'ar';
}
else{
switch(key){
case 'w':
return 'w';
case 'a':
return 'a';
case 's':
return 's';
case 'd':
return 'd';
case 'q':
return 'q';
case 'p':
return 'p';
}
}
}
/** pauses the game until p is pressed again
* no parameters, no returns
*/
pause(){
while(getch()!='p'){
sleep(0.10);
}
}
//khbit und getch aus dem Internet
int kbhit(void){
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
if(ch != EOF)
{
ungetc(ch, stdin);
return 1;
}
return 0;
}
char getch(void)
{
char c;
system("stty raw");
c= getchar();
system("stty sane");
return(c);
}