A Customizable ESP32 Mini-game
by wengyucheng in Circuits > Wireless
97 Views, 0 Favorites, 0 Comments
A Customizable ESP32 Mini-game
Program code
Downloads
Supplies
ESP32 C3*1
USB C LINE*1
A4 paper *9
Connect to Computer
Connect the ESP32-C3 to the computer using a USB Type-C cable.
Open Arduino IDE
Open Arduino IDE, then select and connect the ESP32-C3.
Input Code
Paste the code into Arduino IDE.
Code
#include <WiFi.h>
#include <WebServer.h> // 使用內建的 WebServer,不用額外裝 ZIP
// --- 設定區SETTINGS ---
const char* ssid = "猜猜看遊戲機";
const char* password = "";
// 題目清單LIST OF QUESIONS
const char* topics[] = {"珍珠奶茶", "臭豆腐", "周杰倫", "長頸鹿", "鋼鐵人"};
int totalTopics = 5;
WebServer server(80); // 建立伺服器物件
void handleGame() {
String topic = "請掃描正確的卡片";
// 檢查網址參數 ?id=
if (server.hasArg("id")) {
int id = server.arg("id").toInt();
if (id >= 0 && id < totalTopics) {
topic = topics[id];
}
}
// 產生網頁
String html = "<html><head><meta charset='utf-8'>";
html += "<meta name='viewport' content='width=device-width, initial-scale=1.0'>";
html += "<style>body{display:flex;justify-content:center;align-items:center;height:100vh;margin:0;background:#4CAF50;font-family:sans-serif;}";
html += ".box{background:white;padding:40px;border-radius:20px;text-align:center;box-shadow:0 10px 30px rgba(0,0,0,0.3);}";
html += "h1{font-size:60px;color:#333;}</style></head><body>";
html += "<div class='box'><h2>題目是:</h2><h1>" + topic + "</h1></div>";
html += "</body></html>";
server.send(200, "text/html", html);
}
void setup() {
Serial.begin(115200);
// 啟動熱點
WiFi.softAP(ssid, password);
Serial.println("熱點已啟動");
Serial.println(WiFi.softAPIP());
// 設定路由:當手機訪問 /game 時,執行 handleGame 函式
server.on("/game", handleGame);
server.begin();
Serial.println("HTTP 伺服器已啟動");
}
void loop() {
server.handleClient(); // 必須在 loop 裡持續處理連線
}
Modifiable Projects
All Chinese characters are editable.
猜猜看遊戲機 It's the game's name.
const char* topics[] = {"珍珠奶茶", "臭豆腐", "周杰倫", "長頸鹿", "鋼鐵人"}; It is a prompt word can increase
int totalTopics = 5; The questions need to be modified after they are added. Quantity: Number of items - 1
html += "<div class='box'><h2>題目是:</h2><h1>" + topic + "</h1></div>"; The title is
QR Code * 105
A QR code for the corresponding project needs to be generated.
http://192.168.4.1/game?id=0 The numbers following are the question numbers.
I used 105 projects.
Here is my QR code file.
Downloads
Making Cards
The required size is only after printing.
Connection Usage
Connect your phone to the ESP32 C3's Wi-Fi hotspot.
Scanning
Scanning the QR code will automatically open the website with the specified prompt.
Completed
Congratulations on completing the ESP32 C3 MINI GAME!