|
| 1 | +let gamePattern = []; |
| 2 | +let userClickedPattern = []; |
| 3 | +let buttonColours = ["red", "blue", "green", "yellow"]; |
| 4 | +let started = false; |
| 5 | +let level = 0; |
| 6 | +let highScore = 0; |
| 7 | +let gameMode = null; // Game mode is null initially |
| 8 | + |
| 9 | +// Display the high score and level |
| 10 | +$("#high-score").text(highScore); |
| 11 | +$("#current-level").text(level); |
| 12 | + |
| 13 | +// Start Game Button Click Event |
| 14 | +$("#start-button").on("click", function () { |
| 15 | + if (!gameMode) { |
| 16 | + alert("Please select a mode (Normal or Strict) to start the game!"); |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + if (!started) { |
| 21 | + $("#level-title").text("Happy Gaming! - Simon"); |
| 22 | + nextSequence(); |
| 23 | + started = true; |
| 24 | + $(this).hide(); |
| 25 | + } |
| 26 | +}); |
| 27 | + |
| 28 | +// End Game Button Click Event |
| 29 | +$("#end-button").on("click", function () { |
| 30 | + if (!started) return; // Do nothing if the game hasn't started |
| 31 | + |
| 32 | + // End the game by resetting all values |
| 33 | + $("#level-title").text("Game Ended. Press Start to Restart"); |
| 34 | + updateHighScore(); // Save high score before resetting |
| 35 | + startOver(); // Reset the game state |
| 36 | + $("#start-button").show(); // Show the Start button again |
| 37 | +}); |
| 38 | + |
| 39 | +// Mode Selection (Normal/Strict) |
| 40 | +$("input[name='mode']").on("change", function () { |
| 41 | + gameMode = $(this).val(); |
| 42 | + alert(`Game mode set to: ${gameMode.toUpperCase()}`); |
| 43 | +}); |
| 44 | + |
| 45 | +// User Clicks on a Button |
| 46 | +$(".btn").on("click", function () { |
| 47 | + if (!started) return; |
| 48 | + |
| 49 | + let userChosenColour = $(this).attr("id"); |
| 50 | + userClickedPattern.push(userChosenColour); |
| 51 | + |
| 52 | + playSound(userChosenColour); |
| 53 | + animatePress(userChosenColour); |
| 54 | + |
| 55 | + checkAnswer(userClickedPattern.length - 1); |
| 56 | +}); |
| 57 | + |
| 58 | +// Check User's Answer |
| 59 | +function checkAnswer(currentLevel) { |
| 60 | + if (gamePattern[currentLevel] === userClickedPattern[currentLevel]) { |
| 61 | + if (userClickedPattern.length === gamePattern.length) { |
| 62 | + setTimeout(() => { |
| 63 | + nextSequence(); |
| 64 | + }, 1000); |
| 65 | + } |
| 66 | + } else { |
| 67 | + playSound("wrong"); |
| 68 | + $("body").addClass("game-over"); |
| 69 | + $("#level-title").text("Game Over. Press Start to Retry."); |
| 70 | + |
| 71 | + setTimeout(() => { |
| 72 | + $("body").removeClass("game-over"); |
| 73 | + }, 200); |
| 74 | + |
| 75 | + if (gameMode === "strict") { |
| 76 | + updateHighScore(); |
| 77 | + startOver(); // Strict mode resets the game completely |
| 78 | + } else { |
| 79 | + userClickedPattern = []; // Normal mode allows retrying the same sequence |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// Generate the Next Sequence |
| 85 | +function nextSequence() { |
| 86 | + userClickedPattern = []; |
| 87 | + level++; |
| 88 | + $("#current-level").text(level); |
| 89 | + |
| 90 | + let randomNumber = Math.floor(Math.random() * 4); |
| 91 | + let randomChosenColour = buttonColours[randomNumber]; |
| 92 | + gamePattern.push(randomChosenColour); |
| 93 | + |
| 94 | + $(`#${randomChosenColour}`).fadeOut(100).fadeIn(100); |
| 95 | + playSound(randomChosenColour); |
| 96 | +} |
| 97 | + |
| 98 | +// Play Sound Based on Button Colour |
| 99 | +function playSound(name) { |
| 100 | + let audio = new Audio(`sounds/${name}.mp3`); |
| 101 | + audio.play(); |
| 102 | +} |
| 103 | + |
| 104 | +// Animate Button Press |
| 105 | +function animatePress(currentColour) { |
| 106 | + $(`#${currentColour}`).addClass("pressed"); |
| 107 | + setTimeout(() => { |
| 108 | + $(`#${currentColour}`).removeClass("pressed"); |
| 109 | + }, 100); |
| 110 | +} |
| 111 | + |
| 112 | +// Start Over Function (Resets Game State) |
| 113 | +function startOver() { |
| 114 | + level = 0; |
| 115 | + gamePattern = []; |
| 116 | + userClickedPattern = []; |
| 117 | + started = false; |
| 118 | + $("#current-level").text(level); |
| 119 | +} |
| 120 | + |
| 121 | +// Update High Score |
| 122 | +function updateHighScore() { |
| 123 | + if (level > highScore) { |
| 124 | + highScore = level - 1; // Subtract 1 because the game increments the level before game over |
| 125 | + $("#high-score").text(highScore); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +// Toggle Dark/Light Mode |
| 130 | +$("#mode-toggle").on("click", function () { |
| 131 | + $("body").toggleClass("dark-mode"); |
| 132 | + $(this).toggleClass("dark-mode"); |
| 133 | +}); |
0 commit comments