reframe check_pad_movement logics and inherith the base Paddle class

This commit is contained in:
andrea
2026-03-19 18:26:27 +01:00
parent 68dfce8b12
commit eac8c59d96
5 changed files with 55 additions and 22 deletions

View File

@@ -21,7 +21,7 @@ byte frame[MATRIX_HEIGHT][MATRIX_WIDTH] = {
ArduinoLEDMatrix matrix;
int need_refresh= true;
bool need_refresh= true;
uint8_t hits= 0;
long exec_t2= millis();
@@ -35,8 +35,8 @@ enum game_statuses : uint8_t {
game_statuses game_status= TIMER;
Ball ball(4, 6);
Paddle p1(1);
Paddle p2(4);
HumanPaddle p1(1, P1_BTN_UP, P1_BTN_BOTTOM);
HumanPaddle p2(4, P2_BTN_UP, P2_BTN_BOTTOM);
Engine engine(p1, p2, ball, INITIAL_BALL_DELAY);
Renderer renderer(p1, p2, ball, frame, matrix);
@@ -69,7 +69,8 @@ void loop() {
break;
case RUN:
need_refresh= check_paddle_movements(p1, p2);
// need_refresh= check_paddle_movements(p1, p2);
need_refresh= engine.control_players();
if (exec_t1 - exec_t2 > engine.ball_movement_delay()) {
engine.run();

View File

@@ -60,6 +60,13 @@ void Engine::run() {
}
}
bool Engine::control_players() {
bool need_refresh= false;
need_refresh |= _p1.check_pad_movement();
need_refresh |= _p2.check_pad_movement();
return need_refresh;
}
uint8_t Engine::ball_movement_delay() {
return _ball_mv_delay;
}

View File

@@ -26,6 +26,7 @@ class Engine {
: _p1(p_one), _p2(p_two), _ball(ball), _ball_mv_delay(ball_mv_delay) {}
void run();
bool control_players();
uint8_t ball_movement_delay();
EngineEvents get_event();
void restart_ball();

View File

@@ -11,6 +11,9 @@ void Paddle::move_pad_down() {
}
}
void run_paddle() {
}
uint8_t Paddle::get_position() {
return _position;
}
@@ -31,24 +34,23 @@ 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;
}
bool Paddle::check_pad_movement() {
// redefine me
return false;
}
if (digitalRead(P2_BTN_UP) == LOW) {
p2.move_pad_up();
bool HumanPaddle::check_pad_movement() {
bool need_refresh= false;
if (digitalRead(_pin_btn_top) == LOW) {
this -> move_pad_up();
need_refresh= true;
}
else if (digitalRead(P2_BTN_BOTTOM) == LOW) {
p2.move_pad_down();
else if (digitalRead(_pin_btn_bottom) == LOW) {
this -> move_pad_down();
need_refresh= true;
}
return need_refresh;
}
bool BotPaddle::check_pad_movement() {
}

View File

@@ -6,15 +6,15 @@
class Paddle {
private:
protected:
// define player coordinates
uint8_t _position;
uint8_t _height= PADDLE_LENGTH;
uint8_t _score= 0;
bool _human= true;
bool _human;
public:
Paddle (uint8_t _position) : _position(_position) {}
Paddle (uint8_t position, bool human) : _position(position), _human(human) {}
void move_pad_up();
void move_pad_down();
uint8_t get_position();
@@ -22,8 +22,30 @@ class Paddle {
void increase_score();
uint8_t get_score();
void reset();
virtual bool check_pad_movement();
};
bool check_paddle_movements(Paddle &p1, Paddle &p2);
class HumanPaddle : public Paddle {
private:
uint8_t _pin_btn_top;
uint8_t _pin_btn_bottom;
public:
HumanPaddle(uint8_t position, uint8_t pin_btn_top, uint8_t pin_btn_bottom)
: Paddle(position, true), _pin_btn_top(pin_btn_top), _pin_btn_bottom(pin_btn_bottom) {}
bool check_pad_movement();
};
class BotPaddle : public Paddle {
private:
uint8_t _level; // this is the difficulty level
public:
BotPaddle(uint8_t position, uint8_t level)
: Paddle(position, false), _level(level) {
if (_level < 1) _level= 1;
if (_level > 3) _level= 3;
}
bool check_pad_movement();
};
#endif