From 7afa70c9d82325c4547156d86d86ffe7f253b5d3 Mon Sep 17 00:00:00 2001 From: andrea Date: Wed, 18 Mar 2026 19:12:09 +0100 Subject: [PATCH] fix ball bouncing on pad behavior now the ball bounces on pad when hit the pad, not when it reaches the edge of the "screen" --- src/engine.cpp | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index c803ffb..57c8d33 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -28,9 +28,15 @@ void Engine::run() { _ball.move(); uint8_t bx= _ball.get_x(); uint8_t by= _ball.get_y(); - - if (bx <= 0) { - if (!this -> _check_pad_ball_collision(_p1)) { + + // pad is 1 pixel far from the edge, so i need to calc this delta + if (bx <= 1) { + // score the point only if ball reached the edge + if (this -> _check_pad_ball_collision(_p1) && bx == 1) { + _ball.bounce_on_pad(); + _event= P2_COLLISION; + } + else if (bx <= 0) { // p2 scores _p2.increase_score(); Serial.println("Player 2 Scores"); @@ -38,13 +44,14 @@ void Engine::run() { _event= P2SCORE; return; } - else { - _ball.bounce_on_pad(); - _event= P2_COLLISION; - } } - else if (bx >= MATRIX_WIDTH-1) { - if (!this -> _check_pad_ball_collision(_p2)) { + else if (bx >= MATRIX_WIDTH-2) { + // score the point only if ball reached the edge + if (this -> _check_pad_ball_collision(_p2) && bx == MATRIX_WIDTH-2) { + _ball.bounce_on_pad(); + _event= P1_COLLISION; + } + else if (bx >= MATRIX_WIDTH-1) { // p1 scores _p1.increase_score(); Serial.println("Player 1 Scores"); @@ -52,10 +59,6 @@ void Engine::run() { _event= P1SCORE; return; } - else { - _ball.bounce_on_pad(); - _event= P1_COLLISION; - } } if (by == 0 || by == MATRIX_HEIGHT-1) {