-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
78 lines (65 loc) · 1.62 KB
/
script.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
let levelAr = ["green", "red", "yellow", "blue"];
let level = 0;
let gameAr = [];
let inputAr = [];
var started = false;
$("body").keypress(function(){
if(!started){
$("h1").html("Level "+ level)
randomLevel();
started = true;
}
})
$("button").click(function(){
let curr = $(this).attr("id")
inputAr.push(curr)
//play
playSound(curr);
//animate
//check
checkRes(inputAr.length-1);
})
function checkRes(currLevel){
if(gameAr[currLevel] === inputAr[currLevel]){
if(inputAr.length === gameAr.length){
setTimeout(function () {
randomLevel();
}, 1000);
}
}else{
//play sound
playSound("wrong");
$("body").addClass("go");
$("h1").text("Game Over, Press S Key to Restart");
$("h3").addClass("kpop")
$("h3").text("Your Score: "+level)
//setTIme
setTimeout(function () {
$("body").removeClass("go");
$("h3").removeClass("kpop")
}, 2000);
//startOver
gameOver();
}
}
function randomLevel(){
inputAr=[];
level++;
$("h1").html("Level "+ level)
let randomC = levelAr[Math.floor(Math.random()*4)];
$("#"+randomC).addClass("onP");
gameAr.push(randomC);
setTimeout(function(){
$("#"+randomC).removeClass("onP");
},"1000")
playSound(randomC);
}
function playSound(name){
var audio = new Audio("sounds/" + name + ".mp3");
audio.play();
}
function gameOver(){
level = 0;
gameAr = []
started = false;
}