move players and ball

This commit is contained in:
andrea
2026-03-14 21:35:19 +01:00
parent f2a6a02005
commit ebc6596dce
2 changed files with 103 additions and 10 deletions

View File

@@ -11,5 +11,6 @@ monitor:
arduino-cli monitor -p /dev/ttyACM0 arduino-cli monitor -p /dev/ttyACM0
run: run:
make compile
make upload make upload
make monitor make monitor

View File

@@ -13,26 +13,38 @@ byte frame[8][12] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
}; };
// players coordinates
int p1_start= 1; int p1_start= 1;
int p1_end= 3; int p1_end= 3;
int p2_start= 4; int p2_start= 4;
int p2_end= 6; int p2_end= 6;
// initials balls coordinates
int ball_reset_x=6;
int ball_reset_y=3;
int ball_x= ball_reset_x;
int ball_y= ball_reset_y;
// initially ball has no movements
// once game/round starts, balls gets random x and y movements
int ball_move_x= 0;
int ball_move_y= 0;
const uint32_t frame_game_over[] = { const uint32_t frame_game_over[] = {
0x20410809, 0x20410809,
0x600600, 0x600600,
0x90108204, 0x90108204,
}; };
int bar_length= 3;
int p1_score= 0; int p1_score= 0;
int p2_score= 0; int p2_score= 0;
@@ -53,20 +65,100 @@ void setup() {
randomSeed(analogRead(0)); randomSeed(analogRead(0));
} }
void pong_controls() { void render_matrix() {
if (digitalRead(P1_BTN_UP) == LOW) Serial.println("P1 up"); // clear
else if (digitalRead(P1_BTN_BOTTOM) == LOW) Serial.println("P1 down"); for (int x=0; x < 12; x++) {
else if (digitalRead(P2_BTN_UP) == LOW) Serial.println("P2 up"); for (int y=0; y < 8; y++) {
else if (digitalRead(P2_BTN_BOTTOM) == LOW) Serial.println("P2 down"); frame[y][x]= 0;
frame[y][x]= 0;
}
}
// players coords
for (int i= p1_start; i < p1_start+bar_length; i++) {
frame[i][0]= 1;
}
for (int i= p2_start; i < p2_start+bar_length; i++) {
frame[i][11]= 1;
}
// ball coords
frame[ball_y][ball_x]= 1;
matrix.renderBitmap(frame, 8, 12);
}
void pong_move_p1() {
if (digitalRead(P1_BTN_UP) == LOW && p1_start > 0) {
p1_start -= 1;
Serial.print("P1 up: ");
Serial.println(p1_start);
}
else if (digitalRead(P1_BTN_BOTTOM) == LOW && p1_start < 5) {
p1_start += 1;
Serial.print("P1 down: ");
Serial.println(p1_start);
}
}
void pong_move_p2() {
if (digitalRead(P2_BTN_UP) == LOW && p2_start > 0) {
p2_start -= 1;
Serial.print("P2 up: ");
Serial.println(p2_start);
}
else if (digitalRead(P2_BTN_BOTTOM) == LOW && p2_start < 5) {
p2_start += 1;
Serial.print("P2 down: ");
Serial.println(p2_start);
}
}
void move_ball() {
if (ball_move_x == 0 || ball_move_y == 0) {
// random balldirection
Serial.println("Ball is not moving");
// extract random number between 0 or 1 to select the directions
if (random(2) == 0) ball_move_x= 1;
else ball_move_x= -1;
if (random(2) == 0) ball_move_y= 1;
else ball_move_y= -1;
}
else if (ball_x == 0) {
// p2 score, reset board
ball_x= ball_reset_x;
ball_y= ball_reset_y;
p2_score += 1;
}
else if (ball_x == 11) {
// p1 score, reset board
ball_x= ball_reset_x;
ball_y= ball_reset_y;
p1_score += 1;
}
else if (ball_y == 0 || ball_y == 7) {
// reverse y, go down
ball_move_y= ball_move_y * -1;
}
ball_x+= ball_move_x;
ball_y+= ball_move_y;
} }
void loop() { void loop() {
if (!running) return; if (!running) return;
long exec_t1= millis(); long exec_t1= millis();
pong_controls(); pong_move_p1();
pong_move_p2();
if (exec_t1 - exec_t2 > loop_delay) { if (exec_t1 - exec_t2 > loop_delay) {
matrix.renderBitmap(frame, 8, 12); render_matrix();
move_ball();
exec_t2= exec_t1; exec_t2= exec_t1;
} }
delay(10); delay(10);
} }