From 48fef4be86f1aa176f9c1962645e57f8ca9bfa88 Mon Sep 17 00:00:00 2001 From: andrea Date: Wed, 18 Mar 2026 18:12:38 +0100 Subject: [PATCH] move some logics to loop() --- arduino_pong.ino | 18 +++++++++++++++--- src/engine.cpp | 15 +-------------- src/engine.h | 3 +-- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/arduino_pong.ino b/arduino_pong.ino index edd1678..b2495f9 100644 --- a/arduino_pong.ino +++ b/arduino_pong.ino @@ -23,6 +23,7 @@ byte frame[MATRIX_HEIGHT][MATRIX_WIDTH] = { }; int need_refresh= true; +uint8_t hits= 0; uint8_t ball_delay= INITIAL_BALL_DELAY; long exec_t2= millis(); @@ -87,13 +88,22 @@ void loop() { need_refresh= true; } if (exec_t1 - exec_t2 > ball_delay) { - engine.run(ball_delay); - need_refresh= true; + engine.run(); if (engine.get_event() == P1SCORE || engine.get_event() == P2SCORE) { game_status= SCORE; // delay the ball movement after score exec_t2= millis() + FIRST_START_BALL_DELAY + 1000; - } else exec_t2= exec_t1; + } + else if (engine.get_event() == P1_COLLISION || engine.get_event() == P2_COLLISION) { + hits++; + if (hits >= 6 && ball_delay >= 80) { + hits= 0; + ball_delay-= 20; + exec_t2= exec_t1; + } + } + else exec_t2= exec_t1; + need_refresh= true; } // rerender matrix only if something is changed if (need_refresh) { @@ -107,6 +117,8 @@ void loop() { case SCORE: render_score(frame, p1, p2); matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH); + ball.reset_position(); + ball_delay= INITIAL_BALL_DELAY; delay(1000); if (p1.get_score() >= MAX_POINTS || p2.get_score() >= MAX_POINTS) { game_status= GAMEOVER; diff --git a/src/engine.cpp b/src/engine.cpp index ca81ebe..c803ffb 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -23,7 +23,7 @@ void Engine::_print_score() { Serial.println(); } -void Engine::run(uint8_t &ball_delay) { +void Engine::run() { _event= NONE; _ball.move(); uint8_t bx= _ball.get_x(); @@ -33,15 +33,12 @@ void Engine::run(uint8_t &ball_delay) { if (!this -> _check_pad_ball_collision(_p1)) { // p2 scores _p2.increase_score(); - _ball.reset_position(); // XXX this is probably too early i reset the position before render - ball_delay= INITIAL_BALL_DELAY; Serial.println("Player 2 Scores"); this -> _print_score(); _event= P2SCORE; return; } else { - _hits += 1; _ball.bounce_on_pad(); _event= P2_COLLISION; } @@ -50,15 +47,12 @@ void Engine::run(uint8_t &ball_delay) { if (!this -> _check_pad_ball_collision(_p2)) { // p1 scores _p1.increase_score(); - _ball.reset_position(); // XXX this is probably too early i reset the position before render - ball_delay= INITIAL_BALL_DELAY; Serial.println("Player 1 Scores"); this -> _print_score(); _event= P1SCORE; return; } else { - _hits += 1; _ball.bounce_on_pad(); _event= P1_COLLISION; } @@ -68,13 +62,6 @@ void Engine::run(uint8_t &ball_delay) { _ball.bounce_on_sides(); _event= WALL_COLLISION; } - - // increase ball speed every 6 hits on pads - // if ball is not at max speed - if (_hits >= 6 && ball_delay >= 80) { - _hits= 0; - ball_delay-= 20; // XXX handle it on loop() - } } EngineEvents Engine::get_event() { diff --git a/src/engine.h b/src/engine.h index 05f3ea6..e4c87e9 100644 --- a/src/engine.h +++ b/src/engine.h @@ -14,7 +14,6 @@ class Engine { Paddle& _p1; Paddle& _p2; Ball& _ball; - uint8_t _hits; EngineEvents _event= NONE; bool _check_pad_ball_collision(Paddle &p); @@ -25,7 +24,7 @@ class Engine { Engine(Paddle &p_one, Paddle &p_two, Ball &ball) : _p1(p_one), _p2(p_two), _ball(ball) {} - void run(uint8_t &ball_delay); + void run(); EngineEvents get_event(); void reset(); };