-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
70 lines (63 loc) · 1.68 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
var level = 1;
var started = false;
var sequence = [];
var response = [];
var buttonColours = ["red", "blue", "green", "yellow"];
$(document).keypress(function () {
if (!started) {
started = true;
$("#level-title").text("Level " + level);
sequencer();
}
});
function sequencer() {
response = [];
$("#level-title").text("Level " + level);
level++;
var randomNumber = Math.floor(Math.random() * 4);
var randomColour = buttonColours[randomNumber];
sequence.push(randomColour);
$("#" + randomColour)
.fadeIn(100)
.fadeOut(300)
.fadeIn(100);
playSound(randomColour);
}
$(".btn").click(function () {
var responseColour = $(this).attr("id");
response.push(responseColour);
animatePress(responseColour);
playSound(responseColour);
checkAnswer(response.length - 1);
});
function checkAnswer(currentLevel) {
if (sequence[currentLevel] === response[currentLevel]) {
if (sequence.length === response.length) {
setTimeout(function () {
sequencer();
}, 1000);
}
} else {
playSound("wrong");
$("#level-title").text("Game Over, Press Any Key to Restart");
setTimeout(function () {
$("body").removeClass("game-over");
}, 200);
restart();
}
}
function animatePress(currentColor) {
$("." + currentColor).addClass("pressed");
setTimeout(function () {
$("." + currentColor).removeClass("pressed");
}, 100);
}
function restart() {
level = 1;
sequence = [];
started = false;
}
function playSound(name) {
var audio = new Audio("sounds/" + name + ".mp3");
audio.play();
}