Spaces:
Running
Running
document.addEventListener("DOMContentLoaded", () => { | |
const balanceDisplay = document.getElementById("balance"); | |
const stakeAmountInput = document.getElementById("stake-amount"); | |
const stakeBtn = document.getElementById("stake-btn"); | |
const cashoutBtn = document.getElementById("cashout-btn"); | |
const backBtn = document.getElementById("back-btn"); | |
const plane = document.getElementById("plane"); | |
const multiplierDisplay = document.getElementById("multiplier"); | |
const audio = new Audio("sound.mp3"); // Replace with your sound file path | |
let balance = 10000; // Default balance | |
let gameInterval; | |
let multiplier = 1.0; | |
let crashPoint = 0; | |
let stakeAmount = 0; | |
// Play sound | |
const playSound = () => { | |
if (audio) audio.play(); | |
}; | |
// Stop sound | |
const stopSound = () => { | |
if (audio) { | |
audio.pause(); | |
audio.currentTime = 0; // Reset sound to the beginning | |
} | |
}; | |
// Start the game | |
const startGame = () => { | |
stakeAmount = parseFloat(stakeAmountInput.value); | |
if (isNaN(stakeAmount) || stakeAmount <= 0 || stakeAmount > balance) { | |
alert("Invalid stake amount."); | |
return; | |
} | |
crashPoint = Math.random() * 5 + 1; // Random crash point for simulation | |
multiplier = 1.0; | |
balance -= stakeAmount; | |
balanceDisplay.textContent = balance.toFixed(2); | |
cashoutBtn.disabled = false; | |
stakeBtn.disabled = true; | |
playSound(); // Play sound when the game starts | |
// Start multiplier animation and plane movement | |
gameInterval = setInterval(() => { | |
multiplier += 0.05; // Slower multiplier increment | |
multiplierDisplay.textContent = multiplier.toFixed(2) + "x"; | |
// Move the plane across the screen | |
plane.style.transform = `translateX(${multiplier * 10}px)`; | |
if (multiplier >= crashPoint) { | |
clearInterval(gameInterval); | |
alert("Plane crashed! You lost your stake."); | |
stopSound(); // Stop sound on crash | |
resetGame(); | |
} | |
}, 200); // Slower interval for smoother animation | |
}; | |
// Cash out before crash | |
const cashout = () => { | |
clearInterval(gameInterval); | |
const winnings = stakeAmount * multiplier; | |
balance += winnings; | |
balanceDisplay.textContent = balance.toFixed(2); | |
alert(`You cashed out at ${multiplier.toFixed(2)}x. Winnings: ${winnings.toFixed(2)}`); | |
stopSound(); // Stop sound on cash out | |
resetGame(); | |
}; | |
// Reset the game state | |
const resetGame = () => { | |
multiplierDisplay.textContent = "1.0x"; | |
plane.style.transform = "translateX(0px)"; | |
cashoutBtn.disabled = true; | |
stakeBtn.disabled = false; | |
}; | |
// Event listeners | |
stakeBtn.addEventListener("click", startGame); | |
cashoutBtn.addEventListener("click", cashout); | |
backBtn.addEventListener("click", () => { | |
window.location.href = "home"; // Replace with your actual back button URL | |
}); | |
// Initialize balance | |
balanceDisplay.textContent = balance.toFixed(2); | |
}); |