From adf24983442081d455684fff024e7740034ad11e Mon Sep 17 00:00:00 2001 From: andrea Date: Sat, 14 Mar 2026 21:53:39 +0100 Subject: [PATCH] players can hit the ball --- arduino_pong.ino | 63 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/arduino_pong.ino b/arduino_pong.ino index 86a2915..2e01d01 100644 --- a/arduino_pong.ino +++ b/arduino_pong.ino @@ -127,24 +127,55 @@ void move_ball() { } else if (ball_x == 0) { - // p2 score, reset board - ball_x= ball_reset_x; - ball_y= ball_reset_y; - p2_score += 1; - Serial.print("Player 2: "); - Serial.println(p2_score); - Serial.print("Player 1: "); - Serial.println(p1_score); + // if p1 collision: reverse x, go left + int hit= 0; + for (int p1= p1_start; p1 < p1_start + bar_length; p1++) { + if (ball_y == p1) { + ball_move_x= ball_move_x * -1; + hit= 1; + break; + } + } + + if (!hit) { + // else p2 score, reset board + ball_x= ball_reset_x; + ball_y= ball_reset_y; + p2_score += 1; + + Serial.print("Player 2 score: "); + Serial.println(p2_score); + Serial.print("Player 1 score: "); + Serial.println(p1_score); + } + else { + ball_x += 1; + } } else if (ball_x == 11) { - // p1 score, reset board - ball_x= ball_reset_x; - ball_y= ball_reset_y; - p1_score += 1; - Serial.print("Player 2: "); - Serial.println(p2_score); - Serial.print("Player 1: "); - Serial.println(p1_score); + int hit= 0; + for (int p2= p2_start; p2 < p2_start + bar_length; p2++) { + // if p2 collision: reverse x, go left + if (ball_y == p2) { + ball_move_x= ball_move_x * -1; + hit= 1; + break; + } + } + + if (!hit) { + // else p1 score, reset board + ball_x= ball_reset_x; + ball_y= ball_reset_y; + p1_score += 1; + Serial.print("Player 2: "); + Serial.println(p2_score); + Serial.print("Player 1: "); + Serial.println(p1_score); + } + else { + ball_x -= 1; + } } else if (ball_y == 0 || ball_y == 7) {