edit:update music effect

This commit is contained in:
Yustina 2025-12-15 23:16:17 +07:00
parent e86444cb0d
commit a66c14daa8

View File

@ -19,6 +19,30 @@ body {
flex-direction: column; flex-direction: column;
} }
/* --- OVERLAY COUNTDOWN --- */
#countdown-overlay {
position: fixed;
top: 0; left: 0; width: 100%; height: 100%;
background: rgba(0,0,0,0.85);
display: flex;
justify-content: center;
align-items: center;
z-index: 1000;
color: white;
font-size: 150px;
font-weight: 800;
display: none;
}
.pulse-anim {
animation: pulse 1s ease-out infinite;
}
@keyframes pulse {
0% { transform: scale(1); opacity: 1; }
100% { transform: scale(2); opacity: 0; }
}
.topbar { .topbar {
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
@ -59,10 +83,10 @@ body {
flex: 1; flex: 1;
display: grid; display: grid;
grid-template-columns: repeat(4, 1fr); grid-template-columns: repeat(4, 1fr);
grid-template-rows: repeat(4, 1fr); gap: 16px;
gap: 8px; padding: 25px 40px;
padding: 10px;
box-sizing: border-box; box-sizing: border-box;
place-items: center;
} }
.card { .card {
@ -101,10 +125,7 @@ body {
color: #ff6a4d; color: #ff6a4d;
} }
.back { .back { transform: rotateY(180deg); }
transform: rotateY(180deg);
}
.back img { .back img {
width: 85%; width: 85%;
height: 85%; height: 85%;
@ -112,11 +133,6 @@ body {
filter: drop-shadow(0 3px 4px rgba(0,0,0,0.45)); filter: drop-shadow(0 3px 4px rgba(0,0,0,0.45));
} }
.card:not(.flipped):hover .front {
transform: scale(1.05);
transition: 0.25s;
}
.card.flipped .inner { transform: rotateY(180deg); } .card.flipped .inner { transform: rotateY(180deg); }
.card.matched .front, .card.matched .front,
@ -138,6 +154,7 @@ body {
animation: comboFade 1.4s ease forwards; animation: comboFade 1.4s ease forwards;
pointer-events: none; pointer-events: none;
border: 2px solid rgba(255,255,255,0.35); border: 2px solid rgba(255,255,255,0.35);
z-index: 99;
} }
@keyframes comboFade { @keyframes comboFade {
@ -148,10 +165,7 @@ body {
.end-screen { .end-screen {
position: fixed; position: fixed;
top: 0; top: 0; left: 0; width: 100%; height: 100%;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.45); background: rgba(0,0,0,0.45);
backdrop-filter: blur(7px); backdrop-filter: blur(7px);
display: none; display: none;
@ -169,64 +183,48 @@ body {
text-align: center; text-align: center;
} }
.end-title { .end-title { font-size: 28px; font-weight: 700; margin-bottom: 4px; }
font-size: 28px; .score-row { display: flex; justify-content: space-between; margin: 8px 0; font-size: 18px; }
font-weight: 700; .end-btn { width: 100%; padding: 12px; border-radius: 16px; border: none; font-size: 18px; margin-top: 10px; cursor: pointer; font-weight: 600; }
margin-bottom: 4px;
}
.score-row {
display: flex;
justify-content: space-between;
margin: 8px 0;
font-size: 18px;
}
.end-btn {
width: 100%;
padding: 12px;
border-radius: 16px;
border: none;
font-size: 18px;
margin-top: 10px;
cursor: pointer;
font-weight: 600;
}
.gameboard {
flex: 1;
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 16px;
padding: 25px 40px;
box-sizing: border-box;
place-items: center;
}
.play-again { background: #b700ff; color: white; } .play-again { background: #b700ff; color: white; }
.leaderboard { background: gold; } .leaderboard { background: gold; }
.back-menu { background: #444; color: white; } .back-menu { background: #444; color: white; }
@media (max-width: 900px) { .music-control {
.gameboard { position: fixed;
grid-template-columns: repeat(3, 1fr); bottom: 20px;
padding: 20px; right: 20px;
gap: 12px; z-index: 100;
} background: white;
width: 45px;
height: 45px;
border-radius: 50%;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
box-shadow: 0 4px 10px rgba(0,0,0,0.3);
font-size: 20px;
} }
@media (max-width: 600px) { @media (max-width: 600px) { .gameboard { grid-template-columns: repeat(4, 1fr); padding: 10px; gap: 8px; } }
.gameboard {
grid-template-columns: repeat(2, 1fr);
padding: 14px;
gap: 10px;
}
}
</style> </style>
</head> </head>
<body> <body>
<div id="countdown-overlay"></div>
<audio id="bgMusic" loop src="music/music_background.mp3"></audio>
<audio id="sfxClick" src="music/music-click.mp3"></audio>
<audio id="sfxMatch" src="music/music-correct.mp3"></audio>
<audio id="sfxWrong" src="music/music-incorrect.mp3"></audio>
<audio id="sfxCountdown" src="music/music-countdown.mp3"></audio>
<audio id="sfxWin" src="music/music-win.mp3"></audio>
<audio id="sfxLose" src="music/music-lose.mp3"></audio>
<div class="music-control" id="toggleMusic">🔇</div>
<header class="topbar"> <header class="topbar">
<button class="back-btn" onclick="window.location.href='mainboard.html'"></button> <button class="back-btn" onclick="window.location.href='mainboard.html'"></button>
<div class="right-info"> <div class="right-info">
@ -237,38 +235,72 @@ body {
</header> </header>
<div id="game-board" class="gameboard"></div> <div id="game-board" class="gameboard"></div>
<div id="endScreen" class="end-screen"> <div id="endScreen" class="end-screen">
<div class="end-box"> <div class="end-box">
<div class="end-title">🎉 Selamat!</div> <div id="endTitle" class="end-title">🎉 Selamat!</div>
<p>Anda berhasil menyelesaikan permainan!</p> <p id="endMsg">Anda berhasil menyelesaikan permainan!</p>
<div class="score-row"><span>Skor Base:</span> <span id="baseScoreEnd">0</span></div> <div class="score-row"><span>Skor Base:</span> <span id="baseScoreEnd">0</span></div>
<div class="score-row"><span>Time Bonus:</span> <span id="timeBonusEnd">0</span></div> <div class="score-row"><span>Time Bonus:</span> <span id="timeBonusEnd">0</span></div>
<div class="score-row"><span>Move Bonus:</span> <span id="moveBonusEnd">0</span></div> <div class="score-row"><span>Move Bonus:</span> <span id="moveBonusEnd">0</span></div>
<hr> <hr>
<div class="score-row" style="font-weight:700;"> <div class="score-row" style="font-weight:700;">
<span>Total Skor:</span> <span>Total Skor:</span>
<span id="totalScoreEnd">0</span> <span id="totalScoreEnd">0</span>
</div> </div>
<button class="end-btn play-again" onclick="startGame()">🔁 Main Lagi</button> <button class="end-btn play-again" onclick="startGame()">🔁 Main Lagi</button>
<button class="end-btn leaderboard">🏆 Lihat Leaderboard</button> <button class="end-btn leaderboard">🏆 Lihat Leaderboard</button>
<button class="end-btn back-menu" onclick="window.location.href='mainboard.html'">⬅ Kembali</button> <button class="end-btn back-menu" onclick="window.location.href='mainboard.html'">⬅ Kembali</button>
</div> </div>
</div> </div>
<script> <script>
// --- AUDIO CONFIG ---
const bgMusic = document.getElementById("bgMusic");
const sfxClick = document.getElementById("sfxClick");
const sfxMatch = document.getElementById("sfxMatch");
const sfxWrong = document.getElementById("sfxWrong");
const sfxCountdown = document.getElementById("sfxCountdown");
const sfxWin = document.getElementById("sfxWin");
const sfxLose = document.getElementById("sfxLose");
const toggleBtn = document.getElementById("toggleMusic");
const countdownOverlay = document.getElementById("countdown-overlay");
let musicMuted = true;
function playSFX(audio) {
if(!musicMuted) {
audio.currentTime = 0;
audio.play().catch(() => {});
}
}
// Interaksi pertama untuk aktifkan suara
document.addEventListener('click', function initAudio() {
if(musicMuted) {
musicMuted = false;
toggleBtn.textContent = "🔊";
bgMusic.play().catch(() => {});
document.removeEventListener('click', initAudio);
}
});
toggleBtn.onclick = (e) => {
e.stopPropagation();
if (musicMuted) {
bgMusic.play();
toggleBtn.textContent = "🔊";
} else {
bgMusic.pause();
toggleBtn.textContent = "🔇";
}
musicMuted = !musicMuted;
};
// --- GAME LOGIC ---
const images = [ const images = [
"images/fruit1.png", "images/fruit1.png", "images/fruit2.png", "images/fruit3.png", "images/fruit4.png",
"images/fruit2.png", "images/fruit5.png", "images/fruit6.png", "images/fruit7.png", "images/fruit8.png"
"images/fruit3.png",
"images/fruit4.png",
"images/fruit5.png",
"images/fruit6.png",
"images/fruit7.png",
"images/fruit8.png"
]; ];
let cards = [...images, ...images]; let cards = [...images, ...images];
@ -285,10 +317,7 @@ let lastMatchTime = 0;
function showComboPopup(targetCard, combo, bonus) { function showComboPopup(targetCard, combo, bonus) {
const popup = document.createElement("div"); const popup = document.createElement("div");
popup.className = "combo-popup"; popup.className = "combo-popup";
popup.innerHTML = ` popup.innerHTML = `COMBO X${combo}<br><span style="font-size:14px;">+${bonus} Bonus</span>`;
COMBO X${combo}<br>
<span style="font-size:14px;">+${bonus} Bonus</span>
`;
const rect = targetCard.getBoundingClientRect(); const rect = targetCard.getBoundingClientRect();
popup.style.left = rect.left + rect.width / 2 + "px"; popup.style.left = rect.left + rect.width / 2 + "px";
popup.style.top = rect.top + "px"; popup.style.top = rect.top + "px";
@ -300,63 +329,95 @@ function showComboPopup(targetCard, combo, bonus) {
function shuffleDistant(cards, minDistance = 4) { function shuffleDistant(cards, minDistance = 4) {
let valid = false; let valid = false;
let result; let result;
while (!valid) { while (!valid) {
result = [...cards].sort(() => Math.random() - 0.5); result = [...cards].sort(() => Math.random() - 0.5);
valid = true; valid = true;
const checked = new Set(); const checked = new Set();
for (let i = 0; i < result.length; i++) { for (let i = 0; i < result.length; i++) {
const value = result[i]; const value = result[i];
if (checked.has(value)) continue; if (checked.has(value)) continue;
checked.add(value); checked.add(value);
const firstIndex = result.indexOf(value); const firstIndex = result.indexOf(value);
const lastIndex = result.lastIndexOf(value); const lastIndex = result.lastIndexOf(value);
const distance = Math.abs(lastIndex - firstIndex); const distance = Math.abs(lastIndex - firstIndex);
if (distance < minDistance) { valid = false; break; }
if (distance < minDistance) {
valid = false;
break;
}
} }
} }
return result; return result;
} }
function runCountdown(callback) {
let count = 3;
countdownOverlay.style.display = "flex";
countdownOverlay.innerHTML = `<span class="pulse-anim">${count}</span>`;
playSFX(sfxCountdown);
let cdTimer = setInterval(() => {
count--;
if (count > 0) {
countdownOverlay.innerHTML = `<span class="pulse-anim">${count}</span>`;
playSFX(sfxCountdown);
} else if (count === 0) {
countdownOverlay.innerHTML = `<span class="pulse-anim">GO!</span>`;
} else {
clearInterval(cdTimer);
countdownOverlay.style.display = "none";
callback();
}
}, 1000);
}
function startTimer() { function startTimer() {
timerStarted = true; timerStarted = true;
countdown = setInterval(() => { countdown = setInterval(() => {
document.getElementById("timer").textContent = --time; document.getElementById("timer").textContent = --time;
if (time <= 0) { if (time <= 0) {
clearInterval(countdown); clearInterval(countdown);
document.getElementById("endScreen").style.display = "flex"; showEndScreen(false);
} }
}, 1000); }, 1000);
} }
function showEndScreen(isWin) {
clearInterval(countdown);
bgMusic.pause();
if(isWin) playSFX(sfxWin); else playSFX(sfxLose);
document.getElementById("endTitle").textContent = isWin ? "🎉 Selamat!" : "⏰ Waktu Habis!";
document.getElementById("endMsg").textContent = isWin ? "Anda berhasil menyelesaikan permainan!" : "Coba lagi lain kali!";
let baseScore = score;
let timeBonus = isWin ? time * 5 : 0;
let moveBonus = isWin ? Math.max(0, 200 - moves * 10) : 0;
let total = baseScore + timeBonus + moveBonus;
document.getElementById("baseScoreEnd").textContent = baseScore;
document.getElementById("timeBonusEnd").textContent = "+" + timeBonus;
document.getElementById("moveBonusEnd").textContent = "+" + moveBonus;
document.getElementById("totalScoreEnd").textContent = total;
document.getElementById("endScreen").style.display = "flex";
}
function flipCard(card) { function flipCard(card) {
if (!timerStarted) startTimer(); if (!timerStarted) startTimer();
if (flipped.length === 2 || card.classList.contains("matched") || card.classList.contains("flipped")) if (flipped.length === 2 || card.classList.contains("matched") || card.classList.contains("flipped"))
return; return;
playSFX(sfxClick);
card.classList.add("flipped"); card.classList.add("flipped");
flipped.push(card); flipped.push(card);
if (flipped.length === 2) { if (flipped.length === 2) {
moves++; moves++;
document.getElementById("moves").textContent = moves; document.getElementById("moves").textContent = moves;
let img1 = flipped[0].querySelector("img").src; let img1 = flipped[0].querySelector("img").src;
let img2 = flipped[1].querySelector("img").src; let img2 = flipped[1].querySelector("img").src;
if (img1 === img2) { if (img1 === img2) {
playSFX(sfxMatch);
const now = Date.now(); const now = Date.now();
if (!pendingMatch) { pendingMatch = true; combo = 1; }
if (!pendingMatch) { else {
pendingMatch = true;
combo = 1;
} else {
if (now - lastMatchTime <= 3000) { if (now - lastMatchTime <= 3000) {
combo++; combo++;
let comboBonus = combo * 10; let comboBonus = combo * 10;
@ -364,24 +425,18 @@ function flipCard(card) {
showComboPopup(flipped[0], combo, comboBonus); showComboPopup(flipped[0], combo, comboBonus);
} else combo = 1; } else combo = 1;
} }
lastMatchTime = now; lastMatchTime = now;
flipped.forEach(c => c.classList.add("matched")); flipped.forEach(c => c.classList.add("matched"));
score += 50; score += 50;
document.getElementById("score").textContent = score; document.getElementById("score").textContent = score;
time += 5; time += 5;
document.getElementById("timer").textContent = time; document.getElementById("timer").textContent = time;
flipped = []; flipped = [];
if (document.querySelectorAll(".matched").length === cards.length) { if (document.querySelectorAll(".matched").length === cards.length) {
setTimeout(() => document.getElementById("endScreen").style.display = "flex", 500); setTimeout(() => showEndScreen(true), 500);
} }
} else { } else {
playSFX(sfxWrong);
setTimeout(() => { setTimeout(() => {
flipped.forEach(c => c.classList.remove("flipped")); flipped.forEach(c => c.classList.remove("flipped"));
flipped = []; flipped = [];
@ -395,34 +450,24 @@ function startGame() {
const board = document.getElementById("game-board"); const board = document.getElementById("game-board");
board.innerHTML = ""; board.innerHTML = "";
runCountdown(() => {
if(!musicMuted) bgMusic.play().catch(() => {});
shuffleDistant(cards, 4).forEach(image => { shuffleDistant(cards, 4).forEach(image => {
const card = document.createElement("div"); const card = document.createElement("div");
card.className = "card"; card.className = "card";
card.innerHTML = ` card.innerHTML = `<div class="inner"><div class="front">?</div><div class="back"><img src="${image}"></div></div>`;
<div class="inner">
<div class="front">?</div>
<div class="back"><img src="${image}"></div>
</div>
`;
card.onclick = () => flipCard(card); card.onclick = () => flipCard(card);
board.appendChild(card); board.appendChild(card);
}); });
time = 60; moves = 0; score = 0; combo = 1;
time = 60; pendingMatch = false; flipped = []; timerStarted = false;
moves = 0;
score = 0;
combo = 1;
pendingMatch = false;
flipped = [];
timerStarted = false;
document.getElementById("timer").textContent = time; document.getElementById("timer").textContent = time;
document.getElementById("moves").textContent = moves; document.getElementById("moves").textContent = moves;
document.getElementById("score").textContent = score; document.getElementById("score").textContent = score;
});
} }
startGame(); startGame();
</script> </script>
</body> </body>
</html> </html>