Skip to content

added js projects #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions colorGame/colorGame.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
body
{
background-color: #232323;
margin: 0;
font-family: "Montserrat", "Avenir";
}

.square
{
width: 30%;
background: purple;
padding-bottom: 30%;
float: left;
margin: 1.66%;
border-radius: 25px;
transition: background 1.0s;
-webkit-transition: background 1.0s;
-moz-transition: background 1.0s;
}
#container
{
max-width: 600px;
margin: 20px auto;
}
h1
{
line-height: 1.1;
padding: 20px 0;
font-weight: normal;
text-transform: uppercase;
color: white;
text-align: center;
background: steelblue;
margin: 0;
}

#stripe
{
background: white;
height: 30px;
text-align: center;
}

.selected
{
background: steelblue;
color: white;
}

#colorDisplay
{
font-size: 200%;
}
button
{
border: none;
background: none;
text-transform: uppercase;
height: 100%;
font-weight: 700;
color: steelblue;
letter-spacing: 1px;
font-size: inherit;
transition: all 0.30s;
-webkit-transition: all 0.30s;
-moz-transition: all 0.30s;
outline: none;
}
button:hover
{
color: white;
background: steelblue;
}
#message
{
display: inline-block;
width: 20%;
}
32 changes: 32 additions & 0 deletions colorGame/colorGame.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<html>
<head>
<title>Color Game </title>
<link rel="stylesheet" type="text/css" href="colorGame.css">
</head>
<body>
<h1>
The Great
<br>
<span id="colorDisplay">RGB</span>
<br>
Color Guessing Game
</h1>

<div id="stripe">
<button id="reset">New Colors</button>
<span id="message"></span>
<button class="mode">Easy</button>
<button class="mode selected">Hard</button>
</div>
<div id="container">
<div class= "square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
</div>
<script src="colorGame.js" type="text/javascript"></script>
</body>
</html>
133 changes: 133 additions & 0 deletions colorGame/colorGame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
var colors = [];
var pickedColor;
var numSquares = 6;
var squares = document.querySelectorAll(".square");
var colorDisplay = document.getElementById("colorDisplay");
var messageDisplay = document.getElementById("message")
var h1 = document.querySelector("h1");
var resetButton = document.getElementById("reset");
var modeButton = document.querySelectorAll(".mode");

// loading the page

init()
function init()
{
modeButtonListener();
squareListener();
reset();
}


function squareListener()
{
//setting up our squares
for(var i = 0; i < squares.length; i++)
{

squares[i].addEventListener("click",function()
{
//grab color of picked square.
var clickedColor = this.style.background;

if (clickedColor === pickedColor)
{
h1.style.background = clickedColor;
//change all squares to match the clicked color
changeColor(clickedColor);
messageDisplay.textContent = "Correct";
//reset button
resetButton.textContent = "Play Again?"
}
else
{
this.style.background = "#232323";
messageDisplay.textContent = "Try Again";
}

});
}

}

function modeButtonListener()
{

//loop tru the buttons mode
for (var i = 0; i<modeButton.length; i++)
{
modeButton[i].addEventListener("click", function()
{
modeButton[0].classList.remove("selected");
modeButton[1].classList.remove("selected");
this.classList.add("selected");
//figure how many squares to display
this.textContent === "Easy" ? numSquares = 3: numSquares = 6;
reset()

//pick new color
});
}
}
function reset()
{
colors = generateRandomColors(numSquares);
pickedColor = pickColor();
colorDisplay.textContent = pickedColor;
h1.style.background = "steelblue"
messageDisplay.textContent = "";
resetButton.textContent="new colors";

for (var i = 0; i < squares.length; i++)
{
if(colors[i])
{
squares[i].style.display = "block";
squares[i].style.background = colors[i];
}
else{squares[i].style.display = "none";}
}
}


resetButton.addEventListener("click", function(){reset();});


function changeColor(color)
{
for(var i = 0; i < squares.length; i++)
{
// loop tru squares and change each color to match a given color
squares[i].style.background = color;

}
}
function pickColor()
{
//form a random number and return the index of the color array
var random = Math.floor(Math.random() * colors.length);
//position of the sqaure color
return colors[random ];
}

function generateRandomColors(num)
{
var arr = []
for(var i =0; i < num; i++)
{
//get random color and push into arra
arr.push(randomColor());
}

return arr;
}
function randomColor()
{
// pick a red from o to 255 etc...
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 + ")";
}

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Loading