-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
104 lines (101 loc) · 2.93 KB
/
index.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
var listOfColors = ["green", "red", "yellow", "blue"];
gamePattern = [];
userClickedPattern = [];
var level = 0;
var started = false;
$(document).keypress(() => {
if (!started) {
gameStart();
started = true;
}
})
$(".btn").click(function () {
if (started) {
userClickedPattern.push(this.id);
btnSelectedAnimation(this.id);
playAudio(this.id);
console.log("user " + userClickedPattern);
checkAnswer(level - 1);
}
});
// check answer
function checkAnswer(currentLeverl) {
if (gamePattern[currentLeverl] === userClickedPattern[currentLeverl]) {
console.log("equal");
// if equal
if (userClickedPattern.length === gamePattern.length) {
setTimeout(() => gameStart(), 1000);
}
} else {
// if wrong input
gamePattern = [];
userClickedPattern = [];
level = 0;
$("body").addClass("game-over");
setTimeout(() => {
$("body").removeClass("game-over");
}, 50);
headTitleChanger("game over click any key to start");
playAudio("gameOver");
started = false;
}
}
// Start game
function gameStart() {
level++;
headTitleChanger("Level " + level);
var randomColor = listOfColors[randromNumbers()];
gamePattern.push(randomColor);
btnRandomClick(randomColor);
console.log(gamePattern);
}
// Randrom numbers generator from 0 - 3 = 4
function randromNumbers() {
return Math.floor(Math.random() * 4);
}
// Head title changer by taking the text
function headTitleChanger(headerText) {
$("h1").text(headerText);
}
// btn randrom selected animation by taking id
function btnRandomClick(id) {
$("#" + id).removeClass(id);
setTimeout(() => $("#" + id).addClass(id), 50);
playAudio(id);
}
// btn Selected action animation
function btnSelectedAnimation(id) {
$("#" + id).addClass("pressed")
setTimeout(() => $("#" + id).removeClass("pressed"), 100);
}
// find what button clicked
function btnClick(id) {
playAudio(id);
btnSelectedAnimation(id);
}
// Audio file location of the clicked button
function play(color) {
var audioPath = "./sounds/";
switch (color) {
case 'blue': return audioPath + color + ".mp3";
break;
case 'yellow': return audioPath + color + ".mp3";
break;
case 'red': return audioPath + color + ".mp3";
break;
case 'green': return audioPath + color + ".mp3";
break;
default:
return audioPath + "wrong.mp3";
}
}
// Play Audio by taking btn id
function playAudio(id) {
// play takes the color id and return the file location of audio
audio(play(id));
}
// Its play audio by taking the audio file location
function audio(fileLocation) {
var audioObject = new Audio(fileLocation);
audioObject.play();
}