-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRGBgame.js
More file actions
106 lines (91 loc) · 2.44 KB
/
RGBgame.js
File metadata and controls
106 lines (91 loc) · 2.44 KB
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
var siz = 6 ;
var colors = rand_colors(siz) ;
var squares = document.getElementsByClassName("square") ;
var ans = colors[rand_no()] ;
var msgDisplay = document.getElementById("msg") ;
document.getElementById("rgb").textContent = ans ;
var head = document.querySelector(".head") ;
for(var i=0;i<colors.length;i++)
{
squares[i].style.background = colors[i] ;
squares[i].addEventListener("click" , function(){
if(this.style.background === ans)
{ msgDisplay.textContent = "Correct" ;
new_colorB.textContent = "Play Again" ;
rightanswer() ;
}
else {
this.style.background = "#232323" ;
msgDisplay.textContent = "Nope" ;
}
});
}
var new_colorB = document.querySelector(".newcolors") ;
new_colorB.addEventListener("click" , function(){
new_colorB.textContent = "New Colors" ;
colors = rand_colors(siz) ;
ans = colors[rand_no()] ;
document.getElementById("rgb").textContent = ans ;
for(var i=0;i<squares.length;i++)
{
if(siz==3 && i>=3) squares[i].style.background = "#232323" ;
else squares[i].style.background = colors[i] ;
}
head.style.background = "steelblue" ;
msgDisplay.textContent = "" ;
})
var easyb = document.getElementById("easy") ;
var hardb = document.getElementById("hard") ;
easyb.addEventListener("click" , function(){
siz = 3 ;
this.classList.add("selected") ;
hardb.classList.remove("selected") ;
for(var q=3;q<squares.length;q++)
squares[q].style.background = "#232323" ;
utility() ;
}) ;
hardb.addEventListener("click" , function(){
siz = 6 ;
this.classList.add("selected") ;
easyb.classList.remove("selected") ;
utility();
}) ;
function utility()
{
new_colorB.textContent = "New Colors" ;
colors = rand_colors(siz) ;
ans = colors[rand_no()] ;
document.getElementById("rgb").textContent = ans ;
for(var i=0;i<colors.length;i++)
squares[i].style.background = colors[i] ;
head.style.background = "steelblue" ;
msgDisplay.textContent = "" ;
}
function rightanswer()
{
for(var j=0;j<squares.length;j++)
{
squares[j].style.background = ans ;
}
document.querySelector(".head").style.background = ans ;
}
function rand_no()
{
return ( Math.floor( Math.random()*colors.length ) ) ;
}
function rand_colors(si)
{
var arr=[] ;
for(var k=0;k<si;k++)
{
arr.push(rand_color()) ;
}
return arr ;
}
function rand_color()
{
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+ ")" ) ;
}