Compare commits

...

2 Commits

Author SHA1 Message Date
andrea
b9bc138cad small refactoring to keep code more cleaning
and fixed a crash when the ball goes out of matrix
in any case now move_ball() function, now prevents
the ball to goes out, if ball coordinates
are out of the matrix, ball restarts from initial position
without assigning any point
2026-03-15 09:39:12 +01:00
andrea
e7f0c3cc1a optimize rerender matrix, do it only if something is changed from the last render 2026-03-14 23:47:44 +01:00

View File

@@ -42,6 +42,8 @@ int bar_length= 3;
int p1_score= 0; int p1_score= 0;
int p2_score= 0; int p2_score= 0;
int need_refresh= 1;
int loop_delay= 220; int loop_delay= 220;
long exec_t2= millis(); long exec_t2= millis();
@@ -59,6 +61,8 @@ void setup() {
} }
void render_matrix() { void render_matrix() {
if (!need_refresh) return;
need_refresh= 0;
// clear // clear
for (int x=0; x < 12; x++) { for (int x=0; x < 12; x++) {
for (int y=0; y < 8; y++) { for (int y=0; y < 8; y++) {
@@ -84,30 +88,54 @@ void render_matrix() {
void pong_move_p1() { void pong_move_p1() {
if (digitalRead(P1_BTN_UP) == LOW && p1_start > 0) { if (digitalRead(P1_BTN_UP) == LOW && p1_start > 0) {
p1_start -= 1; p1_start -= 1;
Serial.print("P1 up: "); need_refresh= 1;
Serial.println(p1_start);
} }
else if (digitalRead(P1_BTN_BOTTOM) == LOW && p1_start < 5) { else if (digitalRead(P1_BTN_BOTTOM) == LOW && p1_start < 5) {
p1_start += 1; p1_start += 1;
Serial.print("P1 down: "); need_refresh= 1;
Serial.println(p1_start);
} }
} }
void pong_move_p2() { void pong_move_p2() {
if (digitalRead(P2_BTN_UP) == LOW && p2_start > 0) { if (digitalRead(P2_BTN_UP) == LOW && p2_start > 0) {
p2_start -= 1; p2_start -= 1;
Serial.print("P2 up: "); need_refresh= 1;
Serial.println(p2_start);
} }
else if (digitalRead(P2_BTN_BOTTOM) == LOW && p2_start < 5) { else if (digitalRead(P2_BTN_BOTTOM) == LOW && p2_start < 5) {
p2_start += 1; p2_start += 1;
Serial.print("P2 down: "); need_refresh= 1;
Serial.println(p2_start);
} }
} }
int ball_player_collision(int player) {
for (int p= player; p < player + bar_length; p++) {
if (ball_y == p) {
return 1;
}
}
return 0;
}
void print_points() {
Serial.print("P1: ");
Serial.print(p1_score);
Serial.print(" - ");
Serial.print("P2: ");
Serial.print(p2_score);
Serial.println();
}
void move_ball() { void move_ball() {
need_refresh= 1;
if (ball_x < 0 || ball_x > 11 || ball_y < 0 || ball_y > 7) {
// ball out of matrix limits
ball_x= ball_reset_x;
ball_y= ball_reset_y;
return;
}
// if ball is not moving, get random direction
// this is the initial position
if (ball_move_x == 0 || ball_move_y == 0) { if (ball_move_x == 0 || ball_move_y == 0) {
// extract random number between 0 or 1 to select the directions // extract random number between 0 or 1 to select the directions
if (random(2) == 0) ball_move_x= 1; if (random(2) == 0) ball_move_x= 1;
@@ -118,51 +146,33 @@ void move_ball() {
else if (ball_x == 0) { else if (ball_x == 0) {
// if p1 collision: reverse x, go left // if p1 collision: reverse x, go left
int hit= 0; if (!ball_player_collision(p1_start)) {
for (int p1= p1_start; p1 < p1_start + bar_length; p1++) {
if (ball_y == p1) {
ball_move_x= ball_move_x * -1;
hit= 1;
break;
}
}
if (!hit) {
// else p2 score, reset board // else p2 score, reset board
ball_x= ball_reset_x; ball_x= ball_reset_x;
ball_y= ball_reset_y; ball_y= ball_reset_y;
p2_score += 1; p2_score += 1;
Serial.println("Player 2 Point");
Serial.print("Player 2 score: "); print_points();
Serial.println(p2_score); }
Serial.print("Player 1 score: "); else {
Serial.println(p1_score); ball_move_x= ball_move_x * -1;
} }
} }
else if (ball_x == 11) { else if (ball_x == 11) {
int hit= 0; if (!ball_player_collision(p2_start)) {
for (int p2= p2_start; p2 < p2_start + bar_length; p2++) {
// if p2 collision: reverse x, go left
if (ball_y == p2) {
ball_move_x= ball_move_x * -1;
hit= 1;
break;
}
}
if (!hit) {
// else p1 score, reset board // else p1 score, reset board
ball_x= ball_reset_x; ball_x= ball_reset_x;
ball_y= ball_reset_y; ball_y= ball_reset_y;
p1_score += 1; p1_score += 1;
Serial.print("Player 2: "); Serial.println("Player 1 Point");
Serial.println(p2_score); print_points();
Serial.print("Player 1: "); }
Serial.println(p1_score); else {
ball_move_x= ball_move_x * -1;
} }
} }
else if (ball_y == 0 || ball_y == 7) { if (ball_y == 0 || ball_y == 7) {
// reverse y, go down // reverse y, go down
ball_move_y= ball_move_y * -1; ball_move_y= ball_move_y * -1;
} }
@@ -180,5 +190,5 @@ void loop() {
move_ball(); move_ball();
exec_t2= exec_t1; exec_t2= exec_t1;
} }
delay(10); delay(50);
} }