davay-sozdadim-igru-dlya-det/data.js

108 lines
4.1 KiB
JavaScript

// data.js — данные игры (цвета, цифры, буквы) + озвучка через Web Speech API
window.G = { lang: "kk", mode: null, items: [], round: 1, total: 6, stars: 0, streak: 0 };
window.$ = function (id) { return document.getElementById(id); };
window.t = function (key) { return UI[G.lang][key]; };
// Перемешать массив (копия)
window.shuffle = function (arr) {
const a = arr.slice();
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
};
function pick(arr, n) { return shuffle(arr).slice(0, n); }
// ---- Озвучка слов ----
// kk / en — текстом на том языке; буква казахского — произносим как "бэ"
window.speak = function (text, forceLang) {
if (!("speechSynthesis" in window)) return;
const lang = forceLang || G.lang;
const u = new SpeechSynthesisUtterance(text);
u.lang = lang === "kk" ? "kk-KZ" : "en-US";
u.rate = 0.8;
u.pitch = 1.1;
window.speechSynthesis.cancel();
window.speechSynthesis.speak(u);
};
// ---- Звуковые эффекты (WebAudio: «дзынь» / «туп-туп») ----
let audioCtx = null;
function beeps(freqs) {
try {
audioCtx = audioCtx || new (window.AudioContext || window.webkitAudioContext)();
const now = audioCtx.currentTime;
freqs.forEach(function (f, i) {
const o = audioCtx.createOscillator();
const g = audioCtx.createGain();
o.type = "sine";
o.frequency.value = f;
g.gain.setValueAtTime(0.0001, now + i * 0.11);
g.gain.exponentialRampToValueAtTime(0.2, now + i * 0.11 + 0.02);
g.gain.exponentialRampToValueAtTime(0.0001, now + i * 0.11 + 0.16);
o.connect(g).connect(audioCtx.destination);
o.start(now + i * 0.11);
o.stop(now + i * 0.11 + 0.18);
});
} catch (e) { /* без звука */ }
}
window.playGood = function () { beeps([523.25, 659.25, 783.99]); };
window.playBad = function () { beeps([220, 174.6]); };
window.playWin = function () { beeps([523.25, 659.25, 783.99, 1046.5]); };
// ---- Конфетти ----
window.confetti = function () {
const glyphs = ["✿", "●", "▲", "◆", "★"];
for (let i = 0; i < 36; i++) {
const s = document.createElement("span");
s.className = "confetti-piece";
s.textContent = glyphs[i % glyphs.length];
s.style.left = Math.random() * 100 + "vw";
s.style.fontSize = (12 + Math.random() * 16) + "px";
s.style.color = "var(--kt-ai-primary)";
s.style.animationDuration = (1.6 + Math.random() * 1.6) + "s";
document.body.appendChild(s);
setTimeout(function () { s.remove(); }, 3600);
}
};
// ---- Экраны ----
const screens = ["screen-home", "screen-game", "screen-finish"];
window.showScreen = function (id) {
screens.forEach(function (s) { $(s).classList.toggle("active", s === id); });
window.scrollTo(0, 0);
};
// ---- Главная ----
window.renderHome = function () {
$("logoName").textContent = t("name");
$("homeTitle").textContent = t("hello");
$("homeSub").textContent = t("homeTip");
$("backText").textContent = t("back");
$("btnHome").textContent = t("home");
$("btnAgain").textContent = t("again");
document.title = t("name") + " — игра для малышей";
const modes = [
{ key: "colors", emoji: "🎨", labelKk: "Түстер", labelEn: "Colors", en: false },
{ key: "numbers", emoji: "🔢", labelKk: "Сандар", labelEn: "Numbers", en: false },
{ key: "letters", emoji: "🔤", labelKk: "Әріптер", labelEn: "Letters", en: true }
];
const grid = $("modeGrid");
grid.innerHTML = "";
modes.forEach(function (m) {
const b = document.createElement("button");
b.className = "mode-card";
b.innerHTML =
'<span class="mode-emoji" aria-hidden="true">' + m.emoji + '</span>' +
'<span class="mode-name">' + (G.lang === "kk" ? m.labelKk : m.labelEn) + '</span>' +
'<span class="mode-sub en">' + m.labelEn + '</span>';
b.addEventListener("click", function () { startGame(m.key, b); });
grid.appendChild(b);
});
};