-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (64 loc) · 1.61 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
var userpattern=[];
var buttons=["yellow","green","red","blue"];
var pattern=[];
var started=false;
var level=0;
$(document).keypress(function(){
if(!started){
$("#level-title").text("Level " + level);
nextseq();
started = true;
}
});
$(".btn").click(function(){
var clicked=$(this).attr("id");
userpattern.push(clicked);
sounds(clicked);
animation(clicked);
checkAns(userpattern.length-1);
});
function nextseq(){
level++;
$("#level-title").text("Level " + level);
userpattern=[];
var g=Math.floor(Math.random()*4);
var random=buttons[g];
pattern.push(random);
$("#"+random).fadeIn(100).fadeOut(100).fadeIn(100);
sounds(random);
}
function checkAns(current){
if(pattern[current]===userpattern[current]){
if(userpattern.length===pattern.length){
setTimeout(function(){
nextseq();
},1000);
}
}
else{
sounds("wrong");
$("body").addClass("game-over");
$("#level-title").text("Game Over!");
setTimeout(function(){
$("#level-title").text("Press a key to start again");},1000);
setTimeout(function(){
$("body").removeClass("game-over");
},500);
restart();
}
}
function animation(current){
$("#" + current).addClass("pressed");
setTimeout(function () {
$("#" + current).removeClass("pressed");
}, 100);
}
function sounds(clicked){
var audio=new Audio("sounds/"+clicked+".mp3");
audio.play();
}
function restart(){
started=false;
level=0;
pattern=[];
}