-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
114 lines (94 loc) · 2.98 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
var buttonColours = ["red", "blue", "green", "yellow"];
//pattern created by the system
var gamePattern = [];
//pattern entered by the user
var userClickedPattern=[];
//checks if game started or not
var started = false;
//maintains the count of level we are currently playing
var level =0;
//to start the game user must enter any key
$(document).keypress(function(){
//only works for the first key press,,started variable helps in achieving it
if(!started){
//game started with level 0
$("#level-title").text("Level "+level);
//generate the sequence
nextSequence();
started=true;
}
});
//stores the pattern entered by user
$(".btn").click(function(){
//selecting the particular box based on its id
var userChosenColour = $(this).attr("id");
userClickedPattern.push(userChosenColour);
//console.log(userClickedPattern);
playSound(userChosenColour);
animatePress(userChosenColour);
checkAnswer(userClickedPattern.length -1);
});
//checking if user and game patterns are same or not
function checkAnswer(currentLevel)
{
//when user pattern is correct
//checking if input entered at that order is correct or not
if(gamePattern[currentLevel] === userClickedPattern[currentLevel])
{
console.log("success");
//when all the inputs have been covered
if(userClickedPattern.length === gamePattern.length)
{
//delay before next pattern gets displayed
setTimeout(function(){
nextSequence();
},1000);
}
}
//when user pattern is wrong
else
{
console.log("wrong");
playSound("wrong");
$("body").addClass("game-over");
setTimeout(function(){
$("body").removeClass("game-over");
},200);
$("#level-title").text("Game Over, Press Any Key to Restart");
startOver()
}
}
//generates the sequence of color
function nextSequence()
{
userClickedPattern = [] ;
//level is incremented
level++;
$("#level-title").text("Level "+level);
//choosing a random number to generatecolor patterns
var randomNumber = Math.floor((Math.random()*4));
var randomChosenColour = buttonColours[randomNumber]; //associating the random number with a color
gamePattern.push(randomChosenColour); //adding the random color to the array
//tells this color is the next in sequence by showing animation
$("#"+randomChosenColour).fadeIn(100).fadeOut(100).fadeIn(100); //animation for the color chosen
playSound(randomChosenColour); //play sound for the color chosen
}
//to play the sound
function playSound(name){
var audio = new Audio("sounds/" + name + ".mp3");
audio.play();
}
//restart the game after game is over
function startOver()
{
level=0;
started=false;
gamePattern=[];
}
//animation for buttons when user presses it
function animatePress(currentColor) {
$("#" + currentColor).addClass("pressed");
setTimeout(function () {
$("#" + currentColor).removeClass("pressed");
}, 100);
}