-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (90 loc) · 2.5 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
var userClickedPattern = [];
var gamePattern = [];
var buttonColors = ["red", "blue", "green", "yellow"];
var level = 0;
var continueClick = true;
var started = false;
var iterator = 0;
function nextSequence() {
var randomNumber = Math.floor(4 * Math.random());
level++;
$("h1").text("Level " + level);
var randomChosenColor = buttonColors[randomNumber];
gamePattern.push(randomChosenColor);
return randomChosenColor;
}
$(document).keypress(function() {
if (!started) {
var call = nextSequence();
$("#" + call).fadeOut(100).fadeIn(100);
playSound(call);
started = true;
continueClick = false;
}
});
$(".btn").click(function() {
if (!continueClick) {
var userChosenColor = this.id;
playSound(userChosenColor);
animatePress(userChosenColor);
userClickedPattern.push(userChosenColor);
if (gamePattern[iterator] == userClickedPattern[0]) {
if ((iterator + 1) == gamePattern.length) {
userClickedPattern.shift();
var call2 = nextSequence();
setTimeout(function() {
$("#" + call2).fadeOut(100).fadeIn(100);
playSound(call2);
}, 1000);
iterator = 0;
} else {
userClickedPattern.shift();
iterator++;
}
} else {
userClickedPattern.shift();
while(gamePattern.length > 0){
gamePattern.shift();
}
playSound("wrong");
$("body").addClass("game-over");
setTimeout(function() {
$("body").removeClass("game-over");
}, 100);
$("h1").text("Game over!! Press A key to restart..");
continueClick = true;
started = false;
level=0;
}
}
});
function playSound(name) {
switch (name) {
case "red":
var audio = new Audio("sounds/" + "red" + ".mp3");
audio.play();
break;
case "blue":
var audio = new Audio("sounds/" + "blue" + ".mp3");
audio.play();
break;
case "green":
var audio = new Audio("sounds/" + "green" + ".mp3");
audio.play();
break;
case "yellow":
var audio = new Audio("sounds/" + "yellow" + ".mp3");
audio.play();
break;
case "wrong":
var audio = new Audio("sounds/" + "wrong" + ".mp3");
audio.play();
break;
}
}
function animatePress(currentColor) {
$("." + currentColor).addClass("pressed");
setTimeout(function() {
$("." + currentColor).removeClass("pressed");
}, 100);
}