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