-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.js
221 lines (126 loc) · 3.84 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// colours and patterns
var buttonColours = ["red", "blue", "green", "yellow"];
var gamePattern = [];
var userClickedPattern = [];
var startingTitle = $("h1").text();
var started = false;
var levels = 0;
var highScore ;
$(".high-score").text("High Score : " + localStorage.getItem("topScore"))
if($(".high-score").text() === 'High Score : null'){
$(".high-score").text("High Score : 0")
}
// Sequence function
function nextSequence(){
var randomnumber = Math.floor( Math.random() * 4);
var randomChosenColour = buttonColours[randomnumber];
gamePattern.push(randomChosenColour);
levels++ ;
$("#level-title").text("Level " + levels);
if (highScore === undefined || highScore === null){
highScore = localStorage.getItem("topScore");
}
if(levels >= highScore){
highScore = levels;
localStorage.setItem("topScore", highScore);
}
$(".high-score").text("High Score : " + localStorage.getItem("topScore"))
$("#"+randomChosenColour).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);
playSound(randomChosenColour);
}
// Detect press "A"
$(".start-button").click(function(){
if(!started){
$(".start-button").text("Start")
nextSequence();
$("level-title").text("Level "+ levels);
started = true;
}
});
// user pressed button
$(".btn").click(function(){
var userChosenColour = $(this).attr("id");
userClickedPattern.push(userChosenColour);
checkAnswer(userClickedPattern.length - 1);
playSound(userChosenColour);
animatePress(userChosenColour);
});
// check answer
function checkAnswer(currentLevel){
if (userClickedPattern[currentLevel] === gamePattern[currentLevel]){
console.log("success")
if(userClickedPattern.length === gamePattern.length){
setTimeout(function(){
nextSequence();
}, 1000);
userClickedPattern = [];
}
}
else{
console.log("wrong");
wrong.play();
$("body").addClass("game-over");
setInterval(function(){
$("body").removeClass("game-over")
}, 200 )
$(".level-title").text("Game Over, Press Play Again to Restart");
$(".start-button").text("Play Again");
startOver();
}
}
// audio
var blue = new Audio("./sounds/blue.mp3");
var green = new Audio("./sounds/green.mp3");
var red = new Audio("./sounds/red.mp3");
var wrong = new Audio("./sounds/wrong.mp3");
var yellow = new Audio("./sounds/yellow.mp3");
function playSound(name){
if( name == "blue"){
blue.play();
}
else if( name == "green"){
green.play();
}
else if( name == "red"){
red.play();
}
else if( name == "yellow"){
yellow.play();
}
else;
}
// animation on click
function animatePress(currentColour){
$("#"+currentColour).addClass("pressed");
setTimeout(function(){
$("#"+currentColour).removeClass("pressed"),100})
}
// restart
function startOver(){
levels = 0;
gamePattern = [];
started = false;
userClickedPattern = [];
}
// dark mode
$(document).ready(function () {
$("#tooglenight").change(function () {
if (this.checked) {
$(".box").addClass("night-border");
$(".stats").addClass("night-title");
$("#level-title").addClass("night-title");
$("nav").addClass("night-nav");
$("body").addClass("night");
$("i").css("color", "white");
} else {
$("body").removeClass("night");
$("#level-title").removeClass("night-title");
$(".stats").removeClass("night-title");
$("nav").removeClass("night-nav");
$(".box").removeClass("night-border");
$("i").css("color", "black");
}
});
});
// welcome message
console.log("Welcome to Tic Tac Toe");