-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor_game.js
113 lines (90 loc) · 1.92 KB
/
color_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
var numOfSq = 6;
var colors;
var cor_col;
var h1 = document.querySelector("h1");
var sqlst = document.querySelectorAll(".square");
var resetBut = document.querySelector("#reset");
var modlst = document.querySelectorAll(".mode");
init();
function init()
{
for(var j = 0;j<modlst.length;j++)
{
modlst[j].addEventListener("click",function(){
this.textContent === "Easy"? numOfSq=3: numOfSq=6;
modlst[0].classList.remove("selected");
modlst[1].classList.remove("selected");
this.classList.add("selected");
reset();
})
}
for(var i =0;i<sqlst.length; i++)
{
sqlst[i].addEventListener("click",function()
{
if(this.style.background===cor_col)
{
msg.textContent = "Correct";
change_colors(cor_col);
h1.style.background = cor_col;
resetBut.textContent = "Play Again?";
}
else
{
this.style.background = "#232323";
msg.textContent = "Try Again";
}
})
}
reset();
}
resetBut.addEventListener("click",reset);
function reset()
{
colors = generateSqColors(numOfSq);
cor_col = pickColor();
setColor();
colorh1.textContent = cor_col;
h1.style.background = "steelblue";
resetBut.textContent = "Change Color";
msg.textContent = "";
}
function setColor()
{
for(var i =0;i<sqlst.length;i++)
{
if(colors[i])
{
sqlst[i].style.display = "block";
sqlst[i].style.background = colors[i];
}
else
sqlst[i].style.display = "none";
}
}
function change_colors(color){
for(var i =0;i<sqlst.length;i++)
{
sqlst[i].style.background = color;
}
}
function pickColor(){
var rand = Math.floor(Math.random()*colors.length);
return colors[rand];
}
function generateSqColors(numc){
var arr = [];
for(var i =0;i<numc;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);
var col = "rgb("+r+", "+g+", "+b+")";
return col;
}