-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorGame.js
125 lines (98 loc) · 3.02 KB
/
colorGame.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
/*
var colors = [
"rgb(0, 255, 255)", /*spaces is important!
"rgb(255, 0, 255)",
"rgb(255, 255, 0)",
"rgb(0, 0, 255)",
"rgb(0, 255, 0)",
"rgb(255, 0, 0)"
]
*/
var squaresNum = 6;
var colors = generateRandomColors(squaresNum);
var guessedColor = pickColor();
var colorTitle = document.querySelector("#guessedColor");
colorTitle.textContent = guessedColor;
var messageDisplay = document.querySelector("#message");
var h1Display = document.querySelector("h1");
var newGame = document.querySelector("#reset");
var buttons = document.querySelectorAll(".square");
var easyButton = document.querySelector("#easy");
var hardButton = document.querySelector("#hard");
easyButton.addEventListener("click", function() {
squaresNum = 3;
this.classList.add("selected");
hardButton.classList.remove("selected");
initiate();
});
hardButton.addEventListener("click", function() {
squaresNum = 6;
this.classList.add("selected");
easyButton.classList.remove("selected");
initiate();
});
newGame.addEventListener("click", initiate);
for (var i=0; i<buttons.length; i++) {
buttons[i].style.backgroundColor = colors[i]; /*backgroundColor is more compatible than background*/
console.log("button color: "+colors[i]);
//add listeners
buttons[i].addEventListener("click", function() {
var clickedColor = this.style.backgroundColor; /* must use 'this'*/
//alert(clickedColor);
if(clickedColor === guessedColor) {
messageDisplay.textContent = "Bingo";
changeColors();
newGame.textContent = "Play Again?";
} else {
//this.style.transition = "0.6s";
this.style.backgroundColor = "#232323";
//this.classList.add("body");
console.log("clicked: "+clickedColor);
messageDisplay.textContent = "Incorrect! Try again!";
}
});
}
function initiate() {
colors = generateRandomColors(squaresNum);
console.log(colors);
guessedColor = pickColor();
console.log("golden: "+guessedColor);
colorTitle.textContent = guessedColor;
messageDisplay.textContent = "";
newGame.textContent = "NEW GAME";
h1Display.style.backgroundColor = "grey";
for (var i=0; i<buttons.length; i++) {
//set colors
if(i >= squaresNum) {
buttons[i].style.display = "none"; //disappear, opposite is "block"
} else {
buttons[i].style.display = "block";
buttons[i].style.backgroundColor = colors[i]; /*backgroundColor is more compatible than background*/
}
}
}
function changeColors() {
for (var i=0; i<buttons.length; i++) {
buttons[i].style.backgroundColor = guessedColor;
}
h1Display.style.backgroundColor = guessedColor;
}
function pickColor() {
var index = Math.floor(Math.random() * colors.length);
return colors[index];
}
function generateRandomColors(num) {
var arr = [];
for (var i = 0; i < num; i++) {
//arr[i] = randomRGB();
arr.push(randomRGB());
}
return arr;
}
function randomRGB() {
var colorR = Math.floor(Math.random() * 256); /* 0-255*/
var colorG = Math.floor(Math.random() * 256);
var colorB = Math.floor(Math.random() * 256);
var RGB = "rgb(" + colorR + ", " + colorG + ", " +colorB + ")";
return RGB;
}