-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
104 lines (85 loc) · 2.39 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
var buttonColors = ["red", "blue", "green", "yellow"];
var gamePattern = [];
var userClickedPattern = [];
var randomChosenColor;
var isGameStarted = false;
var level = 0;
var success = false;
function nextSequence() {
// generate random game pattern
userClickedPattern = [];
isGameStarted = true;
var randomNumber = Math.floor(Math.random()*4);
randomChosenColor = buttonColors[randomNumber];
gamePattern.push(randomChosenColor);
$("h1").text(`Level ${level}`);
//animation
$(`#${randomChosenColor}`).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);
level = level + 1;
playSound(randomChosenColor);
}
//Initial start
$(document).on("keydown", function () {
if(!isGameStarted) {
nextSequence();
}
});
//play sound of color
function playSound(color){
var audio = new Audio(`sounds/${color}.mp3`);
audio.play();
}
//add effect of user press
function animatePress(color){
$(`.${color}`).addClass("pressed");
setTimeout(() => {
$(`.${color}`).removeClass("pressed");
}, 100);
}
// user pattern
$(".btn").on("click", function (e) {
var userChosenColor = e.target.id;
playSound(userChosenColor);
animatePress(userChosenColor);
userClickedPattern.push(userChosenColor);
// check users pattern with games pattern
checkAnswer(userClickedPattern.length - 1);
// console.log(`user pattern: ${userClickedPattern}`);
})
// reset values for game restart
function restartGame(){
gamePattern = [];
level = 0;
isGameStarted = false;
$(document).on("keydown", function () {
if(!isGameStarted) {
nextSequence();
}
});
}
function checkAnswer(currentLevel){
if(userClickedPattern[currentLevel] === gamePattern[currentLevel]){
success = true;
console.log("success");
}else{
success = false;
console.log("failure");
}
//failure leads to game over
if(!success){
$("body").addClass("game-over");
$("h1").text("Game Over, Press Any Key to Restart")
playSound("wrong");
setTimeout(function() {
$("body").removeClass("game-over");
}, 200);
//restart
restartGame();
}
//transition to next level after 1sec delay
setTimeout(function() {
if(success && currentLevel + 1 === gamePattern.length) {
nextSequence();
}
}, 1000);
}