-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorgame.js
121 lines (95 loc) · 2.89 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
//Selector
var numberOfSquare=6;
var color =GenerateRandomColor(numberOfSquare);
var square=document.querySelectorAll(".square");
var pickedcolor=choose();
var colordisplay=document.getElementById("spann");
colordisplay.textContent=pickedcolor;
var displaymessage=document.querySelector("#message");
var h1=document.querySelector("h1");
var resetButton=document.getElementById("reset");
var easyButton = document.querySelector("#easyb");
var hardButton = document.querySelector("#hardb");
//Display Message
for(var i=0;i<square.length;i++){
square[i].style.backgroundColor=color[i];
square[i].addEventListener("click" , function(){
var cc=this.style.backgroundColor;
if(cc===pickedcolor){
displaymessage.textContent="Correct!";
changecolor(cc);
h1.style.background=cc;
resetButton.textContent="Play Again?"
}
else{
this.style.backgroundColor="#232323";
displaymessage.textContent="Try Again!"
}
});
}
//Assigning Random colors
function changecolor(color) {
for(var i=0; i<square.length; i++){
square[i].style.backgroundColor=color;
}
}
//Random Color Generator
function choose() {
var pp=Math.floor(Math.random()*color.length);
return color[pp];
}
function GenerateRandomColor(num) {
var arr=[]
for(var i=0;i<num;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+")";
}
//Reset Button
resetButton.addEventListener("click",function(){
color =GenerateRandomColor(numberOfSquare);
pickedcolor=choose();
colordisplay.textContent=pickedcolor;
h1.style.background="steelblue";
resetButton.textContent="New Color";
displaymessage.textContent="";
for(var i=0;i<square.length;i++){
square[i].style.backgroundColor=color[i];
}
})
//Easy Button
easyButton.addEventListener("click",function(){
easyButton.classList.add("selected");
hardButton.classList.remove("selected");
numberOfSquare=3;
color =GenerateRandomColor(numberOfSquare);
pickedcolor=choose();
colordisplay.textContent=pickedcolor;
for(var i=0;i<square.length;i++){
if(color[i]){
square[i].style.backgroundColor=color[i];
}
else{
square[i].style.display="none";
}
}
})
//Hard Button
hardButton.addEventListener("click",function(){
hardButton.classList.add("selected");
easyButton.classList.remove("selected");
numberOfSquare=6;
color =GenerateRandomColor(numberOfSquare);
pickedcolor=choose();
colordisplay.textContent=pickedcolor;
for(var i=0;i<square.length;i++){
square[i].style.backgroundColor=color[i];
square[i].style.display="block";
}
})