Compare commits
2 Commits
fa47480aef
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b9bc138cad | ||
|
|
e7f0c3cc1a |
@@ -42,6 +42,8 @@ int bar_length= 3;
|
||||
int p1_score= 0;
|
||||
int p2_score= 0;
|
||||
|
||||
int need_refresh= 1;
|
||||
|
||||
int loop_delay= 220;
|
||||
long exec_t2= millis();
|
||||
|
||||
@@ -59,6 +61,8 @@ void setup() {
|
||||
}
|
||||
|
||||
void render_matrix() {
|
||||
if (!need_refresh) return;
|
||||
need_refresh= 0;
|
||||
// clear
|
||||
for (int x=0; x < 12; x++) {
|
||||
for (int y=0; y < 8; y++) {
|
||||
@@ -84,30 +88,54 @@ void render_matrix() {
|
||||
void pong_move_p1() {
|
||||
if (digitalRead(P1_BTN_UP) == LOW && p1_start > 0) {
|
||||
p1_start -= 1;
|
||||
Serial.print("P1 up: ");
|
||||
Serial.println(p1_start);
|
||||
need_refresh= 1;
|
||||
}
|
||||
else if (digitalRead(P1_BTN_BOTTOM) == LOW && p1_start < 5) {
|
||||
p1_start += 1;
|
||||
Serial.print("P1 down: ");
|
||||
Serial.println(p1_start);
|
||||
need_refresh= 1;
|
||||
}
|
||||
}
|
||||
|
||||
void pong_move_p2() {
|
||||
if (digitalRead(P2_BTN_UP) == LOW && p2_start > 0) {
|
||||
p2_start -= 1;
|
||||
Serial.print("P2 up: ");
|
||||
Serial.println(p2_start);
|
||||
need_refresh= 1;
|
||||
}
|
||||
else if (digitalRead(P2_BTN_BOTTOM) == LOW && p2_start < 5) {
|
||||
p2_start += 1;
|
||||
Serial.print("P2 down: ");
|
||||
Serial.println(p2_start);
|
||||
need_refresh= 1;
|
||||
}
|
||||
}
|
||||
|
||||
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() {
|
||||
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) {
|
||||
// extract random number between 0 or 1 to select the directions
|
||||
if (random(2) == 0) ball_move_x= 1;
|
||||
@@ -118,51 +146,33 @@ void move_ball() {
|
||||
|
||||
else if (ball_x == 0) {
|
||||
// if p1 collision: reverse x, go left
|
||||
int hit= 0;
|
||||
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) {
|
||||
if (!ball_player_collision(p1_start)) {
|
||||
// else p2 score, reset board
|
||||
ball_x= ball_reset_x;
|
||||
ball_y= ball_reset_y;
|
||||
p2_score += 1;
|
||||
|
||||
Serial.print("Player 2 score: ");
|
||||
Serial.println(p2_score);
|
||||
Serial.print("Player 1 score: ");
|
||||
Serial.println(p1_score);
|
||||
Serial.println("Player 2 Point");
|
||||
print_points();
|
||||
}
|
||||
else {
|
||||
ball_move_x= ball_move_x * -1;
|
||||
}
|
||||
}
|
||||
else if (ball_x == 11) {
|
||||
int hit= 0;
|
||||
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) {
|
||||
if (!ball_player_collision(p2_start)) {
|
||||
// else p1 score, reset board
|
||||
ball_x= ball_reset_x;
|
||||
ball_y= ball_reset_y;
|
||||
p1_score += 1;
|
||||
Serial.print("Player 2: ");
|
||||
Serial.println(p2_score);
|
||||
Serial.print("Player 1: ");
|
||||
Serial.println(p1_score);
|
||||
Serial.println("Player 1 Point");
|
||||
print_points();
|
||||
}
|
||||
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
|
||||
ball_move_y= ball_move_y * -1;
|
||||
}
|
||||
@@ -180,5 +190,5 @@ void loop() {
|
||||
move_ball();
|
||||
exec_t2= exec_t1;
|
||||
}
|
||||
delay(10);
|
||||
delay(50);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user