|
| 1 | +let score = 0; |
| 2 | +let highScore = 0; |
| 3 | +let strength = 10; // Start strength at 10 |
| 4 | +let isFlipping = false; |
| 5 | +let pressStartTime = 0; |
| 6 | +const maxStrength = 100; |
| 7 | +let strengthIncrement = 1; // Controls whether strength is increasing or decreasing |
| 8 | +let bottleAngle = 0; |
| 9 | +let strengthLoop; |
| 10 | +let speedFactor = 1; // This factor increases after successful flips |
| 11 | + |
| 12 | +const bottle = document.getElementById('bottle'); |
| 13 | +const brokenBottle = document.getElementById('broken-bottle'); |
| 14 | +const scoreDisplay = document.getElementById('score'); |
| 15 | +const highScoreDisplay = document.getElementById('high-score'); |
| 16 | +const strengthBar = document.getElementById('strength-bar'); |
| 17 | +const strengthValue = document.getElementById('strength-value'); |
| 18 | +const newGameBtn = document.getElementById('new-game'); |
| 19 | +const progressFill = document.createElement('div'); |
| 20 | +progressFill.classList.add('progress-fill'); |
| 21 | +document.querySelector('.strength-bar-wrapper').appendChild(progressFill); |
| 22 | + |
| 23 | +// Start game function |
| 24 | +function startGame() { |
| 25 | + // Reset everything for a new game |
| 26 | + score = 0; |
| 27 | + updateScore(); |
| 28 | + brokenBottle.style.display = 'none'; |
| 29 | + bottle.style.display = 'block'; |
| 30 | + newGameBtn.style.display = 'none'; |
| 31 | + bottleAngle = 0; |
| 32 | + bottle.style.transform = 'rotate(0deg)'; |
| 33 | + speedFactor = 1; // Reset speed factor |
| 34 | + |
| 35 | + // Reset the strength bar to 0 |
| 36 | + strength = 0; |
| 37 | + strengthBar.value = strength; |
| 38 | + strengthValue.textContent = `${strength}%`; |
| 39 | +} |
| 40 | + |
| 41 | +// Handle mouse down (start strength calculation) |
| 42 | +bottle.addEventListener('mousedown', () => { |
| 43 | + if (!isFlipping) { |
| 44 | + isFlipping = true; |
| 45 | + pressStartTime = Date.now(); |
| 46 | + strengthIncrement = 1; // Start with increasing strength |
| 47 | + startStrengthLoop(); |
| 48 | + } |
| 49 | +}); |
| 50 | + |
| 51 | +// Handle mouse up (calculate strength and flip the bottle) |
| 52 | +bottle.addEventListener('mouseup', () => { |
| 53 | + if (isFlipping) { |
| 54 | + isFlipping = false; |
| 55 | + clearInterval(strengthLoop); |
| 56 | + |
| 57 | + // Check if the strength is in the success range |
| 58 | + if (strength >= 50 && strength <= 60) { |
| 59 | + flipBottle(); // Successful flip |
| 60 | + } else { |
| 61 | + breakBottle(); // Bottle breaks due to incorrect strength |
| 62 | + } |
| 63 | + } |
| 64 | +}); |
| 65 | + |
| 66 | +// Loop strength bar from 10-100 and back to 10, with increasing complexity |
| 67 | +function startStrengthLoop() { |
| 68 | + strengthLoop = setInterval(() => { |
| 69 | + strength += strengthIncrement * speedFactor; |
| 70 | + if (strength >= 100) { |
| 71 | + strengthIncrement = -1; // Switch to decreasing strength |
| 72 | + } else if (strength <= 10) { |
| 73 | + strengthIncrement = 1; // Switch to increasing strength |
| 74 | + } |
| 75 | + |
| 76 | + strengthBar.value = strength; |
| 77 | + updateStrengthBarColor(); |
| 78 | + strengthValue.textContent = `${Math.floor(strength)}%`; |
| 79 | + |
| 80 | + // Spin bottle according to strength |
| 81 | + bottleAngle += (strength / 5) * speedFactor; // Adjust spin speed |
| 82 | + bottle.style.transform = `rotate(${bottleAngle}deg)`; |
| 83 | + }, 50); // Adjust timing for smooth strength increase |
| 84 | +} |
| 85 | + |
| 86 | +// Function to update the strength bar color and width dynamically |
| 87 | +function updateStrengthBarColor() { |
| 88 | + // Set the width of the progress fill according to strength percentage |
| 89 | + progressFill.style.width = `${strength}%`; |
| 90 | + |
| 91 | + // Change the color based on strength value |
| 92 | + if (strength >= 0 && strength < 50) { |
| 93 | + // Yellow for 0% - 49% |
| 94 | + progressFill.style.backgroundColor = 'yellow'; |
| 95 | + } else if (strength >= 50 && strength <= 59) { |
| 96 | + // Green for 50% - 59% |
| 97 | + progressFill.style.backgroundColor = 'green'; |
| 98 | + } else if (strength >= 60 && strength <= 100) { |
| 99 | + // Red for 60% - 100% |
| 100 | + progressFill.style.backgroundColor = 'red'; |
| 101 | + } |
| 102 | + |
| 103 | + // Update the text value for the strength |
| 104 | + strengthValue.textContent = `${Math.floor(strength)}%`; |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | +// Flip the bottle |
| 109 | +function flipBottle() { |
| 110 | + // Random angle between +7 to -7 from 360 degrees |
| 111 | + let finalAngle = 360 + (Math.random() * 14 - 7); |
| 112 | + bottle.style.transform = `rotate(${finalAngle}deg)`; |
| 113 | + |
| 114 | + // Success, reset strength, increase difficulty, and reset progress bar to 0 |
| 115 | + score++; |
| 116 | + updateScore(); |
| 117 | + resetStrength(); |
| 118 | + speedFactor += 0.2; // Increase speed factor to make next flip harder |
| 119 | + |
| 120 | + // Reset the strength bar to 0 |
| 121 | + strength = 0; |
| 122 | + strengthBar.value = strength; |
| 123 | + strengthValue.textContent = `${strength}%`; |
| 124 | +} |
| 125 | + |
| 126 | +// Break the bottle |
| 127 | +function breakBottle() { |
| 128 | + brokenBottle.style.display = 'block'; |
| 129 | + bottle.style.display = 'none'; |
| 130 | + newGameBtn.style.display = 'block'; // Show "New Game" button |
| 131 | + clearInterval(strengthLoop); // Stop strength loop |
| 132 | +} |
| 133 | + |
| 134 | +// Reset strength bar and values |
| 135 | +function resetStrength() { |
| 136 | + strength = 10; |
| 137 | + strengthIncrement = 1; |
| 138 | + strengthBar.value = strength; |
| 139 | + updateStrengthBarColor(); |
| 140 | + strengthValue.textContent = `${strength}%`; |
| 141 | +} |
| 142 | + |
| 143 | +// Update the score display |
| 144 | +function updateScore() { |
| 145 | + scoreDisplay.textContent = `Score: ${score}`; |
| 146 | + if (score > highScore) { |
| 147 | + highScore = score; |
| 148 | + updateHighScore(); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +// Update the high score display |
| 153 | +function updateHighScore() { |
| 154 | + highScoreDisplay.textContent = `High Score: ${highScore}`; |
| 155 | +} |
0 commit comments