first bot implementation

bot is now able to automatically move, but for now is too skilled, i need to reduce his skills
This commit is contained in:
andrea
2026-03-19 18:49:58 +01:00
parent eac8c59d96
commit 69a8fb9dc4
4 changed files with 25 additions and 5 deletions

View File

@@ -36,7 +36,8 @@ game_statuses game_status= TIMER;
Ball ball(4, 6); Ball ball(4, 6);
HumanPaddle p1(1, P1_BTN_UP, P1_BTN_BOTTOM); 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); Engine engine(p1, p2, ball, INITIAL_BALL_DELAY);
Renderer renderer(p1, p2, ball, frame, matrix); Renderer renderer(p1, p2, ball, frame, matrix);

View File

@@ -62,8 +62,10 @@ void Engine::run() {
bool Engine::control_players() { bool Engine::control_players() {
bool need_refresh= false; bool need_refresh= false;
need_refresh |= _p1.check_pad_movement(); if (_p1.is_human()) need_refresh |= _p1.check_pad_movement();
need_refresh |= _p2.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; return need_refresh;
} }

View File

@@ -39,6 +39,11 @@ bool Paddle::check_pad_movement() {
return false; return false;
} }
bool Paddle::check_pad_movement(Ball &ball) {
// redefine me
return false;
}
bool HumanPaddle::check_pad_movement() { bool HumanPaddle::check_pad_movement() {
bool need_refresh= false; bool need_refresh= false;
if (digitalRead(_pin_btn_top) == LOW) { if (digitalRead(_pin_btn_top) == LOW) {
@@ -52,5 +57,15 @@ bool HumanPaddle::check_pad_movement() {
return need_refresh; 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();
}
} }

View File

@@ -3,6 +3,7 @@
#include <Arduino.h> #include <Arduino.h>
#include "config.h" #include "config.h"
#include "ball.h"
class Paddle { class Paddle {
@@ -23,6 +24,7 @@ class Paddle {
uint8_t get_score(); uint8_t get_score();
void reset(); void reset();
virtual bool check_pad_movement(); virtual bool check_pad_movement();
virtual bool check_pad_movement(Ball &ball);
}; };
class HumanPaddle : public Paddle { class HumanPaddle : public Paddle {
@@ -45,7 +47,7 @@ class BotPaddle : public Paddle {
if (_level < 1) _level= 1; if (_level < 1) _level= 1;
if (_level > 3) _level= 3; if (_level > 3) _level= 3;
} }
bool check_pad_movement(); bool check_pad_movement(Ball &ball);
}; };
#endif #endif