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