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

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