-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorGame.js
118 lines (105 loc) · 3.03 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
var squares = document.querySelectorAll(".square");
var colorDisplay = document.getElementById("colorDisplay");
var messageDisplay = document.querySelector("#message");
var h1 = document.querySelector("h1");
var resetButton = document.querySelector("#reset");
var modeButtons = document.querySelectorAll(".mode");
var difficultyMode;
var pickedColor;
var colors;
init();
function init() {
setUpModeButtons();
resetButton.addEventListener("click", reset);
difficultyMode = 6;
colors = generateRandomColors(difficultyMode);
pickedColor = pickColor(colors);
colorDisplay.textContent = pickedColor;
setUpSquares();
}
function reset() {
colors = generateRandomColors(difficultyMode);
pickedColor = pickColor(colors);
colorDisplay.textContent = pickedColor;
resetButton.textContent = "New Colors";
messageDisplay.textContent = "";
h1.style.backgroundColor = "steelblue";
for (let i = 0; i < squares.length; i++) {
const square = squares[i];
if (!colors[i] && square.style.display === "none") {
break;
} else if (!colors[i]) {
square.style.display = "none";
continue;
}
square.style.display = "block";
square.style.backgroundColor = colors[i];
}
}
function setUpModeButtons() {
for (let i = 0; i < modeButtons.length; i++) {
const btn = modeButtons[i];
btn.addEventListener("click", modeButtonListener);
}
}
function modeButtonListener() {
if (this.textContent === "Easy") {
if (difficultyMode === 3) return;
difficultyMode = 3;
} else {
if (difficultyMode === 6) return;
difficultyMode = 6;
}
for (let index = 0; index < modeButtons.length; index++) {
const auxbtn = modeButtons[index];
auxbtn.classList.remove("selected");
}
this.classList.add("selected");
reset();
}
function setUpSquares() {
for (let i = 0; i < squares.length; i++) {
const square = squares[i];
if (!colors[i]) {
square.style.display = "none";
} else {
square.style.backgroundColor = colors[i];
}
square.addEventListener("click", squareListener);
}
}
function squareListener() {
var clickedColor = this.style.backgroundColor;
if (clickedColor === pickedColor) {
messageDisplay.textContent = "Correct!";
changeColors(clickedColor);
h1.style.backgroundColor = clickedColor;
resetButton.textContent = "Play Again?";
} else {
this.style.backgroundColor = "#232323";
messageDisplay.textContent = "Try Again";
}
}
function changeColors(color) {
for (let i = 0; i < squares.length; i++) {
const square = squares[i];
square.style.backgroundColor = color;
}
}
function pickColor(colors) {
var random = Math.floor(Math.random() * colors.length);
return colors[random];
}
function generateRandomColors(num) {
var arr = [];
for (let i = 0; i < num; i++) {
arr.push(randomColor());
}
return arr;
}
function randomColor() {
var red = Math.floor(Math.random() * 256);
var green = Math.floor(Math.random() * 256);
var blue = Math.floor(Math.random() * 256);
return "rgb(" + red + ", " + green + ", " + blue + ")";
}