-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjQueryfile.js
79 lines (64 loc) · 1.96 KB
/
jQueryfile.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
var gamePattern = [];
var userClickedPattern = [];
let randomChoosenColor;
let level;
let index;
const buttonColors = ["red", "green", "blue", "yellow"];
function nextSequence() {
userClickedPattern = [];
level++;
$("#level-title").text("Level " + level);
let randomNumber = Math.floor(Math.random() * 4);
randomChoosenColor = buttonColors[randomNumber];
gamePattern.push(randomChoosenColor);
console.log(gamePattern);
let element = $("#" + randomChoosenColor);
element.animate({ opacity: '0.1' }, 100);
element.animate({ opacity: '1.0' }, 100);
playSound(randomChoosenColor);
}
function playSound(name) {
var audioNext = new Audio("sounds/" + name + ".mp3");
audioNext.play();
}
function checkGame(index) {
let i = index - 1;
if (gamePattern[i] == userClickedPattern[i]) {
console.log("success");
if (gamePattern.length === userClickedPattern.length) {
console.log("next level" + level + 1);
setTimeout(nextSequence, 1000);
}
}
else {
console.log("wrong");
gamePattern = [];
playSound("wrong");
$("body").addClass("game-over");
$("#level-title").text("Game over, Press any key to restart");
setTimeout(function () {
$("body").removeClass("game-over");
}, 200)
level = undefined;
}
}
function animatePress(color) {
$("." + color).addClass('pressed');
setTimeout(function () {
$("." + color).removeClass('pressed');
}, 100);
}
$(document).keydown(function () {
if (level == undefined) {
level = 0;
nextSequence();
}
});
$(".btn").click(function () {
let userChoosedColor = this.id;
index = userClickedPattern.push(userChoosedColor);
console.log(userClickedPattern);
checkGame(index);
animatePress(userChoosedColor);
playSound(userChoosedColor);
});