-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
176 lines (134 loc) · 3.96 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
var buttonColours = ["red", "blue", "green", "yellow"];
var gamePattern = [];
var userClickedPattern = [];
var started = false;
var level = 0;
var value = -1;
var helpOpened = false;
var abtOpened = false;
var playing = false;
$("#about").click(function(){
if (abtOpened){
$(".btn").slideDown(200);
$("#help").text("Show help");
$("#foot").text("@Jems Apps and Games");
$("body").removeClass("abtBody");
$("#jemPic").fadeOut(300);
$("#bioTxt").fadeOut(300);
$("#bioTxt2").fadeOut(300);
$("#bioTxt3").fadeOut(300);
abtOpened = false
} else{
$(".btn").slideUp(200);
$("#help").text("Close help");
$("body").addClass("abtBody");
$("#jemPic").fadeIn(300);
$("#bioTxt").fadeIn(300);
$("#bioTxt2").fadeIn(300);
$("#bioTxt3").fadeIn(300);
abtOpened = true;
}
});
$("#help").click(function(){
if (helpOpened){
$(".btn").slideDown(200);
$("#help").text("Show help");
$("#foot").text("@Jems Apps and Games");
helpOpened = false;
} else{
$(".btn").slideUp(200);
$("#foot").text("First follow the pattern generated by the game-play and try to remember the previous patterns drawn by it then simply append them at the back of the current game-play. For example, if the computer generated Red-Red-Green using three consicutive steps, then you should remember all those three patterns generated by the game-play and you should respond accordingly.");
$("#help").text("Close help");
helpOpened = true;
}
});
$(document).keydown(function() {
if (!started) {
nextSequence();
started = true;
playMid();
}
});
$("#startButton").click(function(){
if (!started) {
nextSequence();
started = true;
playMid();
}
});
$(".btn").click(function() {
var userChosenColour = $(this).attr("id");
userClickedPattern.push(userChosenColour);
playSound(userChosenColour);
animatePress(userChosenColour);
checkAnswer(userClickedPattern.length-1);
});
function checkAnswer(currentLevel) {
if (gamePattern[currentLevel] === userClickedPattern[currentLevel]) {
if (userClickedPattern.length === gamePattern.length){
setTimeout(function () {
nextSequence();
}, 1000);
}
}
else {
playSound("wrong");
$("body").addClass("game-over");
$("#level-title").text("Game Over, Press Any Key to Restart");
setTimeout(function () {
$("body").removeClass("game-over");
}, 200);
startOver();
setTimeout(function (){
var variables = [];
for (var x = 1; x <= userClickedPattern.length; x++ ){
variables.push(" "+userClickedPattern[x]);
}
}, 0)
}
}
function nextSequence() {
userClickedPattern = [];
level++;
$("#level-title").text("Level " + level);
var randomNumber = Math.floor(Math.random() * 4);
var randomChosenColour = buttonColours[randomNumber];
gamePattern.push(randomChosenColour);
$("#" + randomChosenColour).fadeIn(100).fadeOut(100).fadeIn(100);
playSound(randomChosenColour);
if(!playing){
playMid();
}
}
function animatePress(currentColor) {
$("#" + currentColor).addClass("pressed");
setTimeout(function () {
$("#" + currentColor).removeClass("pressed");
}, 100);
}
function playMid(){
var whilePlay = new Audio("sounds/while.wav");
if(!playing){
whilePlay.play();
playing = true;
whilePlay.addEventListener("ended", ()=> {
playing = false;
whilePlay.play();
});
}
}
function playSound(name) {
var audio = new Audio("sounds/" + name + ".mp3");
audio.play();
}
function startOver() {
if(level > value)
{
value = level;
$("#thiss").text("Max Level: " + value);
$("#thiss").fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);
}
level = 0;
gamePattern = [];
started = false;
}