//By Tomasz Wal 26_02_2024
//[email protected]
#include <Adafruit_NeoPixel.h>
#include <Adafruit_NeoMatrix.h>
#define PIN 6
#define POT1 A0
#define POT2 A1
#define BUZZER 8
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(15, 18, PIN, NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_ROWS + NEO_MATRIX_PROGRESSIVE, NEO_GRB + NEO_KHZ800);
int player1 = 0;
int player2 = 0;
int ballX = 7;
int ballY = 1;
int ballDirectionX = 1;
int ballDirectionY = 1;
int score1 = 0;
int score2 = 0;
void displayScore() {
matrix.fill(matrix.Color(0, 0, 0));
matrix.setCursor(1, 1);
matrix.print(score1);
matrix.setCursor(5, 1);
matrix.print(":"); // Wyświetlanie znaku ":"
matrix.setCursor(9, 1);
matrix.print(score2);
matrix.show();
// Play sound for scoring
tone(BUZZER, 500, 200); // C
delay(250);
tone(BUZZER, 600, 200); // D
delay(250);
tone(BUZZER, 700, 200); // E
delay(250);
tone(BUZZER, 800, 200); // F
delay(250);
tone(BUZZER, 900, 200); // G
delay(2000);
}
void resetBall() {
ballX = 7;
ballY = 1;
ballDirectionX = 1;
ballDirectionY = 1;
}
void displayEndGame() {
for (int i = 0; i < 80; i++) {
matrix.fill(matrix.Color(0, 0, 0));
matrix.setCursor(15 - i, 10);
matrix.print("KONIEC GRY");
matrix.setCursor(15 - i, 1);
matrix.print("PING-PONG");
matrix.show();
delay(100);
}
}
void setup() {
matrix.begin();
matrix.setTextWrap(false);
matrix.setBrightness(180);
matrix.setTextColor(matrix.Color(255, 0, 0));
pinMode(BUZZER, OUTPUT);
}
void loop() {
if (score1 >= 5 || score2 >= 5) {
displayEndGame();
return;
}
int player1Position = analogRead(POT1) / (1023 / 12);
int player2Position = analogRead(POT2) / (1023 / 12);
matrix.fill(matrix.Color(0, 0, 0));
// Draw paddles
for (int i = 0; i < 3; i++) {
matrix.drawPixel(player1Position + i, 0, matrix.Color(0, 255, 0));
matrix.drawPixel(player2Position + i, 17, matrix.Color(0, 0, 255));
}
// Move ball
ballX += ballDirectionX;
ballY += ballDirectionY;
// Draw ball
matrix.drawPixel(ballX, ballY, matrix.Color(255, 0, 0));
matrix.show();
delay(50);//prędkość piłki
// Play sound for ball movement
tone(BUZZER, 300, 50);
// Check for collision with paddle
if (ballY == 0 && ballX >= player1Position && ballX < player1Position + 3) {
ballDirectionY = 1;
if (ballX == player1Position) {
ballDirectionX = -1;
} else if (ballX == player1Position + 2) {
ballDirectionX = 1;
} else {
ballDirectionX = 0;
}
// Play sound for paddle hit
tone(BUZZER, 2000, 50);
} else if (ballY == 0) {
score2++;
displayScore();
resetBall();
}
if (ballY == 17 && ballX >= player2Position && ballX < player2Position + 3) {
ballDirectionY = -1;
if (ballX == player2Position) {
ballDirectionX = -1;
} else if (ballX == player2Position + 2) {
ballDirectionX = 1;
} else {
ballDirectionX = 0;
}
// Play sound for paddle hit
tone(BUZZER, 2000, 50);
} else if (ballY == 17) {
score1++;
displayScore();
resetBall();
}
// Check for collision with wall
if (ballX == 0 || ballX == 14) {
ballDirectionX *= -1;
}
}