-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
108 lines (88 loc) · 2.77 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
// Available Colors
var buttonColors = ["red", "blue", "green", "yellow"];
// Array to store game pattern
var gamePattern = [];
// Array to store pattern from user
var userClickedPattern = [];
// variable to check if the game is started
var started = false;
// initialising the game level as 0
var level = 0;
// Checking for a keypress to start the game
$(document).keypress(function () {
if (!started) {
nextSequence();
started = true;
}
});
// handling the user's button clicks.
$(".tile").click(function (e) {
var UserChosenColor = e.target.id;
userClickedPattern.push(UserChosenColor);
playSound(UserChosenColor);
animatePress(UserChosenColor);
// Checking the user's answer after each input
checkAnswer(userClickedPattern.length - 1);
});
// main function to determine the next sequence
function nextSequence() {
// emptying the user click pattern before starting the next level
userClickedPattern = [];
// Changing the level everytime the function is called
level++;
// change the heading to the current level
$("h1").text("level " + level);
// Creating a random number to choose next color
var randomNumber = Math.floor(Math.random() * 4);
var randomChosenColor = buttonColors[randomNumber];
// Pushing the random color generated to game pattern
gamePattern.push(randomChosenColor);
// Animating the game pattern color and playing a sound
$("#" + randomChosenColor)
.fadeIn(100)
.fadeOut(100)
.fadeIn(100);
playSound(randomChosenColor);
checkAnswer(level);
}
// A function to play sounds
function playSound(name) {
var audio = new Audio("sounds/" + name + ".mp3");
audio.play();
}
// A function to animate the buttons clicked by user
function animatePress(currentColor) {
$("#" + currentColor).addClass("pressed");
setTimeout(function () {
$("#" + currentColor).removeClass("pressed");
}, 100);
}
// A function to check the user input pattern
function checkAnswer(currentLevel) {
// checking if the pattern entered by the user is correct
if (userClickedPattern[currentLevel] === gamePattern[currentLevel]) {
console.log("success");
// checking if the user has entered the complete pattern
if (userClickedPattern.length === gamePattern.length) {
setTimeout(nextSequence, 1000);
}
} else {
console.log("wrong");
// Playing the wrong sound effect and the wrong animation
playSound("wrong");
$("body").addClass("game-over");
setTimeout(function () {
$("body").removeClass("game-over");
}, 200);
// changing the text of h1
$("h1").text("Game Over, Press Any Key to Restart");
// calling the startOver function
startOver();
}
}
// A startOver function to reset the variable values
function startOver() {
level = 0;
gamePattern = [];
started = false;
}