-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgame.js
135 lines (119 loc) · 3.57 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// jshint esversion:6
// variables declarations
var randomChosenColor, randomNumber, buttonColors, userChosenColor;
var gamePattern = [],
userClickedPattern = [];
buttonColors = ["blue", "red", "green", "yellow"];
var started = false,
level = 0,
temp = 0;
// event handler for keypress
$(document).keypress(function () {
if (!started) {
$("#level-title").text("Level " + level);
nextSequence();
started = true;
$(".again").text("");
}
});
// playing sound which resembles to color of the btn
function playSound(name) {
var audio = new Audio("sounds/" + name + ".mp3");
audio.play();
}
// event handling (Click)
$(".btn").click(function () {
userChosenColor = $(this).attr("id");
userClickedPattern.push(userChosenColor);
playSound(userChosenColor);
animatePress(userChosenColor);
checkAnswer(userClickedPattern.length - 1);
});
// checking the correct answer for pattern
function checkAnswer(currentLevel) {
if ((userClickedPattern[currentLevel]) === gamePattern[currentLevel]) {
if (userClickedPattern.length === gamePattern.length) {
setTimeout(function () {
nextSequence();
}, 1080);
}
} else {
playSound("wrong");
$("body").addClass("game-over");
setTimeout(() => {
$("body").removeClass("game-over");
}, 200); // 200 milliseconds
$("#level-title").text("You Lose!");
$(".again").text("Press 'Start' to play again.");
score();
startOver();
setTimeout(function () {
restartGame();
}, 1100);
}
}
function restartGame() {
let restart = confirm("Restart the game?");
if (restart) {
restartingGame();
} else {
$("#level-title").text("Thanks for playing!");
}
}
function nextSequence() {
userClickedPattern = [];
level++;
temp += 5;
$("#level-title").text("Level " + level);
randomNumber = Math.floor(Math.random() * 4);
randomChosenColor = buttonColors[randomNumber];
playSound(randomChosenColor);
$("#" + randomChosenColor).fadeOut(150).fadeIn(100);
gamePattern.push(randomChosenColor);
}
function animatePress(currentColor) {
$("#" + currentColor).addClass("pressed");
setTimeout(function () {
$("#" + currentColor).removeClass("pressed");
}, 100);
}
function startOver() {
temp = 0;
level = 0;
gamePattern = [];
started = false;
document.getElementById('highestLevel').style.display = "block";
}
// click event for button so as to be optimized for mobile and tablet devices.
function restartingGame() {
if (!started) {
$("#level-title").text("Level " + level);
setTimeout(() => {
nextSequence();
}, 750);
started = true;
$(".again").text("");
}
}
$("button").click(function () {
if (!started) {
$("#level-title").text("Level " + level);
nextSequence();
started = true;
$(".again").text("");
}
});
// Displating score.
function score() {
$('#highestLevel').text("Your previous score: " + temp);
}
// How to play info
const info = document.getElementById("instruction");
const close = document.getElementById("close");
const card = document.getElementById("card");
info.addEventListener("click", function () {
card.style.visibility = "visible";
});
close.addEventListener("click", function () {
card.style.visibility = "hidden";
});