Files
Golf-Game/golf-game.html
2026-08-01 14:00:27 -04:00

443 lines
15 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Golf Game</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
background: linear-gradient(to bottom, #87CEEB, #98FB98);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.game-container {
background-color: rgba(255, 255, 255, 0.9);
border-radius: 15px;
padding: 20px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
text-align: center;
max-width: 800px;
width: 100%;
}
h1 {
color: #2E8B57;
margin-bottom: 20px;
font-size: 2.5em;
}
.stats {
display: flex;
justify-content: space-around;
flex-wrap: wrap;
margin-bottom: 20px;
background-color: #f0f8ff;
padding: 15px;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.stat {
font-size: 1.2em;
font-weight: bold;
margin: 5px;
color: #333;
}
.game-area {
position: relative;
height: 300px;
background-color: #90EE90;
border: 3px solid #228B22;
border-radius: 10px;
margin-bottom: 20px;
overflow: hidden;
}
.ball {
position: absolute;
width: 20px;
height: 20px;
background-color: white;
border: 2px solid #333;
border-radius: 50%;
bottom: 40px;
left: 20px;
transition: all 1s ease-out;
}
.target {
position: absolute;
width: 30px;
height: 30px;
background-color: #FF4500;
border-radius: 50%;
bottom: 40px;
right: 20px;
box-shadow: 0 0 10px rgba(255, 69, 0, 0.8);
}
.ground {
position: absolute;
bottom: 0;
width: 100%;
height: 40px;
background-color: #8B4513;
}
.controls {
margin-bottom: 20px;
}
button {
background-color: #2E8B57;
color: white;
border: none;
padding: 12px 20px;
margin: 0 10px;
font-size: 1.1em;
border-radius: 5px;
cursor: pointer;
transition: all 0.3s;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
button:hover {
background-color: #3CB371;
transform: translateY(-2px);
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.3);
}
button:active {
transform: translateY(0);
}
.upgrades {
background-color: #f0f8ff;
padding: 15px;
border-radius: 10px;
margin-bottom: 20px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.upgrade-item {
display: flex;
justify-content: space-between;
align-items: center;
margin: 10px 0;
padding: 10px;
background-color: #e6f3ff;
border-radius: 5px;
}
.upgrade-item button {
background-color: #FF8C00;
padding: 8px 15px;
}
.upgrade-item button:hover {
background-color: #FFA500;
}
.message {
font-size: 1.2em;
font-weight: bold;
color: #2E8B57;
min-height: 30px;
margin-top: 10px;
}
/* Animation for ball movement */
@keyframes hitAnimation {
0% { transform: translateX(0) translateY(0); }
25% { transform: translateX(50px) translateY(-30px); }
50% { transform: translateX(100px) translateY(0); }
75% { transform: translateX(150px) translateY(-10px); }
100% { transform: translateX(200px) translateY(0); }
}
.hit-animation {
animation: hitAnimation 2s ease-out;
}
</style>
</head>
<body>
<div class="game-container">
<h1>Simple Golf Game</h1>
<div class="stats">
<div class="stat">Distance: <span id="distance">0</span>m</div>
<div class="stat">Power: <span id="power">50</span></div>
<div class="stat">Accuracy: <span id="accuracy">50</span></div>
<div class="stat">Money: <span id="money">$0</span></div>
</div>
<div class="game-area">
<div class="ball" id="ball"></div>
<div class="target" id="target"></div>
<div class="ground"></div>
</div>
<div class="controls">
<button id="hitButton">HIT BALL!</button>
<button id="upgradeButton">Upgrade Abilities</button>
</div>
<div class="upgrades" id="upgrades" style="display: none;">
<h3>Upgrades:</h3>
<div class="upgrade-item">
<span>Power Upgrade (+5): </span>
<button id="powerUpgrade">Cost: $100</button>
</div>
<div class="upgrade-item">
<span>Accuracy Upgrade (+5): </span>
<button id="accuracyUpgrade">Cost: $100</button>
</div>
</div>
<div class="message" id="message"></div>
</div>
<script>
// 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
// Animate the ball
animateBall(distance);
// Show result message with popup
showResultPopup(distance);
}
// Disable hit button
function disableHitButton() {
hitButton.disabled = true;
hitButton.style.opacity = '0.5';
hitButton.style.cursor = 'default';
}
// Enable hit button
function enableHitButton() {
hitButton.disabled = false;
hitButton.style.opacity = '1';
hitButton.style.cursor = 'pointer';
}
// Show result popup
function showResultPopup(distance) {
// Disable hit button while waiting for continue
disableHitButton();
// Create popup element
const popup = document.createElement('div');
popup.id = 'resultPopup';
popup.innerHTML = `
<div style="position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
background-color: rgba(255, 255, 255, 0.95); padding: 30px;
border-radius: 15px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
text-align: center; z-index: 1000; border: 3px solid #2E8B57;">
<h2>Shot Result</h2>
<p style="font-size: 1.2em; margin: 10px 0;">Distance: <strong>${distance} meters</strong></p>
<p style="font-size: 1.2em; margin: 10px 0;">Money Earned: <strong>$${Math.floor(distance / 2)}</strong></p>
<button id="continueButton" style="background-color: #2E8B57; color: white; border: none;
padding: 12px 25px; font-size: 1.1em; border-radius: 5px; cursor: pointer;
margin-top: 20px;">Continue</button>
</div>
`;
// Add popup to body
document.body.appendChild(popup);
// Add event listener to continue button
document.getElementById('continueButton').addEventListener('click', function() {
// Remove popup
document.body.removeChild(popup);
// Enable hit button for next shot
enableHitButton();
// Update display
updateDisplay();
// Generate new target for next round
generateNewTarget();
});
}
// Animate the ball movement
function animateBall(distance) {
// Reset ball position
ballElement.style.left = '20px';
ballElement.style.bottom = '40px';
// 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);
// Create a more realistic animation with multiple phases
ballElement.style.transition = 'none';
ballElement.style.transform = 'translate(0, 0)';
// Phase 1: Initial hit (upward trajectory)
setTimeout(() => {
ballElement.style.transition = 'transform 0.5s ease-out';
ballElement.style.transform = `translate(${moveDistance * 0.3}px, -60px)`;
}, 10);
// Phase 2: Fall down
setTimeout(() => {
ballElement.style.transition = 'transform 0.3s ease-in';
ballElement.style.transform = `translate(${moveDistance * 0.6}px, 0px)`;
}, 500);
// Phase 3: Bounce
setTimeout(() => {
ballElement.style.transition = 'transform 0.2s ease-out';
ballElement.style.transform = `translate(${moveDistance * 0.7}px, -20px)`;
}, 800);
setTimeout(() => {
ballElement.style.transition = 'transform 0.2s ease-in';
ballElement.style.transform = `translate(${moveDistance * 0.75}px, 0px)`;
}, 1000);
// Phase 4: Roll to final position
setTimeout(() => {
ballElement.style.transition = 'transform 0.8s linear';
ballElement.style.transform = `translate(${moveDistance}px, 0px)`;
}, 1200);
// Reset after animation completes
setTimeout(() => {
// Reset to starting position
ballElement.style.transition = 'none';
ballElement.style.transform = 'translate(0, 0)';
}, 2000);
}
// 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);
</script>
</body>
</html>