-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRGBs.js
183 lines (166 loc) · 5.56 KB
/
RGBs.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
177
178
179
180
181
182
183
// number of colors to generate by default
var numSquares = 6;
// stores the generated random colors
var colors = [];
// stores the correct color
var pickedColor;
// select all squares in the container
var squares = document.querySelectorAll(".square");
//select the colorDisplay element
var colorDisplay = document.getElementById("colorDisplay");
// select the message element
var messageDisplay = document.querySelector("#message");
// select the h1 element
var h1 = document.querySelector("h1");
// select the reset button
var resetButton = document.querySelector("#reset");
//select all mode buttons
var modeButtons = document.querySelectorAll(".mode");
var correctMsg = document.querySelector("#correctMsg");
var wrongMsg = document.querySelector("#wrongMsg");
var resetAudio = document.getElementById("resetAudio");
var positiveAudio = document.getElementById("positiveAudio");
var negativeAudio = document.getElementById("negativeAudio");
//function to run at the beginning
init();
function init() {
setupModeButtons();
setupSquares();
reset();
}
// Setup the Game Mode Buttons
function setupModeButtons() {
// check which mode the player has set (EASY / HARD)
for (var i = 0; i < modeButtons.length; i++) {
modeButtons[i].addEventListener("click", function () {
// remove the 'active' class from each mode button
modeButtons[0].classList.remove("active");
modeButtons[1].classList.remove("active");
modeButtons[2].classList.remove("active");
//add the 'active' class to the clicked button
this.classList.add("active");
// check which mode of the clicked button & give correct num of squares
switch (this.textContent) {
case "Easy":
numSquares = 3;
break;
case "Medium":
numSquares = 6;
break;
default:
numSquares = 9;
break;
}
// reset the game
reset();
});
}
}
// Setup the Squares
function setupSquares() {
for (var i = 0; i < squares.length; i++) {
squares[i].addEventListener("click", function () {
// grab bg color of clicked square
var clickedColor = this.style.background;
if (clickedColor === pickedColor) {
messageDisplay.parentElement.className = "";
messageDisplay.parentElement.classList.add("stripe-correct");
messageDisplay.textContent = "Correct!";
positiveAudio.play();
resetButton.textContent = "Play Again?";
animateCSS("#message", "bounceIn");
// change bg color of all squares to the picked color
changeColors(clickedColor);
h1.style.background = pickedColor;
} else {
// make the selected square have same color of the bg
this.style.background = "#232323"; // dark greyish colory
this.style.visibility = "hidden";
messageDisplay.parentElement.className = "";
messageDisplay.parentElement.classList.add("stripe-wrong");
messageDisplay.textContent = "Try Again!";
negativeAudio.play();
// Play some animations
animateCSS("#message", "headShake").then((msg) => {
animateCSS(".stripe-wrong", "fadeOut").then((msg) => {
messageDisplay.textContent = "";
messageDisplay.parentElement.className = "";
messageDisplay.parentElement.classList.add("stripe");
});
});
}
});
}
}
// Resets the game
function reset() {
resetAudio.play();
// generate all new colors for 'numSquares'
colors = generateRandomColors(numSquares);
// pick new random color from array (The correct color)
pickedColor = pickColor();
// change colorDisplay text to match picked color
colorDisplay.textContent = "Find: " + pickedColor;
animateCSS("#colorDisplay", "backInLeft").then((msg) => {
animateCSS("#colorDisplay", "flash");
});
// button to show new colors on reset
resetButton.textContent = "New Colors";
// message display to be empty
messageDisplay.textContent = "";
messageDisplay.parentElement.className = "";
messageDisplay.parentElement.classList.add("stripe");
// change colors of squares & display style
for (var i = 0; i < squares.length; i++) {
// if color[i] exists,
if (colors[i]) {
// make square[i] visible
squares[i].style.visibility = "visible";
squares[i].style.display = "block";
// then add bg color to square[i]
squares[i].style.background = colors[i];
} else {
// else don't display them
squares[i].style.display = "none";
}
}
// change the h1 background color
h1.removeAttribute("style");
h1.classList.add("cool-gradient");
}
//reset the game when the 'Play again' or 'New Colors' button is clicked
resetButton.addEventListener("click", function () {
reset();
});
//Loop through all squares to match the correct color
function changeColors(color) {
for (var i = 0; i < squares.length; i++) {
squares[i].style.visibility = "visible";
squares[i].style.background = color;
}
}
//Selectes a random color from our array of colors
function pickColor() {
var random = Math.floor(Math.random() * colors.length);
return colors[random];
}
//Generates Random colors and stores them in an array
function generateRandomColors(num) {
var arr = [];
//add 'num' random colors into array
for (var i = 0; i < num; i++) {
arr.push(randomColor());
}
return arr;
}
//Create a random rgb color
function randomColor() {
//pick a "red" from 0 - 255
var r = Math.floor(Math.random() * 256);
//pick a "green" from 0 - 255
var g = Math.floor(Math.random() * 256);
//pick a "blue" from 0 - 255
var b = Math.floor(Math.random() * 256);
//put random color in rgb format
return "rgb(" + r + ", " + g + ", " + b + ")";
}