-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (88 loc) · 2.27 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
81
82
83
84
85
86
87
88
var randomNumber = Math.floor(Math.random() * 4);
var simonColors = ["red", "blue", "yellow", "green"];
var simonArray = [];
var userArray = [];
var cnt = 1;
function makeSound(color)
{
var audio = new Audio("./sounds/" + color + ".mp3");
audio.play();
}
function addEffect(color)
{
$("#" + color).addClass("pressed");
$("#" + color).fadeOut(100).fadeIn(100);
setTimeout(function(){
$("#" + color).removeClass("pressed");
} , 100);
}
function init()
{
setTimeout(function()
{
$("h1").text("level: " + cnt++);
for(let i=0;i<8;i++)
{
addEffect(simonColors[randomNumber]);
}
makeSound(simonColors[randomNumber]);
}, 2000);
}
simonArray.push(simonColors[randomNumber]);
init();
function checking()
{
for(var i=0;i<userArray.length;i++)
{
if(userArray[i] !== simonArray[i])
{
return 0;
}
}
return 1;
}
$(".btn").click(function(event)
{
userArray.push(event.target.id);
makeSound(event.target.id);
addEffect(event.target.id);
if(userArray.length === simonArray.length)
{
var check = checking();
setTimeout(function()
{
if(check === 0)
{
$("h1").text("Game Over");
$("body").addClass("game-over");
makeSound("wrong");
userArray = [];
simonArray = [];
setTimeout(function()
{
location.reload();
}, 3000);
}
else
{
$("h1").text("level: " + cnt++);
userArray = [];
randomNumber = Math.floor(Math.random() * 4);
simonArray.push(simonColors[randomNumber]);
makeSound(simonColors[randomNumber]);
addEffect(simonColors[randomNumber]);
}
}, 1000);
}
else if(checking() === 0)
{
$("h1").text("Game Over");
$("body").addClass("game-over");
makeSound("wrong");
userArray = [];
simonArray = [];
setTimeout(function(){
location.reload();
}, 3000);
}
});