-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
81 lines (73 loc) · 2.33 KB
/
game.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
var userClickedPattern = [],
gamePattern = [],
buttonColours = ["red", "blue", "green", "yellow"],
level = 0;
$(".starter").one("click", function () {
$("h1").text("Level 0");
nextSequence();
});
$(".btn").on("click", function (event) {
var userChosenColour = event.target.id;
userClickedPattern.push(userChosenColour);
playAudio(userChosenColour);
animatePress(userChosenColour);
var lastAnswerIndex = userClickedPattern.length - 1;
checkAnswer(lastAnswerIndex);
});
function checkAnswer(currentLevel) {
if (JSON.stringify(userClickedPattern) === JSON.stringify(gamePattern)) {
setTimeout(function () {
userClickedPattern = [];
nextSequence();
}, 1000);
}
else if (userClickedPattern[currentLevel] === gamePattern[currentLevel]) {
//Empty, just exclude the case.
}
else {
playAudio("wrong");
$("body").addClass("game-over");
setTimeout(function () {
$("body").removeClass("game-over");
}, 200);
$("h1").text("Game Over, Press The Button to Restart");
startOver();
}
}
function animatePress(currentColour) {
var buttonToAnimate = $("#" + currentColour);
buttonToAnimate.addClass("pressed");
setTimeout(function () {
buttonToAnimate.removeClass("pressed");
}, 100);
}
function playAudio(name) {
var audio = new Audio("sounds/" + name + '.mp3'),
promise = audio.play();
if (promise !== undefined) {
promise.then(_ => {
audio.play();
}).catch(error => {
alert("In order to play this game, autoplay must be enabled!");
});
}
}
//Sets random color and adds it to the gamePattern array.
function nextSequence() {
var randomNumber = Math.floor(Math.random() * 4);
randomChosenColor = buttonColours[randomNumber];
gamePattern.push(randomChosenColor);
$("#" + randomChosenColor).fadeIn(100).fadeOut(100).fadeIn(100);
playAudio(randomChosenColor);
level++;
$("#level-title").text("Level " + level);
}
function startOver() {
$(".starter").one("click", function () {
level = 0;
gamePattern = [];
userClickedPattern = [];
$("h1").text("Level 0");
nextSequence();
});
}