move some logics to loop()

This commit is contained in:
andrea
2026-03-18 18:12:38 +01:00
parent 48c2610561
commit 48fef4be86
3 changed files with 17 additions and 19 deletions

View File

@@ -23,6 +23,7 @@ byte frame[MATRIX_HEIGHT][MATRIX_WIDTH] = {
}; };
int need_refresh= true; int need_refresh= true;
uint8_t hits= 0;
uint8_t ball_delay= INITIAL_BALL_DELAY; uint8_t ball_delay= INITIAL_BALL_DELAY;
long exec_t2= millis(); long exec_t2= millis();
@@ -87,13 +88,22 @@ void loop() {
need_refresh= true; need_refresh= true;
} }
if (exec_t1 - exec_t2 > ball_delay) { if (exec_t1 - exec_t2 > ball_delay) {
engine.run(ball_delay); engine.run();
need_refresh= true;
if (engine.get_event() == P1SCORE || engine.get_event() == P2SCORE) { if (engine.get_event() == P1SCORE || engine.get_event() == P2SCORE) {
game_status= SCORE; game_status= SCORE;
// delay the ball movement after score // delay the ball movement after score
exec_t2= millis() + FIRST_START_BALL_DELAY + 1000; 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 // rerender matrix only if something is changed
if (need_refresh) { if (need_refresh) {
@@ -107,6 +117,8 @@ void loop() {
case SCORE: case SCORE:
render_score(frame, p1, p2); render_score(frame, p1, p2);
matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH); matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH);
ball.reset_position();
ball_delay= INITIAL_BALL_DELAY;
delay(1000); delay(1000);
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; game_status= GAMEOVER;

View File

@@ -23,7 +23,7 @@ void Engine::_print_score() {
Serial.println(); Serial.println();
} }
void Engine::run(uint8_t &ball_delay) { void Engine::run() {
_event= NONE; _event= NONE;
_ball.move(); _ball.move();
uint8_t bx= _ball.get_x(); uint8_t bx= _ball.get_x();
@@ -33,15 +33,12 @@ void Engine::run(uint8_t &ball_delay) {
if (!this -> _check_pad_ball_collision(_p1)) { if (!this -> _check_pad_ball_collision(_p1)) {
// p2 scores // p2 scores
_p2.increase_score(); _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"); Serial.println("Player 2 Scores");
this -> _print_score(); this -> _print_score();
_event= P2SCORE; _event= P2SCORE;
return; return;
} }
else { else {
_hits += 1;
_ball.bounce_on_pad(); _ball.bounce_on_pad();
_event= P2_COLLISION; _event= P2_COLLISION;
} }
@@ -50,15 +47,12 @@ void Engine::run(uint8_t &ball_delay) {
if (!this -> _check_pad_ball_collision(_p2)) { if (!this -> _check_pad_ball_collision(_p2)) {
// p1 scores // p1 scores
_p1.increase_score(); _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"); Serial.println("Player 1 Scores");
this -> _print_score(); this -> _print_score();
_event= P1SCORE; _event= P1SCORE;
return; return;
} }
else { else {
_hits += 1;
_ball.bounce_on_pad(); _ball.bounce_on_pad();
_event= P1_COLLISION; _event= P1_COLLISION;
} }
@@ -68,13 +62,6 @@ void Engine::run(uint8_t &ball_delay) {
_ball.bounce_on_sides(); _ball.bounce_on_sides();
_event= WALL_COLLISION; _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() { EngineEvents Engine::get_event() {

View File

@@ -14,7 +14,6 @@ class Engine {
Paddle& _p1; Paddle& _p1;
Paddle& _p2; Paddle& _p2;
Ball& _ball; Ball& _ball;
uint8_t _hits;
EngineEvents _event= NONE; EngineEvents _event= NONE;
bool _check_pad_ball_collision(Paddle &p); bool _check_pad_ball_collision(Paddle &p);
@@ -25,7 +24,7 @@ class Engine {
Engine(Paddle &p_one, Paddle &p_two, Ball &ball) Engine(Paddle &p_one, Paddle &p_two, Ball &ball)
: _p1(p_one), _p2(p_two), _ball(ball) {} : _p1(p_one), _p2(p_two), _ball(ball) {}
void run(uint8_t &ball_delay); void run();
EngineEvents get_event(); EngineEvents get_event();
void reset(); void reset();
}; };