From 99f33da69ec53c9b8a04ff6dbc7fd9bd71ce8192 Mon Sep 17 00:00:00 2001 From: fdai8040 Date: Wed, 31 Jan 2024 16:59:24 +0000 Subject: [PATCH] refactoring:_verbesserung_aktualisierung_spielstand_und_ballposition --- src/main/c/Pong/game.c | 47 ++++++++++++++---------------------------- 1 file changed, 15 insertions(+), 32 deletions(-) diff --git a/src/main/c/Pong/game.c b/src/main/c/Pong/game.c index 6e3bc64..4f6008e 100644 --- a/src/main/c/Pong/game.c +++ b/src/main/c/Pong/game.c @@ -157,50 +157,33 @@ int main(){ } //Bewegung Ball - ball.x += ball.speedX; - ball.y += ball.speedY; + updateBallPosition(&ball); + int result = checkCollision(ball, paddle1PositionY, paddle2PositionY); - //Kollision Ball (mit Schlägern und Wänden) - if (ball.y <= 0 || ball.y >= HEIGHT - 1){ - ball.speedY = -ball.speedY; - } - if (ball.x == 1 && (ball.y >= paddle2PositionY && ball.y < paddle2PositionY + PADDLE_LENGTH)){ - ball.speedX = -ball.speedX; - } - if (ball.x == WIDTH - 1 && (ball.y >= paddle1PositionY && ball.y < paddle1PositionY + PADDLE_LENGTH)){ - ball.speedX = -ball.speedX; - } - - // Punkte zählen - if (ball.x <= 0){ - score1++; + //Aktualisiert Spielstand und Ballposition + if (result == 1) { + score2++; ball.x = WIDTH / 2; ball.y = HEIGHT / 2; ball.speedX = -ball.speedX; - } - if (ball.x >= WIDTH - 1){ - score2++; + } else if (result == 2) { + score1++; ball.x = WIDTH / 2; ball.y = HEIGHT / 2; ball.speedX = -ball.speedX; } - draw Field(paddle1PositionY, paddle2PositionY, ball, score1, score2); - - // Spielende überprüfen - if (score1 == maxScore || score2 == maxScore) { - printf("Spiel beendet!\n"); - if (score1 == maxScore) { - printf("Spieler 1 gewinnt!\n"); - } else { - printf("Spieler 2 gewinnt!\n"); - } - break; - } - + drawField(paddle1PositionY, paddle2PositionY, ball, score1, score2, 0); usleep(100000); //Verlangsamen Schleife/Spiel } + clearScreen(); + if (score1 == maxScore) { + printf("Spieler 1 gewinnt!\n"); + } else { + printf("Spieler 2 gewinnt!\n"); + } + return 0; }