-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
141 lines (133 loc) · 5.58 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
var genPattern = [];
var userPattern = [];
var lvl = 1;
const btnColor = ["green", "red", "yellow", "blue"];
//GENERATE RANDOM NUMBERE HERE
function nexSeq()
{
var randoNum = Math.floor(Math.random()*4);
var randoChosenColor = btnColor[randoNum];
genPattern.push(randoChosenColor);
return randoChosenColor;
}
// PLAY SOUNDS HERE
function playSound(soundColor)
{
switch (soundColor)
{
case "green":
{
var sound = new Audio ('./sounds/green.mp3');
sound.play();
break;
}
case "red":
{
var sound = new Audio ('./sounds/red.mp3');
sound.play();
break;
}
case "yellow":
{
var sound = new Audio ('./sounds/yellow.mp3');
sound.play();
break;
}
case "blue":
{
var sound = new Audio ('./sounds/blue.mp3');
sound.play();
break;
}
default:
{
var sound = new Audio ('./sounds/wrong.mp3');
sound.play();
break;
}
}
}
// firstSeq
$("body").keypress(function()
{
if (lvl === 1)
{
userPattern = [];
genPattern = [];
$("body").css('background-color', '#011F3F');
$(".h2p").css('display', 'none');
$(".btn").click(function(event) {
if ($('#forModal').is(':checked') === true) {
document.getElementById("forModal").checked = false; // $("input[type='checkbox']")[0].checked works for jQuery
}
});
$("#level-title").text("Level " + lvl);
randoChosenColor = nexSeq();
//console.log("#" + randoChosenColor); // check output
// var t = $.inArray(randoChosenColor, btnColor) //This helps to get the index number
setTimeout(function () {
$("#" + randoChosenColor).fadeOut(100).fadeIn(150);
playSound(randoChosenColor);
}, 500);
// console.log("level " + lvl);
lvl++;
}
})
//click watch and click count
var currentNumOfClick = 0;
var hardStop = 0;
$(".btn").click(function()
{
if (lvl > 1)
{
currentNumOfClick++;
var clsColor = this.classList[1];
loop(clsColor);
}
})
//real game logic and subsequent Seq
function loop (clr)
{
hardStop = 0;
// console.log("<----- #" + clr + " $$$$$currentNumOfClick = " + currentNumOfClick);
$("#" + clr).fadeOut(100).fadeIn(150); //flash the buttons
userPattern.push(clr); //adds to userpattern list
// console.log("%%%&&& currentNumOfClick "+currentNumOfClick);
if (userPattern[currentNumOfClick - 1] !== genPattern[currentNumOfClick - 1])
{
hardStop = 1;
// console.log("wrong!!");
$("#level-title").text("Game Over, Press Any Key to Retry");
playSound();
$("body").css('background-color', 'red');
$(".h2p").css('display', 'flex');
lvl = 1;
currentNumOfClick = 0;
return;
}
else
{
if (currentNumOfClick === (lvl-1))
{
playSound(clr); // Play Sound
// console.log("%%%%% currentNumOfClick "+currentNumOfClick);
setTimeout(function () {
// console.log ("before check hardstop " + hardStop); // check check
if (hardStop === 0)
{
// console.log ("execution starts now"); //check check
$("#level-title").text("Level " + lvl);
userPattern =[];
randoChosenColor = nexSeq();
// console.log("-----> #" + randoChosenColor); // check output
$("#" + randoChosenColor).fadeOut(100).fadeIn(150);
playSound(randoChosenColor);
lvl++;
// console.log("%%%%% level "+ lvl);
currentNumOfClick = 0;
}}, 850);
return;
}
else { playSound(clr); return; }
}
}