From d8efabd6dac6c8d4f4c2d1f2f2dcd4adb9bf9aa9 Mon Sep 17 00:00:00 2001 From: andrea Date: Tue, 17 Mar 2026 20:11:21 +0100 Subject: [PATCH 01/11] start refactoring pong entities to use classes --- src/ball.cpp | 51 +++++++++++++++++++++++++++++++++++++ src/ball.h | 21 +++++++++++++++ src/engine.cpp | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/paddle.cpp | 32 +++++++++++++++++++++++ src/paddle.h | 21 +++++++++++++++ 5 files changed, 194 insertions(+) create mode 100644 src/ball.cpp create mode 100644 src/ball.h create mode 100644 src/engine.cpp create mode 100644 src/paddle.cpp create mode 100644 src/paddle.h diff --git a/src/ball.cpp b/src/ball.cpp new file mode 100644 index 0000000..a18ecda --- /dev/null +++ b/src/ball.cpp @@ -0,0 +1,51 @@ +#include +#include +#include "config.h" + +class Ball { + + private: + + uint8_t _x, _y; + int8_t _direction_x, _direction_y; + + void _init_directions(int8_t &_direction) { + if (random(2) == 0) _direction= 1; + else _direction= -1; + } + + public: + + void move() { + if (!_direction_x) _init_directions(_direction_x); + if (!_direction_y) _init_directions(_direction_y); + + if (_x + _direction_x >= 0 && _x + _direction_x < MATRIX_WIDTH) + _x+= _direction_x; + if (_y + _direction_y >= 0 && _y + _direction_y < MATRIX_HEIGHT) + _y+= _direction_y; + } + + void bounce_on_pad() { + _direction_x *= -1; + } + + void bounce_on_sides() { + _direction_y *= -1; + } + + void reset_position () { + _x= BALL_RESET_X; + _y= BALL_RESET_Y; + _init_directions(_direction_x); + _init_directions(_direction_y); + } + + uint8_t get_x() { + return _x; + } + + uint8_t get_y() { + return _y; + } +}; diff --git a/src/ball.h b/src/ball.h new file mode 100644 index 0000000..0987edd --- /dev/null +++ b/src/ball.h @@ -0,0 +1,21 @@ +#ifndef PLAYER_H +#define PLAYER_H + +#include +#include "config.h" + +class Ball { + private: + uint8_t _x, _y; + int8_t _direction_x, _direction_y; + + public: + void move(); + void bounce_on_pad(); + void bounce_on_sides(); + void reset_position (); + uint8_t get_x(); + uint8_t get_y(); +}; + +#endif diff --git a/src/engine.cpp b/src/engine.cpp new file mode 100644 index 0000000..bf68d2e --- /dev/null +++ b/src/engine.cpp @@ -0,0 +1,69 @@ +#include +#include "ball.h" +#include "paddle.h" +#include "config.h" + +class Engine { + + private: + Paddle& _p1; + Paddle& _p2; + Ball& _ball; + uint8_t _p1_score, _p2_score; + uint8_t _hits; + + bool _check_pad_ball_collision(Paddle &p1, Ball &ball) { + return false; + } + + void point_scored(Ball &ball) { + ball.reset_position(); + + Serial.print("P1: "); + Serial.print(_p1_score); + Serial.print(" - "); + Serial.print("P2: "); + Serial.print(_p2_score); + Serial.println(); + + _hits= 0; + // ball_delay= INITIAL_BALL_DELAY; + } + + public: + // inizialize Engine constructor, linking all args with private args + Engine(Paddle &p_one, Paddle &p_two, Ball &ball): _p1(p_one), _p2(p_two), _ball(ball) {} + + void run(uint8_t &ball_delay) { + _ball.move(); + uint8_t bx= _ball.get_x(); + uint8_t by= _ball.get_y(); + + if (bx == 0) { + if (!_check_pad_ball_collision(_p1, _ball)) { + // p2 scores + _p2_score +=1; + Serial.println("Player 2 Scores"); + } + else { + _hits += 1; + _ball.bounce_on_pad(); + } + } + else if (bx >= MATRIX_WIDTH-1) { + if (!_check_pad_ball_collision(_p2, _ball)) { + // p1 scores + _p1_score += 1; + Serial.println("Player 1 Scores"); + } + } + + if (by == 0 || by == MATRIX_HEIGHT-1) _ball.bounce_on_sides(); + + if (_hits >= 6 && ball_delay >= 80) { + // increase ball speed + _hits= 0; + ball_delay-= 20; + } + } +}; diff --git a/src/paddle.cpp b/src/paddle.cpp new file mode 100644 index 0000000..3b7c33c --- /dev/null +++ b/src/paddle.cpp @@ -0,0 +1,32 @@ +#include +#include "config.h" + +class Paddle { + + private: + // define player coordinates + uint8_t _position, _height; + bool _human; + + public: + void move_pad_up(bool &need_refresh) { + if (_position > 0) { + _position -= 1; + need_refresh= true; + } + } + void move_pad_down(bool &need_refresh) { + if (_position + _height <= MATRIX_HEIGHT) { + _position += 1; + need_refresh= true; + } + } + + uint8_t get_position() { + return _position; + } + + bool is_human() { + return _human; + } +}; diff --git a/src/paddle.h b/src/paddle.h new file mode 100644 index 0000000..3f0a927 --- /dev/null +++ b/src/paddle.h @@ -0,0 +1,21 @@ +#ifndef PADDLE_H +#define PADDLE_H + +#include +#include "config.h" + +class Paddle { + + private: + // define player coordinates + uint8_t position, height; + bool human; + + public: + void move_pad_up(bool &need_refresh); + void move_pad_down(bool &need_refresh); + uint8_t get_position(); + bool is_human(); +}; + +#endif From 3d8881ab3d1223b93de0917fed32c93b8ead1447 Mon Sep 17 00:00:00 2001 From: andrea Date: Tue, 17 Mar 2026 23:25:30 +0100 Subject: [PATCH 02/11] total reframe with classes --- arduino_pong.ino | 62 +++++++++++--------- src/ball.cpp | 73 +++++++++++------------- src/ball.h | 8 ++- src/config.h | 2 +- src/engine.cpp | 135 +++++++++++++++++++++++++------------------- src/engine.h | 33 +++++++++++ src/paddle.cpp | 54 +++++++++--------- src/paddle.h | 16 ++++-- src/pong_ball.cpp | 97 ------------------------------- src/pong_ball.h | 6 -- src/pong_player.cpp | 33 ----------- src/pong_player.h | 8 --- src/pong_render.cpp | 30 +++++----- src/pong_render.h | 8 ++- 14 files changed, 246 insertions(+), 319 deletions(-) create mode 100644 src/engine.h delete mode 100644 src/pong_ball.cpp delete mode 100644 src/pong_ball.h delete mode 100644 src/pong_player.cpp delete mode 100644 src/pong_player.h diff --git a/arduino_pong.ino b/arduino_pong.ino index 03d7406..edd1678 100644 --- a/arduino_pong.ino +++ b/arduino_pong.ino @@ -2,8 +2,10 @@ #include "src/config.h" #include "src/pong_render.h" -#include "src/pong_player.h" -#include "src/pong_ball.h" + +#include "src/paddle.h" +#include "src/ball.h" +#include "src/engine.h" // create LED matrix object ArduinoLEDMatrix matrix; @@ -20,18 +22,8 @@ byte frame[MATRIX_HEIGHT][MATRIX_WIDTH] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; -// players coordinates -int players_coords[2]= {1, 4}; -int players_scores[2]= {0, 0}; - -// initials balls coordinates -int ball_x= BALL_RESET_X; -int ball_y= BALL_RESET_Y; - int need_refresh= true; - -int ball_delay= INITIAL_BALL_DELAY; - +uint8_t ball_delay= INITIAL_BALL_DELAY; long exec_t2= millis(); enum game_statuses : uint8_t { @@ -43,6 +35,10 @@ enum game_statuses : uint8_t { }; game_statuses game_status= TIMER; +Ball ball(4, 6); +Paddle p1(1); +Paddle p2(4); +Engine engine(p1, p2, ball); void setup() { Serial.begin(9600); @@ -61,6 +57,7 @@ void loop() { long exec_t1= millis(); switch (game_status) { + case TIMER: for (int i = START_TIMER; i >= 0; i--) { render_timer(frame, i); @@ -73,38 +70,52 @@ void loop() { break; case RUN: - pong_move_p1(players_coords[0], need_refresh); - pong_move_p2(players_coords[1], need_refresh); - if (exec_t1 - exec_t2 > ball_delay) { + if (digitalRead(P1_BTN_UP) == LOW) { + p1.move_pad_up(); need_refresh= true; - bool scored= move_ball(ball_x, ball_y, ball_delay, players_coords, players_scores); - if (scored) { + } + else if (digitalRead(P1_BTN_BOTTOM) == LOW) { + p1.move_pad_down(); + need_refresh= true; + } + if (digitalRead(P2_BTN_UP) == LOW) { + p2.move_pad_up(); + need_refresh= true; + } + else if (digitalRead(P2_BTN_BOTTOM) == LOW) { + p2.move_pad_down(); + need_refresh= true; + } + if (exec_t1 - exec_t2 > ball_delay) { + engine.run(ball_delay); + need_refresh= true; + 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; + exec_t2= millis() + FIRST_START_BALL_DELAY + 1000; } else exec_t2= exec_t1; } // rerender matrix only if something is changed if (need_refresh) { - render_matrix(frame, players_coords, ball_x, ball_y); + render_matrix(frame, p1, p2, ball); matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH); - need_refresh= 0; + need_refresh= false; } delay(50); break; case SCORE: - render_score(frame, players_scores); + render_score(frame, p1, p2); matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH); delay(1000); - if (players_scores[0] >= MAX_POINTS || players_scores[1] >= MAX_POINTS) { + if (p1.get_score() >= MAX_POINTS || p2.get_score() >= MAX_POINTS) { game_status= GAMEOVER; } else game_status= RUN; break; case GAMEOVER: - render_winner(frame, matrix, players_scores); + render_winner(frame, matrix, p1, p2); game_status= WAIT; break; @@ -113,8 +124,7 @@ void loop() { // restart game once one button is pressed if (digitalRead(P1_BTN_UP) == LOW || digitalRead(P1_BTN_BOTTOM) == LOW || digitalRead(P2_BTN_UP) == LOW || digitalRead(P2_BTN_BOTTOM) == LOW) { game_status= TIMER; - players_scores[0]= 0; - players_scores[1]= 0; + engine.reset(); } delay(100); break; diff --git a/src/ball.cpp b/src/ball.cpp index a18ecda..81b61fe 100644 --- a/src/ball.cpp +++ b/src/ball.cpp @@ -1,51 +1,46 @@ #include #include #include "config.h" +#include "ball.h" -class Ball { +void Ball::_init_directions(int8_t &_direction) { + if (random(2) == 0) _direction= 1; + else _direction= -1; +} - private: +void Ball::move() { + if (!_direction_x) this -> _init_directions(_direction_x); + if (!_direction_y) this -> _init_directions(_direction_y); - uint8_t _x, _y; - int8_t _direction_x, _direction_y; + // if (_x < 0 || _x > MATRIX_WIDTH-1 || _y < 0 || _y > MATRIX_HEIGHT-1) { + // this -> reset_position(); + // } - void _init_directions(int8_t &_direction) { - if (random(2) == 0) _direction= 1; - else _direction= -1; - } + if (_x + _direction_x >= 0 && _x + _direction_x < MATRIX_WIDTH) + _x+= _direction_x; + if (_y + _direction_y >= 0 && _y + _direction_y < MATRIX_HEIGHT) + _y+= _direction_y; +} - public: +void Ball::bounce_on_pad() { + _direction_x *= -1; +} - void move() { - if (!_direction_x) _init_directions(_direction_x); - if (!_direction_y) _init_directions(_direction_y); +void Ball::bounce_on_sides() { + _direction_y *= -1; +} - if (_x + _direction_x >= 0 && _x + _direction_x < MATRIX_WIDTH) - _x+= _direction_x; - if (_y + _direction_y >= 0 && _y + _direction_y < MATRIX_HEIGHT) - _y+= _direction_y; - } +void Ball::reset_position () { + _x= BALL_RESET_X; + _y= BALL_RESET_Y; + this -> _init_directions(_direction_x); + this -> _init_directions(_direction_y); +} - void bounce_on_pad() { - _direction_x *= -1; - } +uint8_t Ball::get_x() { + return _x; +} - void bounce_on_sides() { - _direction_y *= -1; - } - - void reset_position () { - _x= BALL_RESET_X; - _y= BALL_RESET_Y; - _init_directions(_direction_x); - _init_directions(_direction_y); - } - - uint8_t get_x() { - return _x; - } - - uint8_t get_y() { - return _y; - } -}; +uint8_t Ball::get_y() { + return _y; +} diff --git a/src/ball.h b/src/ball.h index 0987edd..f358079 100644 --- a/src/ball.h +++ b/src/ball.h @@ -1,5 +1,5 @@ -#ifndef PLAYER_H -#define PLAYER_H +#ifndef BALL_H +#define BALL_H #include #include "config.h" @@ -9,7 +9,11 @@ class Ball { uint8_t _x, _y; int8_t _direction_x, _direction_y; + void _init_directions(int8_t &_direction); + public: + Ball (uint8_t _x, uint8_t _y) : _x(_x), _y(_y) {} + void move(); void bounce_on_pad(); void bounce_on_sides(); diff --git a/src/config.h b/src/config.h index 60c907f..333d4cc 100644 --- a/src/config.h +++ b/src/config.h @@ -7,7 +7,7 @@ #define MATRIX_HEIGHT 8 #define BALL_RESET_X (MATRIX_WIDTH / 2) #define BALL_RESET_Y (MATRIX_HEIGHT / 2) -#define BAR_LENGTH 3 +#define PADDLE_LENGTH 3 #define INITIAL_BALL_DELAY 200 #define MAX_POINTS 9 diff --git a/src/engine.cpp b/src/engine.cpp index bf68d2e..27a2f5f 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -2,68 +2,87 @@ #include "ball.h" #include "paddle.h" #include "config.h" +#include "engine.h" -class Engine { - - private: - Paddle& _p1; - Paddle& _p2; - Ball& _ball; - uint8_t _p1_score, _p2_score; - uint8_t _hits; - - bool _check_pad_ball_collision(Paddle &p1, Ball &ball) { - return false; +bool Engine::_check_pad_ball_collision(Paddle &p) { + uint8_t ppos= p.get_position(); + for (int p= ppos; p < ppos + PADDLE_LENGTH; p++) { + if (_ball.get_y() == p) { + return true; } + } + return false; +} - void point_scored(Ball &ball) { - ball.reset_position(); +void Engine::_print_score() { + Serial.print("P1: "); + Serial.print(_p1.get_score()); + Serial.print(" - "); + Serial.print("P2: "); + Serial.print(_p2.get_score()); + Serial.println(); +} - Serial.print("P1: "); - Serial.print(_p1_score); - Serial.print(" - "); - Serial.print("P2: "); - Serial.print(_p2_score); - Serial.println(); +void Engine::run(uint8_t &ball_delay) { + _event= NONE; + _ball.move(); + uint8_t bx= _ball.get_x(); + uint8_t by= _ball.get_y(); - _hits= 0; - // ball_delay= INITIAL_BALL_DELAY; + if (bx == 0) { + if (!this -> _check_pad_ball_collision(_p1)) { + // p2 scores + _p2.increase_score(); + _ball.reset_position(); + ball_delay= INITIAL_BALL_DELAY; + Serial.println("Player 2 Scores"); + this -> _print_score(); + _event= P2SCORE; + return; } - - public: - // inizialize Engine constructor, linking all args with private args - Engine(Paddle &p_one, Paddle &p_two, Ball &ball): _p1(p_one), _p2(p_two), _ball(ball) {} - - void run(uint8_t &ball_delay) { - _ball.move(); - uint8_t bx= _ball.get_x(); - uint8_t by= _ball.get_y(); - - if (bx == 0) { - if (!_check_pad_ball_collision(_p1, _ball)) { - // p2 scores - _p2_score +=1; - Serial.println("Player 2 Scores"); - } - else { - _hits += 1; - _ball.bounce_on_pad(); - } - } - else if (bx >= MATRIX_WIDTH-1) { - if (!_check_pad_ball_collision(_p2, _ball)) { - // p1 scores - _p1_score += 1; - Serial.println("Player 1 Scores"); - } - } - - if (by == 0 || by == MATRIX_HEIGHT-1) _ball.bounce_on_sides(); - - if (_hits >= 6 && ball_delay >= 80) { - // increase ball speed - _hits= 0; - ball_delay-= 20; - } + else { + _hits += 1; + _ball.bounce_on_pad(); + _event= P2_COLLISION; } -}; + } + else if (bx >= MATRIX_WIDTH-1) { + if (!this -> _check_pad_ball_collision(_p2)) { + // p1 scores + _p1.increase_score(); + _ball.reset_position(); + 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; + } + } + + if (by == 0 || by == MATRIX_HEIGHT-1) { + _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; + } +} + +EngineEvents Engine::get_event() { + return _event; +} + +void Engine::reset() { + _p1.reset(); + _p2.reset(); + _ball.reset_position(); +} diff --git a/src/engine.h b/src/engine.h new file mode 100644 index 0000000..05f3ea6 --- /dev/null +++ b/src/engine.h @@ -0,0 +1,33 @@ +#ifndef ENGINE_H +#define ENGINE_H + +#include +#include "ball.h" +#include "paddle.h" +#include "config.h" + +enum EngineEvents : uint8_t {NONE, P1SCORE, P2SCORE, P1_COLLISION, P2_COLLISION, WALL_COLLISION}; + +class Engine { + + private: + Paddle& _p1; + Paddle& _p2; + Ball& _ball; + uint8_t _hits; + EngineEvents _event= NONE; + + bool _check_pad_ball_collision(Paddle &p); + void _print_score(); + + public: + // inizialize Engine constructor, linking all args with private args + Engine(Paddle &p_one, Paddle &p_two, Ball &ball) + : _p1(p_one), _p2(p_two), _ball(ball) {} + + void run(uint8_t &ball_delay); + EngineEvents get_event(); + void reset(); +}; + +#endif diff --git a/src/paddle.cpp b/src/paddle.cpp index 3b7c33c..b14b516 100644 --- a/src/paddle.cpp +++ b/src/paddle.cpp @@ -1,32 +1,34 @@ -#include +#include #include "config.h" +#include "paddle.h" -class Paddle { +void Paddle::move_pad_up() { + if (_position > 0) { + _position -= 1; + } +} +void Paddle::move_pad_down() { + if (_position + _height < MATRIX_HEIGHT) { + _position += 1; + } +} - private: - // define player coordinates - uint8_t _position, _height; - bool _human; +uint8_t Paddle::get_position() { + return _position; +} - public: - void move_pad_up(bool &need_refresh) { - if (_position > 0) { - _position -= 1; - need_refresh= true; - } - } - void move_pad_down(bool &need_refresh) { - if (_position + _height <= MATRIX_HEIGHT) { - _position += 1; - need_refresh= true; - } - } +bool Paddle::is_human() { + return _human; +} - uint8_t get_position() { - return _position; - } +void Paddle::increase_score() { + if (_score <= 9) _score += 1; +} - bool is_human() { - return _human; - } -}; +uint8_t Paddle::get_score() { + return _score; +} + +void Paddle::reset() { + _score= 0; +} diff --git a/src/paddle.h b/src/paddle.h index 3f0a927..0d7318b 100644 --- a/src/paddle.h +++ b/src/paddle.h @@ -1,21 +1,27 @@ #ifndef PADDLE_H #define PADDLE_H -#include +#include #include "config.h" class Paddle { private: // define player coordinates - uint8_t position, height; - bool human; + uint8_t _position; + uint8_t _height= PADDLE_LENGTH; + uint8_t _score= 0; + bool _human= true; public: - void move_pad_up(bool &need_refresh); - void move_pad_down(bool &need_refresh); + Paddle (uint8_t _position) : _position(_position) {} + void move_pad_up(); + void move_pad_down(); uint8_t get_position(); bool is_human(); + void increase_score(); + uint8_t get_score(); + void reset(); }; #endif diff --git a/src/pong_ball.cpp b/src/pong_ball.cpp deleted file mode 100644 index 1b86829..0000000 --- a/src/pong_ball.cpp +++ /dev/null @@ -1,97 +0,0 @@ -#include -#include "config.h" -#include "pong_player.h" - -// used to increase speed when game is too easy -int hits= 0; - -// initially ball has no movements -// once game/round starts, balls gets random x and y movements -int ball_move_x= 0; -int ball_move_y= 0; - -void random_ball_movement(int &ball_move_x, int &ball_move_y) { - if (random(2) == 0) ball_move_x= 1; - else ball_move_x= -1; - if (random(2) == 0) ball_move_y= 1; - else ball_move_y= -1; -} - -void point_scored(int &ball_x, int &ball_y, int &ball_delay, int players_scores[2], int &ball_move_x, int &ball_move_y) { - ball_x= BALL_RESET_X; - ball_y= BALL_RESET_Y; - random_ball_movement(ball_move_x, ball_move_y); - - Serial.print("P1: "); - Serial.print(players_scores[0]); - Serial.print(" - "); - Serial.print("P2: "); - Serial.print(players_scores[1]); - Serial.println(); - - hits= 0; - ball_delay= INITIAL_BALL_DELAY; -} - -bool move_ball(int &ball_x, int &ball_y, int &ball_delay, int players_coords[2], int players_scores[2]) { - if (ball_x < 0 || ball_x > MATRIX_WIDTH-1 || ball_y < 0 || ball_y > MATRIX_HEIGHT-1) { - // ball out of matrix limits - ball_x= BALL_RESET_X; - ball_y= BALL_RESET_Y; - return false; - } - - bool scored= false; - - // if ball is not moving, get random direction - // this is the initial position - if (ball_move_x == 0 || ball_move_y == 0) { - // extract random number between 0 or 1 to select the directions - random_ball_movement(ball_move_x, ball_move_y); - } - - else if (ball_x == 0) { - // if p1 collision: reverse x, go left - if (!ball_player_collision(players_coords[0], ball_y)) { - // else p2 score, reset board - players_scores[1] += 1; - scored= true; - Serial.println("Player 2 Scores"); - point_scored(ball_x, ball_y, ball_delay, players_scores, ball_move_x, ball_move_y); - return true; - } - else { - hits += 1; - ball_move_x= ball_move_x * -1; - } - } - else if (ball_x == MATRIX_WIDTH-1) { - if (!ball_player_collision(players_coords[1], ball_y)) { - // else p1 score, reset board - players_scores[0] += 1; - scored= true; - Serial.println("Player 1 Scores"); - point_scored(ball_x, ball_y, ball_delay, players_scores, ball_move_x, ball_move_y); - return true; - } - else { - hits += 1; - ball_move_x= ball_move_x * -1; - } - } - - if (ball_y == 0 || ball_y == MATRIX_HEIGHT-1) { - // reverse y, go down - ball_move_y= ball_move_y * -1; - } - - if (hits >= 6 && ball_delay >= 80) { - // increase ball speed - hits= 0; - ball_delay-= 20; - } - - ball_x+= ball_move_x; - ball_y+= ball_move_y; - return scored; -} diff --git a/src/pong_ball.h b/src/pong_ball.h deleted file mode 100644 index 37a0e0f..0000000 --- a/src/pong_ball.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef PONG_BALL_H -#define PONG_BALL_H - -bool move_ball(int &ball_x, int &ball_y, int &loop_delay, int players_coords[2], int players_scores[2]); -void foo(); -#endif diff --git a/src/pong_player.cpp b/src/pong_player.cpp deleted file mode 100644 index e3238e8..0000000 --- a/src/pong_player.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include -#include "config.h" - -int ball_player_collision(int player, int ball_y) { - for (int p= player; p < player + BAR_LENGTH; p++) { - if (ball_y == p) { - return 1; - } - } - return 0; -} - -int pong_move_p1(int &p1_start, int &need_refresh) { - if (digitalRead(P1_BTN_UP) == LOW && p1_start > 0) { - p1_start -= 1; - need_refresh= true; - } - else if (digitalRead(P1_BTN_BOTTOM) == LOW && p1_start < 5) { - p1_start += 1; - need_refresh= true; - } -} - -int pong_move_p2(int &p2_start, int &need_refresh) { - if (digitalRead(P2_BTN_UP) == LOW && p2_start > 0) { - p2_start -= 1; - need_refresh= true; - } - else if (digitalRead(P2_BTN_BOTTOM) == LOW && p2_start < 5) { - p2_start += 1; - need_refresh= true; - } -} diff --git a/src/pong_player.h b/src/pong_player.h deleted file mode 100644 index 6692400..0000000 --- a/src/pong_player.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef PONG_PLAYER_H -#define PONG_PLAYER_H - -int ball_player_collision(int player, int ball_y); -int pong_move_p1(int &p1_start, int &need_refresh); -int pong_move_p2(int &p2_start, int &need_refresh); - -#endif diff --git a/src/pong_render.cpp b/src/pong_render.cpp index a32e6aa..275e955 100644 --- a/src/pong_render.cpp +++ b/src/pong_render.cpp @@ -1,6 +1,8 @@ #include #include "Arduino_LED_Matrix.h" #include "config.h" +#include "paddle.h" +#include "ball.h" #include "font.h" void clear_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH]) { @@ -11,28 +13,26 @@ void clear_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH]) { } } -void render_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int players_coords[2], int ball_x, int ball_y) { +void render_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], Paddle &p1, Paddle &p2, Ball &ball) { clear_matrix(frame); - int player_one= players_coords[0]; - int player_two= players_coords[1]; + uint8_t p1pos= p1.get_position(); + uint8_t p2pos= p2.get_position(); // players coords - for (int i= player_one; i < player_one+BAR_LENGTH; i++) { + for (int i= p1pos; i < p1pos+PADDLE_LENGTH; i++) { frame[i][0]= 1; } - for (int i= player_two; i < player_two+BAR_LENGTH; i++) { + for (int i= p2pos; i < p2pos+PADDLE_LENGTH; i++) { frame[i][MATRIX_WIDTH-1]= 1; } // ball coords - frame[ball_y][ball_x]= 1; + uint8_t bx= ball.get_x(); + uint8_t by= ball.get_y(); + frame[by][bx]= 1; } -void render_score(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int players_scores[2]) { +void render_score(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], Paddle &p1, Paddle &p2) { clear_matrix(frame); - int player_one= players_scores[0]; - int player_two= players_scores[1]; - if (player_one > 9) player_one = 9; - if (player_two > 9) player_two = 9; // player score separator (-) frame[4][5]= 1; @@ -40,12 +40,12 @@ void render_score(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int players_scores[2] for (int h=0; h < 8; h++) { for (int w=0; w < 3; w++) { - frame[h][w+1]= font_pong[player_one][h][w]; + frame[h][w+1]= font_pong[p1.get_score()][h][w]; } } for (int h=0; h < 8; h++) { for (int w=0; w < 3; w++) { - frame[h][w+8]= font_pong[player_two][h][w]; + frame[h][w+8]= font_pong[p2.get_score()][h][w]; } } } @@ -60,10 +60,10 @@ void render_timer(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int seconds) { } } -void render_winner(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], ArduinoLEDMatrix &matrix, int players_scores[2]) { +void render_winner(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], ArduinoLEDMatrix &matrix, Paddle &p1, Paddle &p2) { clear_matrix(frame); // check winner - if (players_scores[0] > players_scores[1]) { + if (p1.get_score() > p2.get_score()) { Serial.println("Player 1 wins!!!"); matrix.loadSequence(pone_wins); } diff --git a/src/pong_render.h b/src/pong_render.h index 5d935e1..2323b20 100644 --- a/src/pong_render.h +++ b/src/pong_render.h @@ -4,10 +4,12 @@ #include #include "Arduino_LED_Matrix.h" #include "config.h" +#include "paddle.h" +#include "ball.h" -void render_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int players_coords[2], int ball_x, int ball_y); -void render_score(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int players_scores[2]); +void render_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], Paddle &p1, Paddle &p2, Ball &ball); +void render_score(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], Paddle &p1, Paddle &p2); void render_timer(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int seconds); -void render_winner(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], ArduinoLEDMatrix &matrix, int players_scores[2]); +void render_winner(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], ArduinoLEDMatrix &matrix, Paddle &p1, Paddle &p2); #endif From 48c2610561bf899c8fa702b966ab9e88d16ecda1 Mon Sep 17 00:00:00 2001 From: andrea Date: Tue, 17 Mar 2026 23:43:37 +0100 Subject: [PATCH 03/11] TODO --- src/engine.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index 27a2f5f..ca81ebe 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -29,11 +29,11 @@ void Engine::run(uint8_t &ball_delay) { uint8_t bx= _ball.get_x(); uint8_t by= _ball.get_y(); - if (bx == 0) { + if (bx <= 0) { if (!this -> _check_pad_ball_collision(_p1)) { // p2 scores _p2.increase_score(); - _ball.reset_position(); + _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(); @@ -50,7 +50,7 @@ void Engine::run(uint8_t &ball_delay) { if (!this -> _check_pad_ball_collision(_p2)) { // p1 scores _p1.increase_score(); - _ball.reset_position(); + _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(); @@ -73,7 +73,7 @@ void Engine::run(uint8_t &ball_delay) { // if ball is not at max speed if (_hits >= 6 && ball_delay >= 80) { _hits= 0; - ball_delay-= 20; + ball_delay-= 20; // XXX handle it on loop() } } From 48fef4be86f1aa176f9c1962645e57f8ca9bfa88 Mon Sep 17 00:00:00 2001 From: andrea Date: Wed, 18 Mar 2026 18:12:38 +0100 Subject: [PATCH 04/11] 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(); }; From 5afa7260159a1dd5a153c9a781d363644de3477d Mon Sep 17 00:00:00 2001 From: andrea Date: Wed, 18 Mar 2026 18:24:52 +0100 Subject: [PATCH 05/11] minors --- arduino_pong.ino | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/arduino_pong.ino b/arduino_pong.ino index b2495f9..96cafb4 100644 --- a/arduino_pong.ino +++ b/arduino_pong.ino @@ -62,8 +62,8 @@ void loop() { case TIMER: for (int i = START_TIMER; i >= 0; i--) { render_timer(frame, i); - delay(1000); matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH); + delay(1000); } game_status= RUN; // delay the first ball movement @@ -111,7 +111,6 @@ void loop() { matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH); need_refresh= false; } - delay(50); break; case SCORE: @@ -138,7 +137,7 @@ void loop() { game_status= TIMER; engine.reset(); } - delay(100); break; } + delay(50); } From 792c0f835c7e85ff6b734e275d36f0c56b4b21f2 Mon Sep 17 00:00:00 2001 From: andrea Date: Wed, 18 Mar 2026 19:00:50 +0100 Subject: [PATCH 06/11] clean main sketch file --- arduino_pong.ino | 34 ++++++++++++---------------------- src/paddle.cpp | 22 ++++++++++++++++++++++ src/paddle.h | 2 ++ 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/arduino_pong.ino b/arduino_pong.ino index 96cafb4..ede77cf 100644 --- a/arduino_pong.ino +++ b/arduino_pong.ino @@ -71,38 +71,21 @@ void loop() { break; case RUN: - if (digitalRead(P1_BTN_UP) == LOW) { - p1.move_pad_up(); - need_refresh= true; - } - else if (digitalRead(P1_BTN_BOTTOM) == LOW) { - p1.move_pad_down(); - need_refresh= true; - } - if (digitalRead(P2_BTN_UP) == LOW) { - p2.move_pad_up(); - need_refresh= true; - } - else if (digitalRead(P2_BTN_BOTTOM) == LOW) { - p2.move_pad_down(); - need_refresh= true; - } + need_refresh= check_paddle_movements(p1, p2); + if (exec_t1 - exec_t2 > ball_delay) { 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 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; + exec_t2= exec_t1; need_refresh= true; } // rerender matrix only if something is changed @@ -114,15 +97,22 @@ void loop() { break; case SCORE: + delay(300); // small delay to let see the last ball position render_score(frame, p1, p2); matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH); + delay(1000); ball.reset_position(); ball_delay= INITIAL_BALL_DELAY; - delay(1000); if (p1.get_score() >= MAX_POINTS || p2.get_score() >= MAX_POINTS) { game_status= GAMEOVER; } - else game_status= RUN; + else { + game_status= RUN; + // before move again the ball wait a second + render_matrix(frame, p1, p2, ball); + matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH); + delay(1000); + } break; case GAMEOVER: diff --git a/src/paddle.cpp b/src/paddle.cpp index b14b516..2b69121 100644 --- a/src/paddle.cpp +++ b/src/paddle.cpp @@ -32,3 +32,25 @@ uint8_t Paddle::get_score() { void Paddle::reset() { _score= 0; } + +bool check_paddle_movements(Paddle &p1, Paddle &p2) { + bool need_refresh= false; + if (digitalRead(P1_BTN_UP) == LOW) { + p1.move_pad_up(); + need_refresh= true; + } + else if (digitalRead(P1_BTN_BOTTOM) == LOW) { + p1.move_pad_down(); + need_refresh= true; + } + + if (digitalRead(P2_BTN_UP) == LOW) { + p2.move_pad_up(); + need_refresh= true; + } + else if (digitalRead(P2_BTN_BOTTOM) == LOW) { + p2.move_pad_down(); + need_refresh= true; + } + return need_refresh; +} diff --git a/src/paddle.h b/src/paddle.h index 0d7318b..55cca24 100644 --- a/src/paddle.h +++ b/src/paddle.h @@ -24,4 +24,6 @@ class Paddle { void reset(); }; +bool check_paddle_movements(Paddle &p1, Paddle &p2); + #endif From 7afa70c9d82325c4547156d86d86ffe7f253b5d3 Mon Sep 17 00:00:00 2001 From: andrea Date: Wed, 18 Mar 2026 19:12:09 +0100 Subject: [PATCH 07/11] 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) { From 1f6e65c5ec3af43bdbadaf999133ce3cef346573 Mon Sep 17 00:00:00 2001 From: andrea Date: Wed, 18 Mar 2026 19:46:52 +0100 Subject: [PATCH 08/11] renderer class --- arduino_pong.ino | 24 ++++++--------- src/engine.cpp | 4 --- src/paddle.cpp | 2 -- src/pong_render.cpp | 75 --------------------------------------------- src/pong_render.h | 15 --------- 5 files changed, 10 insertions(+), 110 deletions(-) delete mode 100644 src/pong_render.cpp delete mode 100644 src/pong_render.h diff --git a/arduino_pong.ino b/arduino_pong.ino index ede77cf..d8b0943 100644 --- a/arduino_pong.ino +++ b/arduino_pong.ino @@ -1,14 +1,11 @@ #include "Arduino_LED_Matrix.h" #include "src/config.h" -#include "src/pong_render.h" - +#include "src/renderer.h" +#include "src/engine.h" #include "src/paddle.h" #include "src/ball.h" -#include "src/engine.h" -// create LED matrix object -ArduinoLEDMatrix matrix; // initial pong frame matrix byte frame[MATRIX_HEIGHT][MATRIX_WIDTH] = { @@ -22,6 +19,8 @@ byte frame[MATRIX_HEIGHT][MATRIX_WIDTH] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; +ArduinoLEDMatrix matrix; + int need_refresh= true; uint8_t hits= 0; uint8_t ball_delay= INITIAL_BALL_DELAY; @@ -40,6 +39,7 @@ Ball ball(4, 6); Paddle p1(1); Paddle p2(4); Engine engine(p1, p2, ball); +Renderer renderer(p1, p2, ball, frame, matrix); void setup() { Serial.begin(9600); @@ -61,8 +61,7 @@ void loop() { case TIMER: for (int i = START_TIMER; i >= 0; i--) { - render_timer(frame, i); - matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH); + renderer.render_timer(i); delay(1000); } game_status= RUN; @@ -90,16 +89,14 @@ void loop() { } // rerender matrix only if something is changed if (need_refresh) { - render_matrix(frame, p1, p2, ball); - matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH); + renderer.render_matrix(); need_refresh= false; } break; case SCORE: delay(300); // small delay to let see the last ball position - render_score(frame, p1, p2); - matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH); + renderer.render_score(); delay(1000); ball.reset_position(); ball_delay= INITIAL_BALL_DELAY; @@ -109,14 +106,13 @@ void loop() { else { game_status= RUN; // before move again the ball wait a second - render_matrix(frame, p1, p2, ball); - matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH); + renderer.render_matrix(); delay(1000); } break; case GAMEOVER: - render_winner(frame, matrix, p1, p2); + renderer.render_winner(); game_status= WAIT; break; diff --git a/src/engine.cpp b/src/engine.cpp index 57c8d33..b20db0f 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -1,7 +1,3 @@ -#include -#include "ball.h" -#include "paddle.h" -#include "config.h" #include "engine.h" bool Engine::_check_pad_ball_collision(Paddle &p) { diff --git a/src/paddle.cpp b/src/paddle.cpp index 2b69121..9e1ed03 100644 --- a/src/paddle.cpp +++ b/src/paddle.cpp @@ -1,5 +1,3 @@ -#include -#include "config.h" #include "paddle.h" void Paddle::move_pad_up() { diff --git a/src/pong_render.cpp b/src/pong_render.cpp deleted file mode 100644 index 275e955..0000000 --- a/src/pong_render.cpp +++ /dev/null @@ -1,75 +0,0 @@ -#include -#include "Arduino_LED_Matrix.h" -#include "config.h" -#include "paddle.h" -#include "ball.h" -#include "font.h" - -void clear_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH]) { - for (int x=0; x < MATRIX_WIDTH; x++) { - for (int y=0; y < MATRIX_HEIGHT; y++) { - frame[y][x]= 0; - } - } -} - -void render_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], Paddle &p1, Paddle &p2, Ball &ball) { - clear_matrix(frame); - uint8_t p1pos= p1.get_position(); - uint8_t p2pos= p2.get_position(); - // players coords - for (int i= p1pos; i < p1pos+PADDLE_LENGTH; i++) { - frame[i][0]= 1; - } - for (int i= p2pos; i < p2pos+PADDLE_LENGTH; i++) { - frame[i][MATRIX_WIDTH-1]= 1; - } - - // ball coords - uint8_t bx= ball.get_x(); - uint8_t by= ball.get_y(); - frame[by][bx]= 1; -} - -void render_score(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], Paddle &p1, Paddle &p2) { - clear_matrix(frame); - - // player score separator (-) - frame[4][5]= 1; - frame[4][6]= 1; - - for (int h=0; h < 8; h++) { - for (int w=0; w < 3; w++) { - frame[h][w+1]= font_pong[p1.get_score()][h][w]; - } - } - for (int h=0; h < 8; h++) { - for (int w=0; w < 3; w++) { - frame[h][w+8]= font_pong[p2.get_score()][h][w]; - } - } -} - -void render_timer(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int seconds) { - clear_matrix(frame); - - for (int h=0; h < 8; h++) { - for (int w=0; w < 3; w++) { - frame[h][w+5]= font_pong[seconds][h][w]; - } - } -} - -void render_winner(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], ArduinoLEDMatrix &matrix, Paddle &p1, Paddle &p2) { - clear_matrix(frame); - // check winner - if (p1.get_score() > p2.get_score()) { - Serial.println("Player 1 wins!!!"); - matrix.loadSequence(pone_wins); - } - else { - Serial.println("Player 2 wins!!!"); - matrix.loadSequence(ptwo_wins); - } - matrix.play(true); -} diff --git a/src/pong_render.h b/src/pong_render.h deleted file mode 100644 index 2323b20..0000000 --- a/src/pong_render.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef PONG_RENDER_H -#define PONG_RENDER_H - -#include -#include "Arduino_LED_Matrix.h" -#include "config.h" -#include "paddle.h" -#include "ball.h" - -void render_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], Paddle &p1, Paddle &p2, Ball &ball); -void render_score(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], Paddle &p1, Paddle &p2); -void render_timer(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int seconds); -void render_winner(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], ArduinoLEDMatrix &matrix, Paddle &p1, Paddle &p2); - -#endif From 33e070c4429a217e96a3bc984a5423bd3da29902 Mon Sep 17 00:00:00 2001 From: andrea Date: Wed, 18 Mar 2026 20:38:31 +0100 Subject: [PATCH 09/11] added renderer.cpp/h files --- src/renderer.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ src/renderer.h | 33 ++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 src/renderer.cpp create mode 100644 src/renderer.h diff --git a/src/renderer.cpp b/src/renderer.cpp new file mode 100644 index 0000000..775d530 --- /dev/null +++ b/src/renderer.cpp @@ -0,0 +1,72 @@ +#include "renderer.h" + + +void Renderer::_clear_matrix() { + for (int x=0; x < MATRIX_WIDTH; x++) { + for (int y=0; y < MATRIX_HEIGHT; y++) { + _frame[y][x]= 0; + } + } +} + +void Renderer::render_timer(uint8_t seconds) { + this -> _clear_matrix(); + + for (int h=0; h < 8; h++) { + for (int w=0; w < 3; w++) { + _frame[h][w+5]= font_pong[seconds][h][w]; + } + } + _matrix.renderBitmap(_frame, MATRIX_HEIGHT, MATRIX_WIDTH); +} + +void Renderer::render_matrix() { + this -> _clear_matrix(); + uint8_t p1pos= _p1.get_position(); + uint8_t p2pos= _p2.get_position(); + // players coords + for (int i= p1pos; i < p1pos+PADDLE_LENGTH; i++) { + _frame[i][0]= 1; + } + for (int i= p2pos; i < p2pos+PADDLE_LENGTH; i++) { + _frame[i][MATRIX_WIDTH-1]= 1; + } + + // ball coords + uint8_t bx= _ball.get_x(); + uint8_t by= _ball.get_y(); + _frame[by][bx]= 1; + _matrix.renderBitmap(_frame, MATRIX_HEIGHT, MATRIX_WIDTH); +} + +void Renderer::render_score() { + this -> _clear_matrix(); + _frame[4][5]= 1; + _frame[4][6]= 1; + + for (int h=0; h < 8; h++) { + for (int w=0; w < 3; w++) { + _frame[h][w+1]= font_pong[_p1.get_score()][h][w]; + } + } + for (int h=0; h < 8; h++) { + for (int w=0; w < 3; w++) { + _frame[h][w+8]= font_pong[_p2.get_score()][h][w]; + } + } + _matrix.renderBitmap(_frame, MATRIX_HEIGHT, MATRIX_WIDTH); +} + +void Renderer::render_winner() { + this -> _clear_matrix(); + // check winner + if (_p1.get_score() > _p2.get_score()) { + Serial.println("Player 1 wins!!!"); + _matrix.loadSequence(pone_wins); + } + else { + Serial.println("Player 2 wins!!!"); + _matrix.loadSequence(ptwo_wins); + } + _matrix.play(true); +} diff --git a/src/renderer.h b/src/renderer.h new file mode 100644 index 0000000..02e8a47 --- /dev/null +++ b/src/renderer.h @@ -0,0 +1,33 @@ +#ifndef RENDERER_H +#define RENDERER_H + +#include +#include "Arduino_LED_Matrix.h" +#include "config.h" +#include "font.h" +#include "paddle.h" +#include "ball.h" + +class Renderer { + + private: + // define player coordinates + Paddle& _p1; + Paddle& _p2; + Ball& _ball; + byte (&_frame)[MATRIX_HEIGHT][MATRIX_WIDTH]; + ArduinoLEDMatrix& _matrix; + + void _clear_matrix(); + + public: + Renderer (Paddle &p1, Paddle &p2, Ball &ball, byte (&frame)[MATRIX_HEIGHT][MATRIX_WIDTH], ArduinoLEDMatrix &matrix) + : _p1(p1), _p2(p2), _ball(ball), _frame(frame), _matrix(matrix) {} + + void render_timer(uint8_t seconds); + void render_matrix(); + void render_score(); + void render_winner(); +}; + +#endif From abc0ddb66105a1ab23627e3f3a7ece8987b4eaac Mon Sep 17 00:00:00 2001 From: andrea Date: Wed, 18 Mar 2026 20:49:25 +0100 Subject: [PATCH 10/11] fix collision state --- src/engine.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index b20db0f..97fc261 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -30,7 +30,7 @@ void Engine::run() { // 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; + _event= P1_COLLISION; } else if (bx <= 0) { // p2 scores @@ -45,7 +45,7 @@ void Engine::run() { // 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; + _event= P2_COLLISION; } else if (bx >= MATRIX_WIDTH-1) { // p1 scores From 49a772f6de8fd8321e2cff9561801171852d0f43 Mon Sep 17 00:00:00 2001 From: andrea Date: Wed, 18 Mar 2026 21:41:51 +0100 Subject: [PATCH 11/11] clean logs and improve engine handling by removing hits and movment_delay from the loop to the engine --- arduino_pong.ino | 25 +++++++------------------ src/ball.cpp | 10 +++++++--- src/ball.h | 4 +++- src/engine.cpp | 45 ++++++++++++++++++++++++++------------------- src/engine.h | 9 ++++++--- src/renderer.cpp | 8 ++------ 6 files changed, 51 insertions(+), 50 deletions(-) diff --git a/arduino_pong.ino b/arduino_pong.ino index d8b0943..09d317a 100644 --- a/arduino_pong.ino +++ b/arduino_pong.ino @@ -23,7 +23,6 @@ ArduinoLEDMatrix matrix; int need_refresh= true; uint8_t hits= 0; -uint8_t ball_delay= INITIAL_BALL_DELAY; long exec_t2= millis(); enum game_statuses : uint8_t { @@ -38,7 +37,7 @@ game_statuses game_status= TIMER; Ball ball(4, 6); Paddle p1(1); Paddle p2(4); -Engine engine(p1, p2, ball); +Engine engine(p1, p2, ball, INITIAL_BALL_DELAY); Renderer renderer(p1, p2, ball, frame, matrix); void setup() { @@ -72,18 +71,10 @@ void loop() { case RUN: need_refresh= check_paddle_movements(p1, p2); - if (exec_t1 - exec_t2 > ball_delay) { + if (exec_t1 - exec_t2 > engine.ball_movement_delay()) { engine.run(); - if (engine.get_event() == P1SCORE || engine.get_event() == P2SCORE) { + if (engine.get_event() == P1SCORE || engine.get_event() == P2SCORE) game_status= SCORE; - } - 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; need_refresh= true; } @@ -95,19 +86,17 @@ void loop() { break; case SCORE: - delay(300); // small delay to let see the last ball position + delay(300); renderer.render_score(); + engine.restart_ball(); delay(1000); - ball.reset_position(); - ball_delay= INITIAL_BALL_DELAY; - if (p1.get_score() >= MAX_POINTS || p2.get_score() >= MAX_POINTS) { + if (p1.get_score() >= MAX_POINTS || p2.get_score() >= MAX_POINTS) game_status= GAMEOVER; - } else { game_status= RUN; // before move again the ball wait a second renderer.render_matrix(); - delay(1000); + exec_t2= millis() + FIRST_START_BALL_DELAY; } break; diff --git a/src/ball.cpp b/src/ball.cpp index 81b61fe..b3e1d91 100644 --- a/src/ball.cpp +++ b/src/ball.cpp @@ -1,6 +1,3 @@ -#include -#include -#include "config.h" #include "ball.h" void Ball::_init_directions(int8_t &_direction) { @@ -30,6 +27,13 @@ void Ball::bounce_on_sides() { _direction_y *= -1; } +int8_t Ball::get_direction_x() { + return _direction_x; +} +int8_t Ball::get_direction_y() { + return _direction_y; +} + void Ball::reset_position () { _x= BALL_RESET_X; _y= BALL_RESET_Y; diff --git a/src/ball.h b/src/ball.h index f358079..6845e5d 100644 --- a/src/ball.h +++ b/src/ball.h @@ -1,7 +1,7 @@ #ifndef BALL_H #define BALL_H -#include +#include #include "config.h" class Ball { @@ -17,6 +17,8 @@ class Ball { void move(); void bounce_on_pad(); void bounce_on_sides(); + int8_t get_direction_x(); + int8_t get_direction_y(); void reset_position (); uint8_t get_x(); uint8_t get_y(); diff --git a/src/engine.cpp b/src/engine.cpp index 97fc261..ccb6890 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -10,48 +10,40 @@ bool Engine::_check_pad_ball_collision(Paddle &p) { return false; } -void Engine::_print_score() { - Serial.print("P1: "); - Serial.print(_p1.get_score()); - Serial.print(" - "); - Serial.print("P2: "); - Serial.print(_p2.get_score()); - Serial.println(); -} - void Engine::run() { + // if (_event == P1SCORE || _event == P2SCORE) this -> _soft_reset(); + _event= NONE; _ball.move(); uint8_t bx= _ball.get_x(); uint8_t by= _ball.get_y(); - + int8_t ball_dir= _ball.get_direction_x(); // pad is 1 pixel far from the edge, so i need to calc this delta - if (bx <= 1) { + // check also if the ball is already moving away, to skip useless checks + if (bx <= 1 && ball_dir < 0) { // score the point only if ball reached the edge - if (this -> _check_pad_ball_collision(_p1) && bx == 1) { + if (this -> _check_pad_ball_collision(_p1) && bx <= 1) { _ball.bounce_on_pad(); _event= P1_COLLISION; + _hits++; } else if (bx <= 0) { // p2 scores _p2.increase_score(); - Serial.println("Player 2 Scores"); - this -> _print_score(); _event= P2SCORE; return; } } - else if (bx >= MATRIX_WIDTH-2) { + else if (bx >= MATRIX_WIDTH-2 && ball_dir > 0) { // score the point only if ball reached the edge - if (this -> _check_pad_ball_collision(_p2) && bx == MATRIX_WIDTH-2) { + if (this -> _check_pad_ball_collision(_p2) && bx >= MATRIX_WIDTH-2) { _ball.bounce_on_pad(); _event= P2_COLLISION; + _hits++; } else if (bx >= MATRIX_WIDTH-1) { // p1 scores _p1.increase_score(); - Serial.println("Player 1 Scores"); - this -> _print_score(); _event= P1SCORE; return; } @@ -61,14 +53,29 @@ void Engine::run() { _ball.bounce_on_sides(); _event= WALL_COLLISION; } + + if (_hits >= 6 && _ball_mv_delay >= 80) { + _hits= 0; + _ball_mv_delay -= 20; + } } +uint8_t Engine::ball_movement_delay() { + return _ball_mv_delay; +} + + EngineEvents Engine::get_event() { return _event; } +void Engine::restart_ball() { + _ball.reset_position(); + _ball_mv_delay= INITIAL_BALL_DELAY; +} + void Engine::reset() { + this -> restart_ball(); _p1.reset(); _p2.reset(); - _ball.reset_position(); } diff --git a/src/engine.h b/src/engine.h index e4c87e9..61051dd 100644 --- a/src/engine.h +++ b/src/engine.h @@ -15,17 +15,20 @@ class Engine { Paddle& _p2; Ball& _ball; EngineEvents _event= NONE; + uint8_t _ball_mv_delay; + uint8_t _hits = 0; bool _check_pad_ball_collision(Paddle &p); - void _print_score(); public: // inizialize Engine constructor, linking all args with private args - Engine(Paddle &p_one, Paddle &p_two, Ball &ball) - : _p1(p_one), _p2(p_two), _ball(ball) {} + Engine(Paddle &p_one, Paddle &p_two, Ball &ball, uint8_t ball_mv_delay) + : _p1(p_one), _p2(p_two), _ball(ball), _ball_mv_delay(ball_mv_delay) {} void run(); + uint8_t ball_movement_delay(); EngineEvents get_event(); + void restart_ball(); void reset(); }; diff --git a/src/renderer.cpp b/src/renderer.cpp index 775d530..280ee9c 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -60,13 +60,9 @@ void Renderer::render_score() { void Renderer::render_winner() { this -> _clear_matrix(); // check winner - if (_p1.get_score() > _p2.get_score()) { - Serial.println("Player 1 wins!!!"); + if (_p1.get_score() > _p2.get_score()) _matrix.loadSequence(pone_wins); - } - else { - Serial.println("Player 2 wins!!!"); + else _matrix.loadSequence(ptwo_wins); - } _matrix.play(true); }