-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
91 lines (86 loc) · 2.18 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
var gamepattern = [];
var userPattern = [];
var level = -1;
var start = false;
var current = 0;
var score = 0;
var mobile = false;
buttonColors = ["red", "blue", "green", "yellow"];
$(".play").on("click", function () {
mobile = true;
$(".score").text(score);
if (start !== true) {
nextSequence();
}
start = true;
});
$(document).on("keypress", function () {
$(".score").text(score);
if (start !== true) {
nextSequence();
}
start = true;
});
$(".btn").on("click", function () {
var name = $(this).attr("id");
userPattern.push(name);
playSound(name);
animatePress(name);
checkAnswer(current);
current++;
});
function animatePress(item){
$("#" + item).addClass("pressed");
setTimeout(function () {
$("#" + item).removeClass("pressed");
}, 100);
}
function animatebody(){
$("body").addClass("game-over");
setTimeout(function () {
$("body").removeClass("game-over");
}, 200);
}
function playSound(p) {
var add = "sounds/" + p + ".mp3";
var audio = new Audio(add);
audio.play();
}
function nextSequence() {
current = 0;
level++;
var randomNumber = Math.floor(Math.random() * 4);
userPattern = [];
$("h1").text("Level " + level);
gamepattern.push(buttonColors[randomNumber]);
playSound(buttonColors[randomNumber]);
animatePress(buttonColors[randomNumber]);
}
function checkAnswer(current){
if (gamepattern[current] === userPattern[current]) {
console.log("success");
}
else {
console.log("wrong");
if (mobile == true) {
$("h1").text("Game Over , Press Play to Start");
}
else {
$("h1").text("Game Over , Press any key to Start");
}
var audio = new Audio('sounds/wrong.mp3');
audio.play();
animatebody();
level = -1;
score = 0;
gamepattern = [];
start = false;
}
if (current === gamepattern.length - 1) {
score += 10;
$(".score").text(score);
setTimeout(function () {
nextSequence();
}, 1000);
}
}