From cef9f09c792bd0eb13f259b028d0f34afedb85a7 Mon Sep 17 00:00:00 2001 From: andrea Date: Sat, 21 Mar 2026 11:19:40 +0100 Subject: [PATCH] render visual menu on led matrix to easly allow user choose the game modes --- arduino_pong.ino | 48 +++++++++++++++++++++++++++++++++++------------- src/font.cpp | 8 ++++---- src/font.h | 8 ++++---- 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/arduino_pong.ino b/arduino_pong.ino index 5b2e41f..42b3332 100644 --- a/arduino_pong.ino +++ b/arduino_pong.ino @@ -35,6 +35,10 @@ enum game_statuses : uint8_t { }; game_statuses game_status= MENU; +enum game_modes : uint8_t {PVP, PVC, CVC}; +game_modes game_mode = PVP; + + Ball ball(4, 6); Paddle* p1= nullptr; @@ -44,6 +48,10 @@ HumanPaddle human_pad2(4, P2_BTN_UP, P2_BTN_BOTTOM); BotPaddle bot_pad1(1, 0, 2); BotPaddle bot_pad2(4, MATRIX_WIDTH-1, 2); +uint8_t current_gmode_idx= 0; +bool update_menu= 1; +bool mode_selected= 0; + Engine engine(ball, INITIAL_BALL_DELAY); Renderer renderer(ball, frame, matrix); @@ -65,35 +73,49 @@ void loop() { switch (game_status) { - case MENU: - // show menu on the matrix - // matrix.renderBitmap(pvp_frame, MATRIX_HEIGHT, MATRIX_WIDTH); + case MENU: { + if (digitalRead(P2_BTN_BOTTOM) == LOW && current_gmode_idx < sizeof(frame_gmodes)/sizeof(frame_gmodes[0]) -1) { + current_gmode_idx += 1; + update_menu= true; + } + else if (digitalRead(P2_BTN_UP) == LOW && current_gmode_idx > 0) { + update_menu= true; + current_gmode_idx -= 1; + } + // 1. P vs P - if (digitalRead(P1_BTN_UP) == LOW) { + else if (digitalRead(P1_BTN_UP) == LOW || digitalRead(P1_BTN_BOTTOM) == LOW && game_modes(current_gmode_idx) == PVP) { p1= &human_pad1; p2= &human_pad2; - engine.set_players(p1, p2); - renderer.set_players(p1, p2); - game_status= TIMER; + mode_selected= true; } // 2. P vs CPU - else if (digitalRead(P1_BTN_BOTTOM) == LOW) { + else if (digitalRead(P1_BTN_UP) == LOW || digitalRead(P1_BTN_BOTTOM) == LOW && game_modes(current_gmode_idx) == PVC) { p1= &human_pad1; p2= &bot_pad2; - engine.set_players(p1, p2); - renderer.set_players(p1, p2); - game_status= TIMER; + mode_selected= true; } // 3. CPU vs CPU - else if (digitalRead(P2_BTN_UP) == LOW) { + else if (digitalRead(P1_BTN_UP) == LOW || digitalRead(P1_BTN_BOTTOM) == LOW && game_modes(current_gmode_idx) == CVC) { p1= &bot_pad1; p2= &bot_pad2; + mode_selected= true; + } + + if (update_menu) { + // show menu on the matrix + const byte (*current_gmode)[12]= frame_gmodes[current_gmode_idx]; + matrix.loadPixels((uint8_t*)current_gmode, MATRIX_HEIGHT * MATRIX_WIDTH); + update_menu= false; + delay(300); + } + else if (mode_selected) { engine.set_players(p1, p2); renderer.set_players(p1, p2); game_status= TIMER; } - // slideshow menu break; + } case TIMER: for (int i = START_TIMER; i >= 0; i--) { diff --git a/src/font.cpp b/src/font.cpp index 78e26c4..e082bc8 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -178,7 +178,7 @@ const byte font_pong[10][8][3] = { }, }; -const byte pvp_frame[MATRIX_HEIGHT][MATRIX_WIDTH] = { +const byte frame_pvp[MATRIX_HEIGHT][MATRIX_WIDTH] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0 }, { 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0 }, @@ -189,7 +189,7 @@ const byte pvp_frame[MATRIX_HEIGHT][MATRIX_WIDTH] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; -const byte pvc_frame[MATRIX_HEIGHT][MATRIX_WIDTH] = { +const byte frame_pvc[MATRIX_HEIGHT][MATRIX_WIDTH] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0 }, { 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0 }, @@ -200,7 +200,7 @@ const byte pvc_frame[MATRIX_HEIGHT][MATRIX_WIDTH] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; -const byte cvc_frame[MATRIX_HEIGHT][MATRIX_WIDTH] = { +const byte frame_cvc[MATRIX_HEIGHT][MATRIX_WIDTH] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0 }, { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 }, @@ -211,4 +211,4 @@ const byte cvc_frame[MATRIX_HEIGHT][MATRIX_WIDTH] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; -const byte (*game_modes[3])[12]= {pvp_frame, pvc_frame, cvc_frame}; +const byte (*frame_gmodes[3])[12]= {frame_pvp, frame_pvc, frame_cvc}; diff --git a/src/font.h b/src/font.h index 8d8b65c..a805269 100644 --- a/src/font.h +++ b/src/font.h @@ -8,9 +8,9 @@ extern const uint32_t pone_wins[5][4]; extern const uint32_t ptwo_wins[5][4]; extern const byte font_pong[10][8][3]; -extern const byte pvp_frame[MATRIX_HEIGHT][MATRIX_WIDTH]; -extern const byte pvc_frame[MATRIX_HEIGHT][MATRIX_WIDTH]; -extern const byte cvc_frame[MATRIX_HEIGHT][MATRIX_WIDTH]; -extern const byte (*game_modes[3])[MATRIX_WIDTH]; +extern const byte frame_pvp[MATRIX_HEIGHT][MATRIX_WIDTH]; +extern const byte frame_pvc[MATRIX_HEIGHT][MATRIX_WIDTH]; +extern const byte frame_cvc[MATRIX_HEIGHT][MATRIX_WIDTH]; +extern const byte (*frame_gmodes[3])[12]; #endif