#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define BUTTON_PIN 2
#define BUZZER_PIN 3 // <--- добавлено
// Bird
int birdY = SCREEN_HEIGHT / 2;
int birdVelocity = 0;
const int gravity = 1;
const int jumpStrength = -5;
// Pipe
int pipeX = SCREEN_WIDTH;
int gapY = 20;
int gapHeight = 35; // <-- шире пролёт
const int pipeWidth = 10;
// Score
int score = 0;
bool crashed = false;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT); // <--- добавлено
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
playStartSound(); // <--- добавлено
}
void loop() {
if (crashed) {
playCrashSound(); // <--- добавлено
display.clearDisplay();
display.setCursor(30, 20);
display.print("Game Over");
display.setCursor(20, 35);
display.print("Score: ");
display.print(score);
display.display();
delay(3000);
restartGame();
}
// Input
if (digitalRead(BUTTON_PIN) == LOW) {
birdVelocity = jumpStrength;
playJumpSound(); // <--- добавлено
}
// Physics
birdVelocity += gravity;
birdY += birdVelocity;
// Pipe movement
pipeX -= 2;
if (pipeX < -pipeWidth) {
pipeX = SCREEN_WIDTH;
gapY = random(10, SCREEN_HEIGHT - gapHeight - 10);
score++;
}
// Collision
if (birdY < 0 || birdY > SCREEN_HEIGHT ||
(pipeX < 20 && pipeX + pipeWidth > 5 &&
(birdY < gapY || birdY > gapY + gapHeight))) {
crashed = true;
}
// Drawing
display.clearDisplay();
display.fillCircle(10, birdY, 3, WHITE);
display.fillRect(pipeX, 0, pipeWidth, gapY, WHITE);
display.fillRect(pipeX, gapY + gapHeight, pipeWidth, SCREEN_HEIGHT - (gapY + gapHeight), WHITE);
display.setCursor(0, 0);
display.print(score);
display.display();
delay(30);
}
void restartGame() {
birdY = SCREEN_HEIGHT / 2;
birdVelocity = 0;
pipeX = SCREEN_WIDTH;
gapY = 20;
score = 0;
crashed = false;
}
// 🎵 Звук прыжка
void playJumpSound() {
tone(BUZZER_PIN, 1000, 100); // частота 1 кГц, длительность 100 мс
}
// 💥 Звук поражения
void playCrashSound() {
tone(BUZZER_PIN, 200, 400);
delay(400);
tone(BUZZER_PIN, 100, 300);
delay(300);
noTone(BUZZER_PIN);
}
// 🚀 Стартовая мелодия
void playStartSound() {
tone(BUZZER_PIN, 800, 150);
delay(200);
tone(BUZZER_PIN, 1000, 150);
delay(200);
tone(BUZZER_PIN, 1200, 150);
delay(200);
noTone(BUZZER_PIN);
}