-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
66 lines (60 loc) · 1.57 KB
/
game.js
File metadata and controls
66 lines (60 loc) · 1.57 KB
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
let SOUNDS = {
red: "./sounds/red.mp3",
blue: "./sounds/blue.mp3",
green: "./sounds/green.mp3",
yellow: "./sounds/yellow.mp3",
wrong: "./sounds/wrong.mp3",
};
let BUTTON_COLOURS = ["red", "blue", "green", "yellow"];
let gamePattern = [];
let userClickedPattern = [];
let level = 0;
let playSound = (sound) => {
new Audio(SOUNDS[sound]).play();
};
let nextSequence = () => {
let button = BUTTON_COLOURS[Math.round(Math.random() * 3)];
gamePattern.push(button);
$("#" + button)
.fadeOut(100)
.fadeIn(100);
playSound(button);
level++;
$("h1").text("Level " + level);
};
$(document).on("keypress", function (e) {
if (gamePattern.length === 0) nextSequence();
});
$(".btn").click(function (e) {
userClickedPattern.push(e.target.id);
playSound(e.target.id);
animatePress(e.target.id);
checkAnswer();
});
let animatePress = (currentColour) => {
$(`#${currentColour}`).addClass("pressed");
setTimeout(function () {
$(`#${currentColour}`).removeClass("pressed");
}, 100);
};
let checkAnswer = () => {
if (gamePattern.join("") === userClickedPattern.join("")) {
userClickedPattern = [];
setTimeout(function () {
nextSequence();
}, 500);
} else if (
userClickedPattern.join("") !==
gamePattern.join("").substring(0, userClickedPattern.join("").length)
) {
$("h1").text("Game Over, Press Any Key to Restart");
$("body").addClass("game-over");
setTimeout(function () {
$("body").removeClass("game-over");
}, 200);
playSound("wrong");
gamePattern = [];
userClickedPattern = [];
level = 0;
}
};