Skip to content

Commit c0d2dc2

Browse files
committed
added colorgame
0 parents  commit c0d2dc2

File tree

3 files changed

+223
-0
lines changed

3 files changed

+223
-0
lines changed

colorGame.css

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
body {
2+
background-color: #232323;
3+
margin: 0;
4+
font-family: "Montserrat", "Avenir";
5+
}
6+
7+
.square {
8+
9+
width: 30%;
10+
background-color: purple;
11+
padding-bottom: 30%;
12+
float: left;
13+
margin: 1.66%;
14+
border-radius: 20%;
15+
transition: background 0.6s;
16+
-webkit-transition:background 0.6s;
17+
-moz-transition:background 0.6s;
18+
}
19+
20+
#container {
21+
margin: 20px auto;
22+
max-width: 600px;
23+
}
24+
25+
h1 {
26+
text-align: center;
27+
line-height: 1.1;
28+
font-weight: normal;
29+
color: white;
30+
background:steelblue;
31+
margin: 0;
32+
text-transform: uppercase;
33+
padding-top: 20px;
34+
padding-bottom: 20px;
35+
}
36+
37+
#colorDisplay {
38+
font-size: 200%;
39+
}
40+
41+
#messageDisplay {
42+
display: inline-block;
43+
width: 20%;
44+
}
45+
46+
#stripe {
47+
48+
background-color: white;
49+
height: 30px;
50+
text-align: center;
51+
color: black;
52+
}
53+
54+
.selected {
55+
background: steelblue;
56+
color: white;
57+
}
58+
59+
button {
60+
border: none;
61+
background: none;
62+
text-transform: uppercase;
63+
height: 100%;
64+
font-weight: 700;
65+
color: steelblue;
66+
letter-spacing: 1px;
67+
font-size: inherit;
68+
transition: all 0.3s;
69+
-webkit-transition:all 0.3s;
70+
-moz-transition:all 0.3s;
71+
outline: none;
72+
}
73+
74+
button:hover {
75+
76+
color: white;
77+
background: steelblue;
78+
}

colorGame.html

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Color Game</title>
5+
<link rel="stylesheet" type="text/css" href="colorGame.css">
6+
</head>
7+
<body>
8+
<h1>
9+
The Great
10+
<br>
11+
<span id="colorDisplay">RGB</span>
12+
<br>
13+
Color Game
14+
</h1>
15+
16+
17+
<div id="stripe">
18+
<button id="reset">New Colors</button>
19+
<span id="messageDisplay"></span>
20+
<button class="mode">Easy</button>
21+
<button class="mode selected">Hard</button>
22+
</div>
23+
24+
<div id="container">
25+
<div class="square"></div>
26+
<div class="square"></div>
27+
<div class="square"></div>
28+
<div class="square"></div>
29+
<div class="square"></div>
30+
<div class="square"></div>
31+
</div>
32+
33+
34+
35+
36+
<script type="text/javascript" src="colorGame.js"></script>
37+
</body>
38+
</html>

colorGame.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
var numSquares = 6;
2+
var colors = generateRandomColors(numSquares);
3+
var squares = document.querySelectorAll(".square");
4+
var pickedColor = pickColor();
5+
var colorDisplay = document.getElementById('colorDisplay');
6+
var messageDisplay = document.querySelector("#messageDisplay");
7+
var h1 = document.querySelector("h1");
8+
var resetButton = document.querySelector("#reset");
9+
var easyBtn = document.querySelector("#easyBtn");
10+
var hardBtn = document.querySelector("#hardBtn");
11+
12+
var modeButtons = document.querySelectorAll(".mode");
13+
14+
for (var i = 0; i < modeButtons.length; i++) {
15+
modeButtons[i].addEventListener("click", function(){
16+
modeButtons[0].classList.remove("selected");
17+
modeButtons[1].classList.remove("selected");
18+
this.classList.add("selected");
19+
this.textContent === "Easy" ? numSquares = 3: numSquares = 6;
20+
reset();
21+
});
22+
}
23+
24+
function reset(){
25+
colors = generateRandomColors(numSquares);
26+
//pick a new random color
27+
pickedColor = pickColor();
28+
//change the color display
29+
colorDisplay.textContent = pickedColor;
30+
resetButton.textContent = "NEW COLORS";
31+
messageDisplay.textContent = "";
32+
//change the color of the square
33+
for (var i = 0; i < squares.length; i++) {
34+
if(colors[i]){
35+
squares[i].style.display = "block";
36+
squares[i].style.backgroundColor = colors[i];
37+
} else {
38+
squares[i].style.display = "none"; }
39+
}
40+
h1.style.backgroundColor = "steelblue";
41+
}
42+
43+
resetButton.addEventListener("click", function(){
44+
reset();
45+
46+
})
47+
48+
colorDisplay.textContent = pickedColor;
49+
50+
for (var i = 0; i < squares.length; i++) {
51+
//setting initial colors to squares
52+
squares[i].style.backgroundColor = colors[i];
53+
//setting eventListeners on square
54+
squares[i].addEventListener("click", function(){
55+
//grab color of clicked square
56+
var clickedColor = this.style.backgroundColor;
57+
//check if the pickedColor is same as correctColor
58+
if(clickedColor === pickedColor){
59+
messageDisplay.textContent = "CORRECT!";
60+
changeColors(clickedColor);
61+
h1.style.backgroundColor = clickedColor;
62+
resetButton.textContent = "PLAY AGAIN!";
63+
} else{
64+
this.style.backgroundColor = "#232323";
65+
messageDisplay.textContent = "TRY AGAIN!";
66+
}
67+
});
68+
69+
};
70+
71+
function changeColors(color){
72+
73+
//loop through all squares
74+
for (var i = 0; i < squares.length; i++) {
75+
//change each color to match correct color
76+
squares[i].style.backgroundColor = color;
77+
};
78+
};
79+
80+
function pickColor(){
81+
var random = Math.floor(Math.random() * colors.length);
82+
return colors[random];
83+
};
84+
85+
function generateRandomColors(num){
86+
// create array
87+
var arr = [];
88+
// add random colors to array (push)
89+
for (var i = 0; i < num; i++) {
90+
arr.push(randomColor())
91+
}
92+
// return array
93+
return arr;
94+
}
95+
96+
function randomColor(){
97+
// pick a "red" from 0 - 255
98+
var r = Math.floor(Math.random() * 256);
99+
100+
// pick a "green" from 0 - 255
101+
102+
var g = Math.floor(Math.random() * 256);
103+
104+
//pick a "blue" from 0 - 255
105+
var b = Math.floor(Math.random() * 256);
106+
return "rgb(" + r + ", " + g + ", " + b + ")";
107+
}

0 commit comments

Comments
 (0)