-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
104 lines (92 loc) · 3.41 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
const buttonColors = ["red", "blue", "green", "yellow"];
let gamePattern = [];
let userClickedPattern = [];
let level = 0;
let started = false;
if (!started) {
$(".btn").css("pointerEvents", "none");
$("u").click(function() {
if (!started) {
$("#level-title").text("Level " + level);
$(".btn").css("pointerEvents", "auto");
$("footer").hide();
nextSequence();
started = true;
}});
};
$(".btn").click(function() {
let userChosenColor = $(this).attr("id");
userClickedPattern.push(userChosenColor);
playSound(userChosenColor);
animatePress(userChosenColor);
checkAnswer(level);
});
function nextSequence() {
let randomNumber = Math.floor(Math.random() * 4);
let randomChosenColor = buttonColors[randomNumber];
gamePattern.push(randomChosenColor);
for (let i = 0; i < gamePattern.length; i++) {
setTimeout(function() {
$("#" + gamePattern[i]).fadeIn(100).fadeOut(100).fadeIn(100);
playSound(gamePattern[i]);
}, 500 * i)
};
};
function playSound(name) {
const audio = new Audio("sounds/" + name + ".mp3");
audio.play();
};
function animatePress(currentColor) {
$(".btn." + currentColor).addClass("pressed");
setTimeout(function() {
$(".btn." + currentColor).removeClass("pressed");
}, 100)
};
function checkAnswer() {
if (userClickedPattern.length !== gamePattern.length) {
for (i = 0; i < userClickedPattern.length; i++) {
if (userClickedPattern[i] !== gamePattern[i]) {
setTimeout(function() {
let audioWrong = new Audio("sounds/wrong.mp3");
audioWrong.play();
$("body").addClass("game-over");
// setTimeout(function() {
// $("body").removeClass("game-over");
// }, 200);
}, 200)
$(".btn").css("pointerEvents", "none");
$("#level-title").css("fontSize", "2rem");
$("#level-title").after("<h1 id='level-title'>Game Over</h1>");
$(".container").after("<h1 id='restart' id='level-title'>Click here to restart.</h1>");
$("#restart").click(function() {
location.reload(true);
});
}
}
} else if (userClickedPattern.toString() === gamePattern.toString()) {
level++;
setTimeout(function() {
$("#level-title").text("Level " + level);
}, 300);
setTimeout(function() {
userClickedPattern = [];
nextSequence();
}, 1000);
} else {
setTimeout(function() {
let audioWrong = new Audio("sounds/wrong.mp3");
audioWrong.play();
$("body").addClass("game-over");
// setTimeout(function() {
// $("body").removeClass("game-over");
// }, 200);
}, 200)
$(".btn").css("pointerEvents", "none");
$("#level-title").css("fontSize", "2rem");
$("#level-title").after("<h1 id='level-title'>Game Over</h1>");
$(".container").after("<h1 id='restart'>Click <u>here</u> to restart.</h1>");
$("#restart").click(function() {
location.reload(true);
});
};
};