From c774e11923818e7469d65923048d88e4cafa8953 Mon Sep 17 00:00:00 2001 From: andrea Date: Tue, 17 Mar 2026 18:42:48 +0100 Subject: [PATCH] Improve arduino_pong.ino using enum to handle game statuses and switch case for better readibility and little memory improvements --- arduino_pong.ino | 107 +++++++++++++++++++++++++------------------- src/pong_ball.cpp | 3 +- src/pong_ball.h | 4 +- src/pong_player.cpp | 8 ++-- 4 files changed, 68 insertions(+), 54 deletions(-) diff --git a/arduino_pong.ino b/arduino_pong.ino index a8cb22b..03d7406 100644 --- a/arduino_pong.ino +++ b/arduino_pong.ino @@ -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; - } - - // 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); + } + game_status= RUN; + // delay the first ball movement + exec_t2= millis() + FIRST_START_BALL_DELAY; + break; + + 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); - - // keep showing the winner waiting for a restart - while (game_over) { + delay(1000); + if (players_scores[0] >= MAX_POINTS || players_scores[1] >= MAX_POINTS) { + game_status= GAMEOVER; + } + else game_status= RUN; + break; + + 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; } } diff --git a/src/pong_ball.cpp b/src/pong_ball.cpp index 137bccc..1b86829 100644 --- a/src/pong_ball.cpp +++ b/src/pong_ball.cpp @@ -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 diff --git a/src/pong_ball.h b/src/pong_ball.h index 4c236ea..37a0e0f 100644 --- a/src/pong_ball.h +++ b/src/pong_ball.h @@ -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 diff --git a/src/pong_player.cpp b/src/pong_player.cpp index 3287e1e..e3238e8 100644 --- a/src/pong_player.cpp +++ b/src/pong_player.cpp @@ -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; } }