🌙
首页
?/span> 数学挑战
🔢 数学挑战
中等
困难
0
得分
60
时间
0
正确
12 + 8 = ?
b.classList.remove("active")); btn.classList.add("active"); if (!gameActive) generateQuestion(); } function generateQuestion() { const cfg = config[difficulty]; const op = cfg.ops[Math.floor(Math.random() * cfg.ops.length)]; let a, b; if (op === "÷") { b = Math.floor(Math.random() * 12) + 1; currentAnswer = Math.floor(Math.random() * 12) + 1; a = b * currentAnswer; } else { a = Math.floor(Math.random() * cfg.max) + 1; b = Math.floor(Math.random() * cfg.max) + 1; if (op === "-" && a < b) [a, b] = [b, a]; switch (op) { case "+": currentAnswer = a + b; break; case "-": currentAnswer = a - b; break; case "×": currentAnswer = a * b; break; } } document.getElementById("question").textContent = `${a} ${op} ${b} = ?`; document.getElementById("answer").value = ""; document.getElementById("answer").className = ""; } function checkAnswer() { const input = document.getElementById("answer"); const userAnswer = parseInt(input.value); if (userAnswer === currentAnswer) { streak++; correctCount++; const bonus = streak >= 5 ? 3 : streak >= 3 ? 2 : 1; const points = (difficulty === "easy" ? 10 : difficulty === "medium" ? 20 : 30) * bonus; score += points; input.className = "correct"; updateStreak(); } else { streak = 0; input.className = "wrong"; updateStreak(); } document.getElementById("score").textContent = score; document.getElementById("correct").textContent = correctCount; setTimeout(() => { generateQuestion(); input.focus(); }, 300); } function updateStreak() { const el = document.getElementById("streak"); if (streak >= 3) { el.innerHTML = `
🔥
连击 x${streak}!`; } else { el.textContent = ""; } } function startGame() { score = 0; correctCount = 0; streak = 0; timeLeft = 60; gameActive = true; document.getElementById("score").textContent = 0; document.getElementById("correct").textContent = 0; document.getElementById("time").textContent = 60; document.getElementById("startBtn").textContent = "重新开?; document.getElementById("answer").disabled = false; generateQuestion(); document.getElementById("answer").focus(); if (timer) clearInterval(timer); timer = setInterval(() => { timeLeft--; document.getElementById("time").textContent = timeLeft; document.getElementById("progress").style.width = (timeLeft / 60) * 100 + "%"; if (timeLeft <= 0) endGame(); }, 1000); } function endGame() { clearInterval(timer); gameActive = false; document.getElementById("answer").disabled = true; document.getElementById("question").textContent = `游戏结束! 得分: ${score}`; const high = localStorage.getItem("math_high_" + difficulty) || 0; if (score > high) { localStorage.setItem("math_high_" + difficulty, score); document.getElementById("streak").innerHTML = "🎉 新纪录!"; } } document.getElementById("answer").addEventListener("keydown", function (e) { if (e.key === "Enter" && gameActive && this.value) { checkAnswer(); } }); generateQuestion(); // Theme toggle function toggleTheme() { const html = document.documentElement; const currentTheme = html.getAttribute("data-theme"); const newTheme = currentTheme === "light" ? "dark" : "light"; html.setAttribute("data-theme", newTheme); document.getElementById("theme-icon").textContent = newTheme === "light" ? "☀? : "🌙"; localStorage.setItem("theme", newTheme); } // Load saved theme const savedTheme = localStorage.getItem("theme") || "dark"; if (savedTheme === "light") { document.documentElement.setAttribute("data-theme", "light"); document.getElementById("theme-icon").textContent = "☀?; }