diff --git a/Color game/17-colorgame.css b/Color game/17-colorgame.css new file mode 100644 index 0000000..1d2a3df --- /dev/null +++ b/Color game/17-colorgame.css @@ -0,0 +1,85 @@ +body +{ + background: #232323; + margin: 0; + font-family: "Montserrat", "Avenir"; +} + +.square +{ + width: 30%; + background: purple; + /*width 30% of parent element which is container so 30% of 600 px*/ + float: left; + margin: 1.66%; + padding-bottom: 30%; + border-radius: 15%; + transition: background 0.3s; + -webkit-transition: background 0.3s; + -moz-transition: background 0.3s; +} + +#container +{ + max-width: 600px; + margin: 20px auto; /*center the container 20px top and bottom and auto left and right to center*/ +} + +h1 +{ + color: white; + text-align: center; + line-height: 1.1; + background-color: steelblue; + margin: 0; + font-weight: normal; + text-transform: uppercase; + padding: 20px 0 20px 0; +} + +#stripe +{ + background: white; + height: 30px; + color: black; + text-align: center; +} + +.selected +{ + color: white; + background: steelblue; +} + +#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.3s; + -webkit-transition: all 0.3s; + -moz-transition: all 0.3s; + outline: none; +} + +#message +{ + display: inline-block; + width: 20%; +} + +button:hover +{ + color: white; + background: steelblue; +} \ No newline at end of file diff --git a/Color game/17-colorgame.html b/Color game/17-colorgame.html new file mode 100644 index 0000000..db510ac --- /dev/null +++ b/Color game/17-colorgame.html @@ -0,0 +1,26 @@ + + + + Color game Project + + + +

The Great
RGB
Color Game

+
+ + + + +
+
+
+
+
+
+
+
+
+ + + + \ No newline at end of file diff --git a/Color game/17-colorgame.js b/Color game/17-colorgame.js new file mode 100644 index 0000000..6ada953 --- /dev/null +++ b/Color game/17-colorgame.js @@ -0,0 +1,185 @@ +// The way to win this game is to do clicked color=picked color. + +// rgb(1,112,146) awesome color +// rgb(248,61,91) awesome color +// rgb(3,98,171) awesome color + +var numSquares=6 +var colors=[]; +var pickedColor=pickColor(); + +// select all squares +var squares = document.querySelectorAll(".square"); +var colorDisplay = document.getElementById("colorDisplay"); +var messageDisplay=document.getElementById("message") +var h1=document.querySelector("h1") +var resetButton=document.querySelector("#reset") +var modeButtons=document.querySelectorAll(".mode") + +init(); + + +// Code to run when the page first loads +function init() +{ + setupModeButtons(); + setupSquares(); + reset(); // now we are calling reset for actual when the page loads. it is not inside an event listener + // so it will execute as soon as the page loads. +} + +function setupModeButtons() +{ + //mode buttons-easy hard + for (var i=0; i it is string + // compare grabbed color with picked color + if(clickedColor===pickedColor) + { + h1.style.backgroundColor=pickedColor; + messageDisplay.textContent="Correct!" + changeColors(pickedColor) + resetButton.textContent="Play again?" + } + else + { + // alert("Wrong color"); + this.style.backgroundColor="#232323" + messageDisplay.textContent="Try again" + } + }) + } +} + + +// easyBtn.addEventListener("click", function(){ +// numSquares=3 +// easyBtn.classList.add("selected"); +// hardBtn.classList.remove("selected"); +// colors=generateRandomColors(numSquares); +// pickedColor=pickColor(); +// colorDisplay.textContent=pickedColor + +// for(var i = 0; i < colors.length; i++) // colors.length=3 now +// { +// // added colors to each of the squares +// squares[i].style.backgroundColor=colors[i]; +// } + +// // set remaining bottom three elements display as hidden +// for(var i = 3; i < squares.length; i++) +// { +// squares[i].style.display="none"; +// } +// }) +// hardBtn.addEventListener("click", function(){ +// numSquares=6 +// easyBtn.classList.remove("selected"); +// hardBtn.classList.add("selected"); +// colors=generateRandomColors(numSquares); +// pickedColor=pickColor(); +// colorDisplay.textContent=pickedColor +// for(var i = 0; i < colors.length; i++) // colors.length=6 now +// { +// // added colors to each of the squares +// squares[i].style.backgroundColor=colors[i]; +// squares[i].style.display="block"; +// } +// }) + + +// Reset generates a new game. It has to do three main tasks. Generate random colors and assign to array. +// Select picked color from the array. Change colors of sqaures to colors in the "colors" array +function reset() +{ + // Once reset/new colors is clicked, correct/play again is replaced with an empty string + messageDisplay.textContent="" + // Once new game has started new colors should be shown instead of play again, correct, ect. + resetButton.textContent = "New Colors" + // generate colors and assign + colors=generateRandomColors(numSquares); + // Select picked color from array + pickedColor=pickColor(); + // Change heading display color to match picked color + colorDisplay.textContent=pickedColor + // Add colors of squares from "colors" array + for(var i = 0; i < squares.length; i++) + { + if(colors[i]) // if colors array has 3 then on 4th index this will evaluate to false and will goto else + { + squares[i].style.display="block"; + squares[i].style.backgroundColor=colors[i]; + } + else + squares[i].style.display="none"; + } + h1.style.backgroundColor="steelblue"; + +} + +resetButton.addEventListener("click", function(){ + reset(); +}) + +// Change all colors of all square to the clicked color because clicked color=picked color => you won +function changeColors(color){ + //loop through all squares + for(var i = 0; i < squares.length; i++){ + //change each color to match given color because the color chosen is correct + squares[i].style.backgroundColor = color; + } +} + +// Assign picked color to one of the colors from the "colors" array +function pickColor(){ + var random = Math.floor(Math.random() * colors.length); + return colors[random]; +} + +// Fill the color array with num random colors +function generateRandomColors(num){ + //make an array + var arr = [] + //repeat num times + for(var i = 0; i < num; i++){ + //get random color and push into arr + arr.push(randomColor()) + } + //return that array + return arr; +} + +// generate a random color +function randomColor(){ + //pick a "red" from 0 - 255 + var r = Math.floor(Math.random() * 256); + //pick a "green" from 0 -255 + var g = Math.floor(Math.random() * 256); + //pick a "blue" from 0 -255 + var b = Math.floor(Math.random() * 256); + return "rgb(" + r + ", " + g + ", " + b + ")"; +} \ No newline at end of file