#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Preferences.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define BUZZER_PIN 25
#define BUTTON_PIN 15
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
Preferences preferences;
// Игровые параметры
int dinoY = 45;
int velocity = 0;
int gravity = 2;
bool isJumping = false;
int obstacleX = 128;
int score = 0;
int highScore = 0;
// Параметры сложности
int gameSpeed = 7;
int obstacleWidth = 6;
int obstacleHeight = 10;
int level = 1;
bool inverted = false;
void showStartScreen();
void handleGameOver();
void drawGame();
void playSound(int freq, int duration);
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
randomSeed(analogRead(0)); // Инициализация генератора случайных чисел
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
for(;;);
}
preferences.begin("dino-game", false);
highScore = preferences.getInt("highscore", 0);
showStartScreen();
}
void playSound(int freq, int duration) {
tone(BUZZER_PIN, freq, duration);
}
void loop() {
// 1. УПРАВЛЕНИЕ
if (digitalRead(BUTTON_PIN) == LOW && !isJumping) {
velocity = -12;
isJumping = true;
playSound(600, 80);
}
// 2. ФИЗИКА
if (isJumping) {
dinoY += velocity;
velocity += gravity;
if (dinoY >= 45) {
dinoY = 45;
isJumping = false;
}
}
// ЛОГИКА УРОВНЕЙ И СЛОЖНОСТИ
level = (score / 10) + 1;
gameSpeed = 6 + level;
// Инверсия на 3 уровне
if (level >= 3 && !inverted) {
inverted = true;
display.invertDisplay(true);
playSound(400, 200); delay(100); playSound(800, 200);
}
obstacleX -= gameSpeed;
// Если препятствие ушло за экран
if (obstacleX < -20) {
score++;
// СЛУЧАЙНОЕ РАССТОЯНИЕ:
// На 1 уровне задержка больше (от 0 до 80 пикселей)
// На 2 уровне и выше кактусы появляются чаще (задержка меньше: 0-40)
int maxSpawnDelay = (level >= 2) ? 40 : 90;
obstacleX = 128 + random(0, maxSpawnDelay);
// СЛУЧАЙНАЯ ВЫСОТА (от 8 до 15 пикселей)
obstacleHeight = random(8, 16);
if (score % 10 == 0) playSound(1200, 50);
}
// Проверка столкновения
if (obstacleX > 15 && obstacleX < 25 && dinoY > (55 - obstacleHeight)) {
handleGameOver();
}
drawGame();
delay(30);
}
void showStartScreen() {
inverted = false;
display.invertDisplay(false);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(25, 5);
display.print("DINO RUN ESP32");
display.setCursor(18, 18);
display.print("v1.3 by AndiBond");
display.setCursor(28, 38);
display.print("HI-SCORE: ");
display.print(highScore);
display.setCursor(20, 54);
display.print("Press to Start");
display.display();
while(digitalRead(BUTTON_PIN) == HIGH) { delay(10); }
playSound(800, 100);
delay(200);
}
void handleGameOver() {
playSound(150, 600);
if (score > highScore) {
highScore = score;
preferences.putInt("highscore", highScore);
}
display.clearDisplay();
display.setCursor(10, 10);
display.setTextSize(2);
display.print("GAME OVER");
display.setTextSize(1);
display.setCursor(15, 35);
display.print("Score: "); display.print(score);
display.print(" Lv: "); display.print(level);
display.setCursor(30, 50);
display.print("HI-Score: "); display.print(highScore);
display.setCursor(30, 57);
display.print("andibond.com");
display.display();
delay(2500);
score = 0;
obstacleX = 128;
dinoY = 45;
showStartScreen();
}
void drawGame() {
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.print("L:"); display.print(level);
display.setCursor(35, 0);
display.print("S:"); display.print(score);
display.setCursor(80, 0);
display.print("HI:"); display.print(highScore);
display.drawLine(0, 55, 128, 55, WHITE);
display.fillRect(20, dinoY, 10, 10, WHITE);
display.fillRect(obstacleX, 55 - obstacleHeight, obstacleWidth, obstacleHeight, WHITE);
display.display();
}