-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (57 loc) · 1.46 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
var colors = ["green","red","yellow","blue"];
var game_pattern = [];
var counter = 0;
var level = 1;
var is_start = false;
$(document).keypress(function(event){
if(is_start == false){
is_start = true;
start();
}
})
$(".btn").click(function() {
if (game_pattern.length >0 && game_pattern[counter] == this.id){
make_sound(this.id);
make_animation(this.id);
counter = counter + 1;
if (counter == game_pattern.length){
setTimeout(function(){start();}, 400)
}
}
else{
game_over();
}
});
function start(){
var rand = Math.floor(Math.random() * 4);
var color = colors[rand];
game_pattern.push(color);
counter = 0;
$("h1").text("Level: " + level);
make_sound(color);
make_animation(color);
level += 1;
}
function make_sound(chosen_color){
var audio = new Audio("/sounds/"+chosen_color+".mp3");
audio.play();
}
function make_animation(chosen_color){
$("."+chosen_color).addClass("pressed");
setTimeout(function(){
$("."+chosen_color).removeClass("pressed");
},100);
}
function game_over(){
$("body").addClass("game-over");
setTimeout(function(){
$("body").removeClass("game-over");
},200);
var game_over_audio = new Audio("/sounds/wrong.mp3");
game_over_audio.play();
$("h1").text("Game Over, Press Any Key to Restart");
game_pattern = [];
counter = 0;
level = 1;
is_start = false;
}