ESP32 Dino Game v3.2.1 Cloud-Born: Evolution of a Legend on ESP32 (Part 6)

The ESP32 Dino Game AndiBond Project v.3.2.1 represents the pinnacle of the «Dino Run» evolution. If version 2.1.1 was about the «Golden-Heart» mechanics, the Cloud-Born version marks a quantum leap into the IoT ecosystem. Your ESP32 is no longer just a standalone console; it is now a connected device that synchronizes global high scores in real-time via Google Sheets.

🛠 Technical Breakthrough: What’s New?

In this iteration, we have integrated technologies typically found in professional IoT industrial solutions:

  • Identity System: Every device generates a unique deviceID based on its hardware MAC address.
  • Cloud Sync: A seamless bridge to Google Sheets using an intermediate Google Apps Script.
  • Non-Volatile Memory (Preferences): We have moved away from temporary data. Your nickname and High Score are permanently stored in the ESP32’s flash memory.

💾 Code Breakdown & Explanations. ESP32 Dino Game

Below is the core logic of the v3.2.1 firmware.

#include <WiFi.h>
#include <HTTPClient.h>
#include <Preferences.h> // Library for permanent flash storage
#include <Adafruit_SSD1306.h>

// --- NETWORK & CLOUD SETTINGS ---
const char* ssid = "Wokwi-GUEST"; 
const char* scriptID = "YOUR_SCRIPT_ID"; // Google Apps Script Deployment ID

// --- CLOUD SYNC FUNCTION ---
void sendDataToGoogle(int finalScore) {
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    
    // Construct the URL with parameters: Nickname, Device ID, and Score
    String url = "https://script.google.com/macros/s/" + String(scriptID) + "/exec";
    url += "?nick=" + playerName + "&id=" + deviceID + "&score=" + String(finalScore);

    http.begin(url.c_str());
    // Crucial for Google Scripts as they use redirects
    http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); 
    
    int httpCode = http.GET(); // Send the data via GET request
    
    if (httpCode > 0) {
      Serial.println("Cloud Sync: Success");
    }
    http.end();
  }
}

Key Logic Explained:

  • Unique Identity: The deviceID is extracted from the WiFi MAC address. This ensures that even if two players use the same name, their scores remain separate in the database.
  • One-Button UI: Since the hardware uses only one button, we implemented a timing-based input system. A Short Press cycles through characters, a Medium Press (0.5s) selects a letter, and a Long Press (3s) saves the profile.
  • Adaptive Difficulty: The game speed is calculated using constrain(7 + (score/15), 7, 14), ensuring the game gets harder but stays playable.

👾 Gameplay Innovation: Boss Mode

In version 3.2.1, the game is no longer an endless run. Every 100 points, the Boss Mode activates:

  • The Mechanic: A «Giant Cactus» appears on the right and fires «Mini-Cactus» bullets.
  • The Goal: Survival. You must dodge bullets for 400 game ticks. Success grants a +10 point bonus and returns you to the normal run.

Wokwi Simulation: Testing without hardware.

Before picking up a soldering iron, I recommend testing everything in the online simulator.

Try the Game in your Browser:

📊 Global Leaderboard. ESP32 Dino Game

All results are automatically pushed to the Google Spreadsheet. This allows for real-time competitions between different physical devices located anywhere in the world.

Conclusion

ESP32 Dino Game transforms a simple hobby project into a comprehensive IoT learning platform. You master WiFi stacks, HTTP protocols, permanent memory management, and complex UI/UX logic—all using a single button.


Support the AndiBond Project

Creating high-quality guides, finding working solutions, and debugging code takes a lot of time. All my projects remain open-source and free so that everyone can enter the world of electronics with the lowest possible barrier to entry.

If this tutorial saved you time or helped you launch your first game on ESP32, you can support the blog’s development. Your support helps me buy new sensors, displays, and controllers for future reviews.

Every donation is fuel for new articles and videos. Thank you for being with me!

Один комментарий

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *