2026-03-15 19:56:20 +01:00
|
|
|
#include <Arduino.h>
|
|
|
|
|
#include "config.h"
|
|
|
|
|
#include "pong_player.h"
|
|
|
|
|
|
|
|
|
|
// used to increase speed when game is too easy
|
|
|
|
|
int hits= 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;
|
|
|
|
|
|
2026-03-16 22:37:53 +01:00
|
|
|
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) {
|
2026-03-15 19:56:20 +01:00
|
|
|
ball_x= BALL_RESET_X;
|
|
|
|
|
ball_y= BALL_RESET_Y;
|
2026-03-16 22:37:53 +01:00
|
|
|
random_ball_movement(ball_move_x, ball_move_y);
|
|
|
|
|
|
2026-03-15 19:56:20 +01:00
|
|
|
Serial.print("P1: ");
|
2026-03-16 22:37:53 +01:00
|
|
|
Serial.print(players_scores[0]);
|
2026-03-15 19:56:20 +01:00
|
|
|
Serial.print(" - ");
|
|
|
|
|
Serial.print("P2: ");
|
2026-03-16 22:37:53 +01:00
|
|
|
Serial.print(players_scores[1]);
|
2026-03-15 19:56:20 +01:00
|
|
|
Serial.println();
|
|
|
|
|
|
|
|
|
|
hits= 0;
|
2026-03-15 20:23:52 +01:00
|
|
|
ball_delay= INITIAL_BALL_DELAY;
|
2026-03-15 19:56:20 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-16 22:37:53 +01:00
|
|
|
bool move_ball(int &ball_x, int &ball_y, int &ball_delay, int players_coords[2], int players_scores[2], int &need_refresh) {
|
2026-03-15 19:56:20 +01:00
|
|
|
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;
|
2026-03-16 22:37:53 +01:00
|
|
|
return false;
|
2026-03-15 19:56:20 +01:00
|
|
|
}
|
2026-03-16 22:37:53 +01:00
|
|
|
|
|
|
|
|
need_refresh= 1;
|
|
|
|
|
bool scored= false;
|
2026-03-15 19:56:20 +01:00
|
|
|
|
|
|
|
|
// 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
|
2026-03-16 22:37:53 +01:00
|
|
|
random_ball_movement(ball_move_x, ball_move_y);
|
2026-03-15 19:56:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (ball_x == 0) {
|
|
|
|
|
// if p1 collision: reverse x, go left
|
2026-03-16 22:37:53 +01:00
|
|
|
if (!ball_player_collision(players_coords[0], ball_y)) {
|
2026-03-15 19:56:20 +01:00
|
|
|
// else p2 score, reset board
|
2026-03-16 22:37:53 +01:00
|
|
|
players_scores[1] += 1;
|
|
|
|
|
scored= true;
|
2026-03-15 19:56:20 +01:00
|
|
|
Serial.println("Player 2 Scores");
|
2026-03-16 22:37:53 +01:00
|
|
|
point_scored(ball_x, ball_y, ball_delay, players_scores, ball_move_x, ball_move_y);
|
|
|
|
|
return true;
|
2026-03-15 19:56:20 +01:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
hits += 1;
|
|
|
|
|
ball_move_x= ball_move_x * -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (ball_x == MATRIX_WIDTH-1) {
|
2026-03-16 22:37:53 +01:00
|
|
|
if (!ball_player_collision(players_coords[1], ball_y)) {
|
2026-03-15 19:56:20 +01:00
|
|
|
// else p1 score, reset board
|
2026-03-16 22:37:53 +01:00
|
|
|
players_scores[0] += 1;
|
|
|
|
|
scored= true;
|
2026-03-15 19:56:20 +01:00
|
|
|
Serial.println("Player 1 Scores");
|
2026-03-16 22:37:53 +01:00
|
|
|
point_scored(ball_x, ball_y, ball_delay, players_scores, ball_move_x, ball_move_y);
|
|
|
|
|
return true;
|
2026-03-15 19:56:20 +01:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
hits += 1;
|
|
|
|
|
ball_move_x= ball_move_x * -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ball_y == 0 || ball_y == MATRIX_HEIGHT-1) {
|
|
|
|
|
// reverse y, go down
|
|
|
|
|
ball_move_y= ball_move_y * -1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-15 20:23:52 +01:00
|
|
|
if (hits >= 6 && ball_delay >= 80) {
|
2026-03-15 19:56:20 +01:00
|
|
|
// increase ball speed
|
2026-03-16 22:37:53 +01:00
|
|
|
hits= 0;
|
|
|
|
|
ball_delay-= 20;
|
2026-03-15 19:56:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ball_x+= ball_move_x;
|
|
|
|
|
ball_y+= ball_move_y;
|
2026-03-16 22:37:53 +01:00
|
|
|
return scored;
|
2026-03-15 19:56:20 +01:00
|
|
|
}
|