From 1e473e2d0bb1f4e2e60257dd06d122889ff7ec61 Mon Sep 17 00:00:00 2001 From: fdai8040 Date: Wed, 31 Jan 2024 13:59:23 +0000 Subject: [PATCH] =?UTF-8?q?Hinzuf=C3=BCgen=20Tastatureingabe?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/c/Pong/game.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/main/c/Pong/game.c b/src/main/c/Pong/game.c index 69d6a31..e8b68d6 100644 --- a/src/main/c/Pong/game.c +++ b/src/main/c/Pong/game.c @@ -1,4 +1,5 @@ #include +#include #define WIDTH 40 //Breite Spielfeld #define HEIGHT 20 //Höhe Spielfeld @@ -24,6 +25,36 @@ void drawField(int paddle1PositionY, int paddle2PositionY) { } } +int kbhit(void){ + struct termios oldt, newt; + int ch, 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; +} + +int getch(void) { + int ch; + + while ((ch = getchar()) == '\n'); + + return ch; +} int main(){ int paddle1PositionY = HEIGHT / 2 - PADDLE_LENGTH / 2;