-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
73 lines (59 loc) · 1.64 KB
/
script.js
File metadata and controls
73 lines (59 loc) · 1.64 KB
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
//comment
const buttonColors = ["red", "blue", "green", "yellow"];
let gamePattern = [];
let userClickedPattern = [];
function changeTitle(text) {
$("#level-title").slideDown(50, function () {
$("#level-title").text(text);
});
}
function nextSequence() {
const randomNumber = Math.floor(Math.random() * 4);
gamePattern.push(buttonColors[randomNumber]);
changeTitle(`Level ${gamePattern.length}`);
animatePress(buttonColors[randomNumber]);
playSound(buttonColors[randomNumber]);
userClickedPattern = [];
}
function playSound(id) {
const audio = new Audio(`sounds/${id}.mp3`);
audio.play();
}
function animatePress(currentCoulour) {
$(`#${currentCoulour}`).addClass("pressed");
setTimeout(function () {
$(`#${currentCoulour}`).removeClass("pressed");
}, 100);
}
function gameOver() {
playSound("wrong");
gamePattern = [];
$("body").addClass("game-over");
setTimeout(function () {
$("body").removeClass("game-over");
}, 200);
changeTitle("Game Over. Click the button to start again!");
}
function checkAnswer() {
const latestIndex = userClickedPattern.length - 1;
if (userClickedPattern[latestIndex] === gamePattern[latestIndex]) {
if (gamePattern.length === userClickedPattern.length) {
$("#level-title").slideUp(75, function () {});
setTimeout(nextSequence, 1000);
}
} else {
gameOver();
}
}
$(".btn").click(function (e) {
$(this).fadeOut(100).fadeIn(100);
playSound(this.id);
animatePress(this.id);
userClickedPattern.push(this.id);
checkAnswer(userClickedPattern.length);
});
$(".start-btn").click(function () {
if (gamePattern.length === 0) {
nextSequence();
}
});