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

@@ -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;
}
}