-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (79 loc) · 2.39 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
75
76
77
78
79
80
/*jshint esversion: 6 */
var colorSet = ["red", "green", "blue", "yellow"];
var userClickedColors = [];
var colorSequence = [];
var counter = 0;
var flag = false;
var player=prompt("Please enter your name");
var name=(player==="" || player===null)?"Player":player;
$('h1').children('span').text(name);
$(".center-btn").click(function() {
if (!flag) {
$(".level-title").text("Level " + counter);
$(".level-title").css({ fontSize: 24});
nextSequence();
flag = true;
}
});
$(".box").click(function() {
var userChosenColor = $(this).attr("class").split(" ")[0];
userClickedColors.push(userChosenColor);
playButtonSound(userChosenColor);
checkAnswer(userClickedColors.length-1);
});
function checkAnswer(currentLevel){
if (colorSequence[currentLevel] === userClickedColors[currentLevel]) {
if (colorSequence.length === userClickedColors.length){
nextSequence();
userClickedColors=[];
}
} else {
$(".box").addClass("redOutline");
(new Audio("sounds/wrong.mp3")).play();
$("body").addClass("game-over");
setTimeout(function(){
$("body").removeClass("game-over");
},200);
$(".box").removeClass("redOutline");
$(".level-title").css({ fontSize: 16});
$(".level-title").text(name+" you have conquered "+((counter===0)?counter:(counter-1))+" levels, press center button to restart");
$(".score").css("visibility","visible");
$(".score").text("Game over, your score: "+((counter===0)?counter:(counter-1)));
$(".center-btn").click(function() {
location.reload();
});
}
}
function startOver(){
counter=0;
colorSequence = [];
userClickedColors = [];
$(".level-title").text("Level " + counter);
nextSequence();
flag = true;
}
function playButtonSound(button){
(new Audio("sounds/"+button+".mp3")).play();
}
function nextSequence() {
counter++;
$(".level-title").text("Level " + counter);
var randomNum = Math.floor(Math.random() * 4);
var chosenColor = colorSet[randomNum];
colorSequence.push(chosenColor);
flickerColor(colorSequence);
}
function flickerColor(arrayColor) {
arrayColor.forEach((color,index)=>{
setTimeout(()=>{
flicker(color);
},(index+1)*600);
});
// $(".box").filter("." + arrayColor[i]).fadeOut(500).fadeIn(500);
}
function flicker(item){
$(".box").filter("." + item).addClass("hidden");
setTimeout(function(){
$(".box").filter("." +item).removeClass("hidden");
},500);
}