-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
71 lines (62 loc) · 1.86 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
let buttonColours = ["red", "blue", "green", "yellow"];
let gamePattern = [];
let userChosenColour = null;
let userClickPattern = [];
let level = 0;
let started = false;
$(document).on("keydown", function (event) {
if (event.key === 'a' && started === false) {
nextSequence();
started = true;
}
}
);
$(".btn").click(function () {
userChosenColour = $(this).attr("id");
userClickPattern.push(userChosenColour);
checkAnswer(userClickPattern.length);
animatePress();
playSound(userChosenColour);
//console.log(userChosenColour);
});
function checkAnswer(currentLevel) {
if (userClickPattern[currentLevel - 1] === gamePattern[currentLevel - 1]) {
if (gamePattern.length === userClickPattern.length) {
userClickPattern = [];
setTimeout(nextSequence, 1000);
}
}
else {
playSound("wrong");
$("#level-title").text("Game Over, Press Any Key to Restart");
$("body").addClass("game-over");
setTimeout(() => {
$("body").removeClass("game-over");
}, 200);
startOver();
}
}
function startOver() {
level=0;
started=false;
gamePattern=[];
}
function playSound(name) {
let audio1 = new Audio(`sounds/${name}.mp3`);
audio1.play();
}
function nextSequence() {
level++;
let randomNumber = Math.floor(Math.random() * 4);
let randomChosenColours = buttonColours[randomNumber];
$(`#${randomChosenColours}`).fadeIn(100).fadeOut(100).fadeIn(100);
gamePattern.push(randomChosenColours);
playSound(randomChosenColours);
$("#level-title").text(`Level ${level}`);
}
function animatePress() {
$(`#${userChosenColour}`).addClass("pressed");
setTimeout(() => {
$(`#${userChosenColour}`).removeClass("pressed");
}, 100);
}