Improve arduino_pong.ino using enum to handle game statuses and switch case for better readibility and little memory improvements
Some checks failed
Arduino Pong CI / build (macos-latest) (push) Has been cancelled
Arduino Pong CI / build (ubuntu-latest) (push) Has been cancelled
Arduino Pong CI / build (windows-latest) (push) Has been cancelled
Arduino Pong CD / release (push) Has been cancelled

This commit is contained in:
andrea
2026-03-17 18:42:48 +01:00
parent b3f6aeb3fe
commit c774e11923
4 changed files with 68 additions and 54 deletions

View File

@@ -28,15 +28,21 @@ int players_scores[2]= {0, 0};
int ball_x= BALL_RESET_X;
int ball_y= BALL_RESET_Y;
int need_refresh= 1;
int need_refresh= true;
int ball_delay= INITIAL_BALL_DELAY;
bool game_over= false;
bool go= true;
long exec_t2= millis();
enum game_statuses : uint8_t {
TIMER,
RUN,
SCORE,
GAMEOVER,
WAIT,
};
game_statuses game_status= TIMER;
void setup() {
Serial.begin(9600);
@@ -52,56 +58,65 @@ void setup() {
}
void loop() {
for (int i = START_TIMER; i >= 0; i--) {
render_timer(frame, i);
delay(1000);
matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH);
}
long exec_t1= millis();
game_over= false;
go= true;
// delay the first ball movement
exec_t2= millis() + FIRST_START_BALL_DELAY;
while (go) {
long exec_t1= millis();
pong_move_p1(players_coords[0], need_refresh);
pong_move_p2(players_coords[1], need_refresh);
if (exec_t1 - exec_t2 > ball_delay) {
bool scored= move_ball(ball_x, ball_y, ball_delay, players_coords, players_scores, need_refresh);
if (scored) {
render_score(frame, players_scores);
matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH);
switch (game_status) {
case TIMER:
for (int i = START_TIMER; i >= 0; i--) {
render_timer(frame, i);
delay(1000);
if (players_scores[0] >= MAX_POINTS || players_scores[1] >= MAX_POINTS) {
render_winner(frame, matrix, players_scores);
need_refresh= 0;
game_over= true;
}
// delay the ball movement after score
exec_t2= millis() + FIRST_START_BALL_DELAY;
} else exec_t2= exec_t1;
}
matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH);
}
game_status= RUN;
// delay the first ball movement
exec_t2= millis() + FIRST_START_BALL_DELAY;
break;
// rerender matrix only if something is changed
if (need_refresh) {
render_matrix(frame, players_coords, ball_x, ball_y);
case RUN:
pong_move_p1(players_coords[0], need_refresh);
pong_move_p2(players_coords[1], need_refresh);
if (exec_t1 - exec_t2 > ball_delay) {
need_refresh= true;
bool scored= move_ball(ball_x, ball_y, ball_delay, players_coords, players_scores);
if (scored) {
game_status= SCORE;
// delay the ball movement after score
exec_t2= millis() + FIRST_START_BALL_DELAY;
} else exec_t2= exec_t1;
}
// rerender matrix only if something is changed
if (need_refresh) {
render_matrix(frame, players_coords, ball_x, ball_y);
matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH);
need_refresh= 0;
}
delay(50);
break;
case SCORE:
render_score(frame, players_scores);
matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH);
need_refresh= 0;
}
if (!game_over) delay(50);
delay(1000);
if (players_scores[0] >= MAX_POINTS || players_scores[1] >= MAX_POINTS) {
game_status= GAMEOVER;
}
else game_status= RUN;
break;
// keep showing the winner waiting for a restart
while (game_over) {
case GAMEOVER:
render_winner(frame, matrix, players_scores);
game_status= WAIT;
break;
case WAIT:
// keep showing the winner waiting for a restart
// restart game once one button is pressed
if (digitalRead(P1_BTN_UP) == LOW || digitalRead(P1_BTN_BOTTOM) == LOW || digitalRead(P2_BTN_UP) == LOW || digitalRead(P2_BTN_BOTTOM) == LOW) {
go= false;
game_over= false;
game_status= TIMER;
players_scores[0]= 0;
players_scores[1]= 0;
}
delay(100);
}
break;
}
}

View File

@@ -33,7 +33,7 @@ void point_scored(int &ball_x, int &ball_y, int &ball_delay, int players_scores[
ball_delay= INITIAL_BALL_DELAY;
}
bool move_ball(int &ball_x, int &ball_y, int &ball_delay, int players_coords[2], int players_scores[2], int &need_refresh) {
bool move_ball(int &ball_x, int &ball_y, int &ball_delay, int players_coords[2], int players_scores[2]) {
if (ball_x < 0 || ball_x > MATRIX_WIDTH-1 || ball_y < 0 || ball_y > MATRIX_HEIGHT-1) {
// ball out of matrix limits
ball_x= BALL_RESET_X;
@@ -41,7 +41,6 @@ bool move_ball(int &ball_x, int &ball_y, int &ball_delay, int players_coords[2],
return false;
}
need_refresh= 1;
bool scored= false;
// if ball is not moving, get random direction

View File

@@ -1,6 +1,6 @@
#ifndef PONG_BALL_H
#define PONG_BALL_H
bool move_ball(int &ball_x, int &ball_y, int &loop_delay, int players_coords[2], int players_scores[2], int &need_refresh);
bool move_ball(int &ball_x, int &ball_y, int &loop_delay, int players_coords[2], int players_scores[2]);
void foo();
#endif

View File

@@ -13,21 +13,21 @@ int ball_player_collision(int player, int ball_y) {
int pong_move_p1(int &p1_start, int &need_refresh) {
if (digitalRead(P1_BTN_UP) == LOW && p1_start > 0) {
p1_start -= 1;
need_refresh= 1;
need_refresh= true;
}
else if (digitalRead(P1_BTN_BOTTOM) == LOW && p1_start < 5) {
p1_start += 1;
need_refresh= 1;
need_refresh= true;
}
}
int pong_move_p2(int &p2_start, int &need_refresh) {
if (digitalRead(P2_BTN_UP) == LOW && p2_start > 0) {
p2_start -= 1;
need_refresh= 1;
need_refresh= true;
}
else if (digitalRead(P2_BTN_BOTTOM) == LOW && p2_start < 5) {
p2_start += 1;
need_refresh= 1;
need_refresh= true;
}
}