-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.js
91 lines (75 loc) · 2.16 KB
/
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
var message = document.querySelector("#message");
var h1 = document.querySelector("h1");
var reset = document.querySelector("#reset");
var squares = document.querySelectorAll(".square");
var colorPicked = document.querySelector("#colorPicked");
var modes = document.querySelectorAll(".mode");
var numSquares = 6;
var colors = generateColors(numSquares);
var colorAnswer = randomColor();
resetNow();
for(var i = 0; i < modes.length; i++) {
modes[i].addEventListener("click", function() {
modes[0].classList.remove("selected");
modes[1].classList.remove("selected");
this.classList.add("selected");
if(this.textContent === "Easy")
numSquares = 3;
else
numSquares = 6;
resetNow();
})
}
function resetNow() {
reset.textContent = "New colors";
colors = generateColors(numSquares);
h1.style.backgroundColor = "steelblue";
colorAnswer = randomColor();
colorPicked.textContent = colorAnswer;
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";
}
message.textContent = "";
}
reset.addEventListener("click", function() {
resetNow();
});
for (var i = 0; i < colors.length; i++) {
squares[i].style.backgroundColor = colors[i];
squares[i].addEventListener("click", function() {
var clickedColor = this.style.backgroundColor;
if(clickedColor === colorAnswer) {
message.textContent = "Correct!";
h1.style.backgroundColor = clickedColor;
correctColor(clickedColor);
reset.textContent = "Play again?";
}
else {
this.style.backgroundColor = "black";
message.textContent = "Try again!";
}
});
}
function generateColors(num) {
var generatedColors = [];
for (var i = 0; i < num; i++) {
generatedColors[i] = "rgb(" + Math.round(Math.random()*255) + ", "
+ Math.round(Math.random()*255) + ", "
+ Math.round(Math.random()*255) + ")";
}
return generatedColors;
}
function correctColor(color) {
for (var i = 0; i < colors.length; i++) {
squares[i].style.backgroundColor = color;
}
}
function randomColor() {
var index = (Math.round(Math.random() * colors.length));
return colors[index];
}