This repository has been archived by the owner on Dec 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
77 lines (62 loc) · 1.72 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
var pattern = [];
var roundCounter = 0;
var level = 0;
var colors = ["red", "yellow", "green", "blue"];
// $(".btn").click(function(event){
// console.log(event.currentTarget.id);
// })
function chooseNewColor(){
return colors[Math.floor(Math.random()*4)];
}
function buttonRingOut(id){
$("#"+id).addClass("pressed");
var audio = new Audio("sounds/" + id + ".mp3");
audio.play();
setTimeout(function(){
$("#"+id).removeClass("pressed");
}, 100);
}
function addNewMember(pat){
var level = pat.length + 1;
$("#level-title").text("Level " + level);
var new_member = chooseNewColor();
buttonRingOut(new_member);
pat = pat.concat([new_member]);
return pat
}
// function failAttempt(){
// pattern = [];
// $("#level-title").text()
// }
$(".btn").click(function(event){
// debugger;
if (event.currentTarget.id === pattern[roundCounter]){
roundCounter++;
buttonRingOut(event.currentTarget.id);
if (pattern.length === roundCounter){
setTimeout(function(){
pattern = addNewMember(pattern);
},1000)
roundCounter = 0;
}
}
else {
pattern = [];
roundCounter = 0;
$("#level-title").text("Game Over, Press Any Key to Restart");
$("body").addClass("game-over");
var audio = new Audio("sounds/wrong.mp3");
audio.play();
setTimeout(function(){
$("body").removeClass("game-over");
}, 200);
}
})
$(document).keydown(function(){
// debugger;
if (!$("#level-title").text().startsWith("Level")){
pattern = [];
pattern = addNewMember(pattern);
roundCounter = 0;
}
})