forked from elfreki/ColorGame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolorgame.js
More file actions
134 lines (130 loc) · 4.58 KB
/
colorgame.js
File metadata and controls
134 lines (130 loc) · 4.58 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
var colors = colorarray(6);
var squares = document.querySelectorAll(".square");
var selected = pickcolor();
var picked = document.getElementById("picked");
var message = document.getElementById("message");
var body = document.getElementsByTagName("body");
var head = document.getElementById("headcontain");
var easy = document.getElementById("easy");
var hard = document.getElementById("hard");
picked.textContent = selected;
var reset = document.getElementById("new");
easy.addEventListener("click",function(){
easy.classList.add("selectedd");
hard.classList.remove("selectedd");
message.textContent = "";
for(var i=0;i<squares.length;i++){
squares[i].style.backgroundColor = "#232323";
}
colors = colorarray(3);
selected = pickcolor();
picked.textContent = selected;
head.style.background = "steelblue";
for(var i=0;i<squares.length;i++){
squares[i].style.backgroundColor = colors[i];
squares[i].addEventListener("click",function(){
var clicked = this.style.backgroundColor;
if(clicked==selected){
message.textContent = "correct!";
reset.textContent = "Play again?";
for(var i=0;i<colors.length;i++){
squares[i].style.backgroundColor = selected;
}
head.style.background = selected;
}
else{
this.style.backgroundColor = "#232323";
message.textContent = "try again!";
}
})
}
})
hard.addEventListener("click",function(){
easy.classList.remove("selectedd");
hard.classList.add("selectedd");
message.textContent = "";
colors = colorarray(6);
selected = pickcolor();
picked.textContent = selected;
head.style.background = "steelblue";
for(var i=0;i<squares.length;i++){
squares[i].style.backgroundColor = colors[i];
squares[i].addEventListener("click",function(){
var clicked = this.style.backgroundColor;
if(clicked==selected){
message.textContent = "correct!";
reset.textContent = "Play again?";
for(var i=0;i<colors.length;i++){
squares[i].style.backgroundColor = selected;
}
head.style.background = selected;
}
else{
this.style.backgroundColor = "#232323";
message.textContent = "try again!";
}
})
}
})
reset.addEventListener("click",function(){
easy.classList.remove("selectedd");
hard.classList.add("selectedd");
colors = colorarray(6);
message.textContent = "";
selected = pickcolor();
reset.textContent = "new color";
picked.textContent = selected;
head.style.background = "steelblue";
for(var i=0;i<squares.length;i++){
squares[i].style.backgroundColor = colors[i];
squares[i].addEventListener("click",function(){
var clicked = this.style.backgroundColor;
if(clicked==selected){
message.textContent = "correct!";
reset.textContent = "Play again?";
for(var i=0;i<colors.length;i++){
squares[i].style.backgroundColor = selected;
}
head.style.background = selected;
}
else{
this.style.backgroundColor = "#232323";
message.textContent = "try again!";
}
})
}
})
for(var i=0;i<squares.length;i++){
squares[i].style.backgroundColor = colors[i];
squares[i].addEventListener("click",function(){
var clicked = this.style.backgroundColor;
if(clicked==selected){
message.textContent = "correct!";
for(var i=0;i<colors.length;i++){
squares[i].style.backgroundColor = selected;
}
head.style.background = selected;
}
else{
this.style.backgroundColor = "#232323";
message.textContent = "try again!";
}
})
}
function colorarray(x){
var arr=[];
for(var i=0;i<x;i++){
arr.push(gencolor());
}
return arr;
}
function gencolor(){
var r = Math.floor(Math.random() * 256);
var b = Math.floor(Math.random() * 256);
var g = Math.floor(Math.random() * 256);
return "rgb(" + r + ", " + g + ", " + b + ")";
}
function pickcolor(){
var index = Math.floor(Math.random() * colors.length);
return colors[index];
}