-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
149 lines (118 loc) · 2.96 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var countA_press = 0;
var started = false;
$(document).keypress(function(event){
if (event.key == "a" && countA_press == 0){
countA_press++;
random();
}
});
var arrSys = [];
var level = 1;
//System Input
function random(){
$("h1").text("Level "+level);
var ran = Math.floor(Math.random()*4+1);
pressButton(ran);
}
function pressButton(colorNo){
switch(colorNo){
case 1:
arrSys.push("red");
colorSound("red");
break;
case 2:
arrSys.push("green");
colorSound("green");
break;
case 3:
arrSys.push("blue");
colorSound("blue");
break;
case 4:
arrSys.push("yellow");
colorSound("yellow");
break;
default: console.log(colorNo);
}
}
function colorSound(color){
var sound = new Audio("sounds/"+color+".mp3");
sound.play();
colorStyle(color);
}
function colorStyle(color){
$("#"+color).addClass("pressed");
setTimeout(function(){
$("#"+color).removeClass("pressed");
},100);
}
// User Input
var arrUser = [];
var pressedTimes = 0;
$(".btn").click(function() {
var userChosenColour = $(this).attr("id");
arrUser.push(userChosenColour);
colorSound(userChosenColour);
pressedTimes++;
check(pressedTimes);
});
// $("#red").click(function(){
// arrUser.push("red");
// colorSound("red");
// pressedTimes++;
// check(pressedTimes);
// });
// $("#green").click(function(){
// arrUser.push("green");
// colorSound("green");
// pressedTimes++;
// check(pressedTimes);
// });
// $("#blue").click(function(){
// arrUser.push("blue");
// colorSound("blue");
// pressedTimes++;
// check(pressedTimes);
// });
// $("#yellow").click(function(){
// arrUser.push("yellow");
// colorSound("yellow");
// pressedTimes++;
// check(pressedTimes);
// });
function check(times){
if(arrUser[times -1] === arrSys[times -1]){
nextIn();
}
else { // You lost, back to level 1
var wrong = new Audio ("sounds/wrong.mp3");
wrong.play();
$("body").addClass("game-over");
setTimeout(function(){
$("body").removeClass("game-over");
},100);
level = 1;
arrSys = [];
arrUser = [];
var count_press = 0;
$("h1").text("Game over, refresh the page");
$(document).keypress(function(){
if (count_press == 0){
count_press++;
setTimeout(function(){
random();
},1000);
}
});
}
}
function nextIn(){ // You are going to next level
if(pressedTimes == arrSys.length){
level++;
arrUser = [];
pressedTimes = 0;
setTimeout(function(){
random();
},500);
}
}