-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorGame.js
105 lines (91 loc) · 2.9 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
var colors = [];
var numColors = 6;
var pickedColor;
var rgbColor = document.querySelector("#rgbColor");
var squares = document.querySelectorAll(".square");
var easybtn = document.querySelector("#easy");
var hardbtn = document.querySelector("#hard");
var resetbtn=document.querySelector("#reset");
var messageDisplay=document.getElementById("messageDisplay");
var h1=document.querySelector("h1");
colors = generateColors(numColors);
generateSquares();
pickedColor = pickColor();
rgbColor.textContent = pickedColor;
easybtn.addEventListener("click", function () {
messageDisplay.textContent="";
resetbtn.textContent="New Colors";
this.classList.add("selected");
hardbtn.classList.remove("selected" );
numColors = 3;
colors = generateColors(numColors);
generateSquares();
pickedColor = pickColor();
rgbColor.textContent = pickedColor;
});
hardbtn.addEventListener("click",function(){
messageDisplay.textContent="";
resetbtn.textContent="New Colors";
this.classList.add("selected");
easybtn.classList.remove("selected" );
numColors = 6;
colors = generateColors(numColors);
generateSquares();
pickedColor = pickColor();
rgbColor.textContent = pickedColor;
});
resetbtn.addEventListener("click",function(){
messageDisplay.textContent="";
resetbtn.textContent="New Colors";
colors=generateColors(numColors);
generateSquares();
pickedColor=pickColor();
rgbColor.textContent=pickedColor;
h1.style.backgroundColor="steelblue";
})
function pickColor() {
return colors[Math.floor(Math.random() * colors.length)];
}
function generateColors(numColors) {
var arr = [];
for (var i = 0; i < numColors; i++) {
arr.push(randomColor());
}
return arr;
}
function randomColor() {
var r = Math.floor(Math.random() * 256);
var g = Math.floor(Math.random() * 256);
var b = Math.floor(Math.random() * 256);
return "rgb(" + r + ", " + g + ", " + b + ")";
}
function generateSquares() {
console.log(colors);
for (var i = 0; i < squares.length; i++) {
if (colors[i]) {
squares[i].style.display="block";
squares[i].style.backgroundColor = colors[i];
}
else {
squares[i].style.display="none";
}
squares[i].addEventListener("click",function(){
var selectedColor=this.style.backgroundColor;
if(selectedColor===pickedColor){
changeColors(selectedColor);
messageDisplay.textContent="Correct!";
resetbtn.textContent="Play Again?";
h1.style.backgroundColor=selectedColor;
}
else{
messageDisplay.textContent="Try Again!!"
this.style.backgroundColor="#232323";
}
});
}
}
function changeColors(color){
for(var i=0;i<squares.length;i++){
squares[i].style.backgroundColor=color;
}
}