start implementi difficulty level, but needs some balancements

This commit is contained in:
andrea
2026-03-19 19:38:10 +01:00
parent 69a8fb9dc4
commit 2838b5fd14
3 changed files with 54 additions and 18 deletions

View File

@@ -35,9 +35,10 @@ enum game_statuses : uint8_t {
game_statuses game_status= TIMER;
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);
BotPaddle p2(4, 1);
BotPaddle p1(1, 0, 1);
BotPaddle p2(4, MATRIX_WIDTH-1, 1);
Engine engine(p1, p2, ball, INITIAL_BALL_DELAY);
Renderer renderer(p1, p2, ball, frame, matrix);

View File

@@ -1,13 +1,13 @@
#include "paddle.h"
void Paddle::move_pad_up() {
if (_position > 0) {
_position -= 1;
if (_pos_y > 0) {
_pos_y -= 1;
}
}
void Paddle::move_pad_down() {
if (_position + _height < MATRIX_HEIGHT) {
_position += 1;
if (_pos_y + _height < MATRIX_HEIGHT) {
_pos_y += 1;
}
}
@@ -15,7 +15,7 @@ void run_paddle() {
}
uint8_t Paddle::get_position() {
return _position;
return _pos_y;
}
bool Paddle::is_human() {
@@ -43,6 +43,9 @@ bool Paddle::check_pad_movement(Ball &ball) {
// redefine me
return false;
}
uint8_t Paddle::get_skills() {
return 0;
}
bool HumanPaddle::check_pad_movement() {
bool need_refresh= false;
@@ -58,14 +61,41 @@ bool HumanPaddle::check_pad_movement() {
}
bool BotPaddle::check_pad_movement(Ball &ball) {
uint8_t y= ball.get_y();
uint8_t ball_y= ball.get_y();
int8_t ball_dir= ball.get_direction_x();
// ball is moving left and pad is on right, do not move
if (ball_dir < 0 && _pos_x > MATRIX_WIDTH / 2) return false;
// ball is moving right and pad is on left, do not move
else if (ball_dir > 0 && _pos_x < MATRIX_WIDTH / 2) return false;
uint8_t ball_x= ball.get_x();
int8_t ball_distance= ball_x - _pos_x;
if (ball_distance < 0) ball_distance *= -1;
switch (this -> get_skills()) {
case 1:
if (ball_distance > MATRIX_WIDTH / 2 - 2) return false;
break;
case 2:
if (ball_distance > MATRIX_WIDTH / 2 - 1) return false;
break;
case 3:
if (ball_distance > MATRIX_WIDTH / 2) return false;
break;
}
// 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++) {
for (uint8_t py= _pos_y; py < _pos_y+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();
if (py == ball_y) continue;
else if (_pos_y - ball_y >= 0) this -> move_pad_up();
else this -> move_pad_down();
}
}
uint8_t BotPaddle::get_skills() {
return _skills;
}

View File

@@ -2,6 +2,7 @@
#define PADDLE_H
#include <Arduino.h>
#include <cstdint>
#include "config.h"
#include "ball.h"
@@ -9,13 +10,13 @@ class Paddle {
protected:
// define player coordinates
uint8_t _position;
uint8_t _pos_y;
uint8_t _height= PADDLE_LENGTH;
uint8_t _score= 0;
bool _human;
public:
Paddle (uint8_t position, bool human) : _position(position), _human(human) {}
Paddle (uint8_t pos_y, bool human) : _pos_y(pos_y), _human(human) {}
void move_pad_up();
void move_pad_down();
uint8_t get_position();
@@ -25,6 +26,7 @@ class Paddle {
void reset();
virtual bool check_pad_movement();
virtual bool check_pad_movement(Ball &ball);
virtual uint8_t get_skills();
};
class HumanPaddle : public Paddle {
@@ -38,16 +40,19 @@ class HumanPaddle : public Paddle {
};
class BotPaddle : public Paddle {
private:
uint8_t _level; // this is the difficulty level
uint8_t _pos_x;
uint8_t _skills; // 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;
BotPaddle(uint8_t position, uint8_t pos_x, uint8_t skills)
: Paddle(position, false), _pos_x(pos_x), _skills(skills) {
if (_skills < 1) _skills= 1;
if (_skills > 3) _skills= 3;
}
bool check_pad_movement(Ball &ball);
uint8_t get_skills();
};
#endif