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.

149 lines
2.6 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include<stdio.h>
  2. #include<ctype.h>
  3. #include<stdlib.h>
  4. #include<unistd.h>
  5. #include<fcntl.h>
  6. #include<termios.h>
  7. int kbhit(void);
  8. char getch(void);
  9. void Pausieren();
  10. /*int main(int argc, char ** argv){
  11. printf("wasd or arrows to control snake, q to quit, p to pause game.");
  12. while(1){
  13. getInput();
  14. }
  15. }*/
  16. /** getInput gets the userinput and reacts to input of w,a,s,d, arrow keys, p or q
  17. * utilises getch() and kbhit, no parameters, no returns
  18. */
  19. char getInput(){
  20. if(kbhit()){
  21. char key= getch();
  22. if(key == '\033'){
  23. getch();
  24. char key = getch();
  25. switch(key){
  26. case 'A':
  27. return 'u';
  28. break;
  29. case 'B':
  30. return 'd';
  31. break;
  32. case 'C':
  33. return 'r';
  34. break;
  35. case 'D':
  36. return 'l';
  37. break;
  38. }
  39. }
  40. else{
  41. switch(key){
  42. case 'w':
  43. printf("W");
  44. break;
  45. case 'a':
  46. printf("A");
  47. break;
  48. case 's':
  49. printf("S");
  50. break;
  51. case 'd':
  52. printf("D");
  53. break;
  54. case 'q':
  55. printf("quit");
  56. break;
  57. case 'p':
  58. printf("\033[31mgame paused, p to continue");
  59. printf("\033[0m");
  60. Pausieren();
  61. printf("\033[32mgame continued");
  62. printf("\033[0m");
  63. break;
  64. }
  65. }
  66. }
  67. }
  68. /** getTInput with parameters to test if working correctly
  69. * returns char, parameter: char
  70. */
  71. char getTInput(char key){
  72. if(key == '\033'){
  73. return 'ar';
  74. }
  75. else{
  76. switch(key){
  77. case 'w':
  78. return 'w';
  79. case 'a':
  80. return 'a';
  81. case 's':
  82. return 's';
  83. case 'd':
  84. return 'd';
  85. case 'q':
  86. return 'q';
  87. case 'p':
  88. return 'p';
  89. }
  90. }
  91. }
  92. /** pauses the game until p is pressed again
  93. * no parameters, no returns
  94. */
  95. void Pausieren(){
  96. while(getch()!='p'){
  97. sleep(0.10);
  98. }
  99. }
  100. //khbit und getch aus dem Internet
  101. int kbhit(void){
  102. struct termios oldt, newt;
  103. int ch;
  104. int oldf;
  105. tcgetattr(STDIN_FILENO, &oldt);
  106. newt = oldt;
  107. newt.c_lflag &= ~(ICANON | ECHO);
  108. tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  109. oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
  110. fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
  111. ch = getchar();
  112. tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  113. fcntl(STDIN_FILENO, F_SETFL, oldf);
  114. if(ch != EOF)
  115. {
  116. ungetc(ch, stdin);
  117. return 1;
  118. }
  119. return 0;
  120. }
  121. char getch(void)
  122. {
  123. char c;
  124. system("stty raw");
  125. c= getchar();
  126. system("stty sane");
  127. return(c);
  128. }