Show scores on the matrix, show winner message once someone reches 9, add start ball movement delay on start and restart after score, and fix uncentered ball on restart
This commit is contained in:
@@ -5,54 +5,61 @@
|
||||
// used to increase speed when game is too easy
|
||||
int hits= 0;
|
||||
|
||||
int p1_score= 0;
|
||||
int p2_score= 0;
|
||||
|
||||
// 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;
|
||||
|
||||
void point_scored(int &ball_x, int &ball_y, int &ball_delay) {
|
||||
void random_ball_movement(int &ball_move_x, int &ball_move_y) {
|
||||
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;
|
||||
}
|
||||
|
||||
void point_scored(int &ball_x, int &ball_y, int &ball_delay, int players_scores[2], int &ball_move_x, int &ball_move_y) {
|
||||
ball_x= BALL_RESET_X;
|
||||
ball_y= BALL_RESET_Y;
|
||||
random_ball_movement(ball_move_x, ball_move_y);
|
||||
|
||||
Serial.print("P1: ");
|
||||
Serial.print(p1_score);
|
||||
Serial.print(players_scores[0]);
|
||||
Serial.print(" - ");
|
||||
Serial.print("P2: ");
|
||||
Serial.print(p2_score);
|
||||
Serial.print(players_scores[1]);
|
||||
Serial.println();
|
||||
|
||||
hits= 0;
|
||||
ball_delay= INITIAL_BALL_DELAY;
|
||||
}
|
||||
|
||||
void move_ball(int &ball_x, int &ball_y, int &ball_delay, int p1_start, int p2_start, int &need_refresh) {
|
||||
need_refresh= 1;
|
||||
bool move_ball(int &ball_x, int &ball_y, int &ball_delay, int players_coords[2], int players_scores[2], int &need_refresh) {
|
||||
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;
|
||||
ball_y= BALL_RESET_Y;
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
need_refresh= 1;
|
||||
bool scored= false;
|
||||
|
||||
// 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;
|
||||
else ball_move_x= -1;
|
||||
if (random(2) == 0) ball_move_y= 1;
|
||||
else ball_move_y= -1;
|
||||
random_ball_movement(ball_move_x, ball_move_y);
|
||||
}
|
||||
|
||||
else if (ball_x == 0) {
|
||||
// if p1 collision: reverse x, go left
|
||||
if (!ball_player_collision(p1_start, ball_y)) {
|
||||
if (!ball_player_collision(players_coords[0], ball_y)) {
|
||||
// else p2 score, reset board
|
||||
p2_score += 1;
|
||||
players_scores[1] += 1;
|
||||
scored= true;
|
||||
Serial.println("Player 2 Scores");
|
||||
point_scored(ball_x, ball_y, ball_delay);
|
||||
point_scored(ball_x, ball_y, ball_delay, players_scores, ball_move_x, ball_move_y);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
hits += 1;
|
||||
@@ -60,11 +67,13 @@ void move_ball(int &ball_x, int &ball_y, int &ball_delay, int p1_start, int p2_s
|
||||
}
|
||||
}
|
||||
else if (ball_x == MATRIX_WIDTH-1) {
|
||||
if (!ball_player_collision(p2_start, ball_y)) {
|
||||
if (!ball_player_collision(players_coords[1], ball_y)) {
|
||||
// else p1 score, reset board
|
||||
p1_score += 1;
|
||||
players_scores[0] += 1;
|
||||
scored= true;
|
||||
Serial.println("Player 1 Scores");
|
||||
point_scored(ball_x, ball_y, ball_delay);
|
||||
point_scored(ball_x, ball_y, ball_delay, players_scores, ball_move_x, ball_move_y);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
hits += 1;
|
||||
@@ -79,10 +88,11 @@ void move_ball(int &ball_x, int &ball_y, int &ball_delay, int p1_start, int p2_s
|
||||
|
||||
if (hits >= 6 && ball_delay >= 80) {
|
||||
// increase ball speed
|
||||
hits = 0;
|
||||
ball_delay -= 20;
|
||||
hits= 0;
|
||||
ball_delay-= 20;
|
||||
}
|
||||
|
||||
ball_x+= ball_move_x;
|
||||
ball_y+= ball_move_y;
|
||||
return scored;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user