From 69a8fb9dc42604fb52ecdc6517ecf886d7c9e6f5 Mon Sep 17 00:00:00 2001 From: andrea Date: Thu, 19 Mar 2026 18:49:58 +0100 Subject: [PATCH] first bot implementation bot is now able to automatically move, but for now is too skilled, i need to reduce his skills --- arduino_pong.ino | 3 ++- src/engine.cpp | 6 ++++-- src/paddle.cpp | 17 ++++++++++++++++- src/paddle.h | 4 +++- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/arduino_pong.ino b/arduino_pong.ino index c13d304..d886e0f 100644 --- a/arduino_pong.ino +++ b/arduino_pong.ino @@ -36,7 +36,8 @@ game_statuses game_status= TIMER; Ball ball(4, 6); HumanPaddle p1(1, P1_BTN_UP, P1_BTN_BOTTOM); -HumanPaddle p2(4, P2_BTN_UP, P2_BTN_BOTTOM); +// HumanPaddle p2(4, P2_BTN_UP, P2_BTN_BOTTOM); +BotPaddle p2(4, 1); Engine engine(p1, p2, ball, INITIAL_BALL_DELAY); Renderer renderer(p1, p2, ball, frame, matrix); diff --git a/src/engine.cpp b/src/engine.cpp index ad2b557..b5cf853 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -62,8 +62,10 @@ void Engine::run() { bool Engine::control_players() { bool need_refresh= false; - need_refresh |= _p1.check_pad_movement(); - need_refresh |= _p2.check_pad_movement(); + if (_p1.is_human()) need_refresh |= _p1.check_pad_movement(); + else need_refresh |= _p1.check_pad_movement(_ball); + if (_p2.is_human()) need_refresh |= _p2.check_pad_movement(); + else need_refresh |= _p2.check_pad_movement(_ball); return need_refresh; } diff --git a/src/paddle.cpp b/src/paddle.cpp index ffccb46..e52ed1a 100644 --- a/src/paddle.cpp +++ b/src/paddle.cpp @@ -39,6 +39,11 @@ bool Paddle::check_pad_movement() { return false; } +bool Paddle::check_pad_movement(Ball &ball) { + // redefine me + return false; +} + bool HumanPaddle::check_pad_movement() { bool need_refresh= false; if (digitalRead(_pin_btn_top) == LOW) { @@ -52,5 +57,15 @@ bool HumanPaddle::check_pad_movement() { return need_refresh; } -bool BotPaddle::check_pad_movement() { +bool BotPaddle::check_pad_movement(Ball &ball) { + uint8_t y= ball.get_y(); + // TODO BotPaddle movement logics + // on higher difficult level i could also check the ball direction + // or at lover difficulty level i could also check the distance from the pad and move only when the ball si near + for (uint8_t _py= _position; _py < _position+PADDLE_LENGTH; _py++) { + // don't move if ball is already centered to the pad + if (_py == y) continue; + else if (_position - y >= 0) this -> move_pad_up(); + else this -> move_pad_down(); + } } diff --git a/src/paddle.h b/src/paddle.h index 79f5588..642da1e 100644 --- a/src/paddle.h +++ b/src/paddle.h @@ -3,6 +3,7 @@ #include #include "config.h" +#include "ball.h" class Paddle { @@ -23,6 +24,7 @@ class Paddle { uint8_t get_score(); void reset(); virtual bool check_pad_movement(); + virtual bool check_pad_movement(Ball &ball); }; class HumanPaddle : public Paddle { @@ -45,7 +47,7 @@ class BotPaddle : public Paddle { if (_level < 1) _level= 1; if (_level > 3) _level= 3; } - bool check_pad_movement(); + bool check_pad_movement(Ball &ball); }; #endif