148 lines
4.5 KiB
JavaScript
148 lines
4.5 KiB
JavaScript
// Game state variables
|
|
let gameState = {
|
|
distance: 0,
|
|
power: 50,
|
|
accuracy: 50,
|
|
money: 0,
|
|
targetDistance: 0
|
|
};
|
|
|
|
// DOM elements
|
|
const distanceElement = document.getElementById('distance');
|
|
const powerElement = document.getElementById('power');
|
|
const accuracyElement = document.getElementById('accuracy');
|
|
const moneyElement = document.getElementById('money');
|
|
const ballElement = document.getElementById('ball');
|
|
const targetElement = document.getElementById('target');
|
|
const hitButton = document.getElementById('hitButton');
|
|
const upgradeButton = document.getElementById('upgradeButton');
|
|
const powerUpgradeButton = document.getElementById('powerUpgrade');
|
|
const accuracyUpgradeButton = document.getElementById('accuracyUpgrade');
|
|
const messageElement = document.getElementById('message');
|
|
|
|
// Initialize game
|
|
function initGame() {
|
|
updateDisplay();
|
|
generateNewTarget();
|
|
|
|
// Add event listeners
|
|
hitButton.addEventListener('click', hitBall);
|
|
upgradeButton.addEventListener('click', showUpgrades);
|
|
powerUpgradeButton.addEventListener('click', upgradePower);
|
|
accuracyUpgradeButton.addEventListener('click', upgradeAccuracy);
|
|
}
|
|
|
|
// Update display with current game state
|
|
function updateDisplay() {
|
|
distanceElement.textContent = gameState.distance;
|
|
powerElement.textContent = gameState.power;
|
|
accuracyElement.textContent = gameState.accuracy;
|
|
moneyElement.textContent = `$${gameState.money}`;
|
|
}
|
|
|
|
// Generate a new target distance
|
|
function generateNewTarget() {
|
|
// Target distance between 50 and 200 meters
|
|
gameState.targetDistance = Math.floor(Math.random() * 151) + 50;
|
|
|
|
// Position the target at the calculated distance
|
|
const gameArea = document.querySelector('.game-area');
|
|
const gameWidth = gameArea.offsetWidth;
|
|
const targetPosition = (gameState.targetDistance / 200) * (gameWidth - 50);
|
|
targetElement.style.right = `${Math.max(20, gameWidth - targetPosition - 30)}px`;
|
|
}
|
|
|
|
// Hit the ball
|
|
function hitBall() {
|
|
// Calculate distance based on power and accuracy
|
|
const powerFactor = gameState.power / 100;
|
|
const accuracyFactor = gameState.accuracy / 100;
|
|
|
|
// Add some randomness to make it interesting
|
|
const randomFactor = Math.random() * 0.5 + 0.75; // Between 0.75 and 1.25
|
|
|
|
// Calculate the actual distance
|
|
let distance = Math.floor(powerFactor * accuracyFactor * 200 * randomFactor);
|
|
|
|
// Update game state
|
|
gameState.distance = distance;
|
|
gameState.money += Math.floor(distance / 2); // Earn money based on distance
|
|
|
|
// Update display
|
|
updateDisplay();
|
|
|
|
// Animate the ball
|
|
animateBall(distance);
|
|
|
|
// Show result message
|
|
showMessage(`You hit the ball ${distance} meters!`);
|
|
|
|
// Generate new target for next round
|
|
setTimeout(generateNewTarget, 1000);
|
|
}
|
|
|
|
// Animate the ball movement
|
|
function animateBall(distance) {
|
|
// Reset ball position
|
|
ballElement.style.left = '20px';
|
|
|
|
// Calculate how far to move the ball (max 600px for visualization)
|
|
const gameArea = document.querySelector('.game-area');
|
|
const gameWidth = gameArea.offsetWidth;
|
|
const maxDistance = Math.min(distance, 200); // Cap at 200 meters for visualization
|
|
const moveDistance = (maxDistance / 200) * (gameWidth - 50);
|
|
|
|
// Add animation class
|
|
ballElement.classList.add('hit-animation');
|
|
|
|
// Move the ball
|
|
setTimeout(() => {
|
|
ballElement.style.left = `${moveDistance}px`;
|
|
ballElement.classList.remove('hit-animation');
|
|
}, 100);
|
|
}
|
|
|
|
// Show upgrades panel
|
|
function showUpgrades() {
|
|
const upgradesPanel = document.getElementById('upgrades');
|
|
if (upgradesPanel.style.display === 'none') {
|
|
upgradesPanel.style.display = 'block';
|
|
} else {
|
|
upgradesPanel.style.display = 'none';
|
|
}
|
|
}
|
|
|
|
// Upgrade power
|
|
function upgradePower() {
|
|
if (gameState.money >= 100) {
|
|
gameState.money -= 100;
|
|
gameState.power += 5;
|
|
updateDisplay();
|
|
showMessage('Power upgraded!');
|
|
} else {
|
|
showMessage('Not enough money for upgrade!');
|
|
}
|
|
}
|
|
|
|
// Upgrade accuracy
|
|
function upgradeAccuracy() {
|
|
if (gameState.money >= 100) {
|
|
gameState.money -= 100;
|
|
gameState.accuracy += 5;
|
|
updateDisplay();
|
|
showMessage('Accuracy upgraded!');
|
|
} else {
|
|
showMessage('Not enough money for upgrade!');
|
|
}
|
|
}
|
|
|
|
// Show message
|
|
function showMessage(text) {
|
|
messageElement.textContent = text;
|
|
setTimeout(() => {
|
|
messageElement.textContent = '';
|
|
}, 3000);
|
|
}
|
|
|
|
// Initialize the game when page loads
|
|
window.addEventListener('load', initGame); |