Skip to content

Instantly share code, notes, and snippets.

@behouba
Created August 2, 2017 22:54
Show Gist options
  • Save behouba/ec5425a202a394e850e8f1bfdf5acf1e to your computer and use it in GitHub Desktop.
Save behouba/ec5425a202a394e850e8f1bfdf5acf1e to your computer and use it in GitHub Desktop.
Simon game training projet in HTML CSS JAVASCRIPT
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptLibraryMappings">
<file url="PROJECT" libraries="{jquery}" />
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/SimonGame.iml" filepath="$PROJECT_DIR$/.idea/SimonGame.iml" />
</modules>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
<excludeFolder url="file://$MODULE_DIR$/temp" />
<excludeFolder url="file://$MODULE_DIR$/tmp" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="jquery" level="application" />
</component>
</module>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectTasksOptions" suppressed-tasks="Babel" />
</project>
body{
background: #d98c8c;
}
.container{
position: relative;
background: black;
width: 450px;
height: 450px;
border-radius: 50%;
margin: auto;
margin-top: 50px;
}
.box{
float: left;
width: 200px;
height: 200px;
margin: 10px;
position: relative;
left: 5px;
top: 5px;
}
.box:hover{
cursor: pointer;
}
.clicked{
background-color: white;
}
#red{
border-radius: 0% 100% 0% 0%;
}
.red{
background-color: red;
}
#green{
border-radius: 100% 0% 0% 0%;
}
.green{
background-color: green;
}
.yellow{
background-color: yellow;
}
#yellow{
border-radius: 0% 0% 0% 100%;
}
#blue{
border-radius: 0% 0% 100% 0%;
}
.blue{
background-color: blue;
}
#panel{
width: 195px;
height: 195px;
background: grey;
position: absolute;
top: 25%;
left: 25%;
border-radius: 50%;
border: 15px black solid;
}
#screen, #start, #strict{
float: left;
width: 55px;
height: 60px;
margin: 3px;
border: 4px;
position: relative;
left: 10px;
top: 70px;
font-size: 13px;
}
#count p{
margin-top: 10px;
text-align: center;
}
#count{
width: 85%;
height: 60%;
border-radius: 20%;
background: #d1d1e0;
border: 4px solid black;
}
#start-button{
background: #ee86ff;
width: 20px;
height: 20px;
border-radius: 50%;
position: relative;
top: 10%;
left: 20%;
border: 5px black solid;
}
#start-button:hover{
cursor: pointer;
}
#start-button:active {
transform: scale3d(0.9,0.9,0.9)
}
#start p{
text-align: center;
margin-top: 14px;
}
#strict-button{
background: #ecf2f5;
width: 20px;
height: 20px;
border-radius: 50%;
position: relative;
top: 10%;
left: 20%;
border: 5px black solid;
}
#strict-button:hover{
cursor: pointer;
}
#strict-button:active {
transform: scale3d(0.9,0.9,0.9)
}
#strict p{
text-align: center;
margin-top: 14px;
}
#power{
font-size: 13px;
}
#off, #power-status, #on{
float: left;
position: relative;
top: 76px;
left: 23%;
margin: 2px;
}
#power-status{
background: black;
width: 50px;
height: 20px;
position: relative;
border-radius: 4px;
}
#btn:hover{
cursor: pointer;
}
#btn{
background: #00cc00;
width: 25px;
height: 16px;
border: 2px black solid;
position: relative;
}
.btn-on{
left: 25px;
}
#off p, #on p{
margin-top: 3px;
}
.title{
text-align: center;
}
a{
text-decoration: none;
}
#me{
margin-top: 10%;
text-align: center;
}
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>SIMON GAME FREECODECAMP</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div>
<h1 class="title"><a href="https://en.wikipedia.org/wiki/Simon_(game)" target="blanc">SIMON-GAME</a></h1>
<div class="container">
<div class="game-circle">
<div class="box green" id="green" ></div>
<div class="box red" id="red"></div>
<div class="box yellow" id="yellow"></div>
<div class="box blue" id="blue"></div>
<div style="display: none">
<audio id="s1">
<source src="https://s3.amazonaws.com/freecodecamp/simonSound1.mp3" type="audio/mp3"/>
</audio>
<audio id="s2">
<source src="https://s3.amazonaws.com/freecodecamp/simonSound2.mp3" type="audio/mp3"/>
</audio>
<audio id="s3">
<source src="https://s3.amazonaws.com/freecodecamp/simonSound3.mp3" type="audio/mp3"/>
</audio>
<audio id="s4">
<source src="https://s3.amazonaws.com/freecodecamp/simonSound3.mp3" type="audio/mp3"/>
</audio>
<audio id="s5">
<source src="audio/Wrong-answer-sound-effect.mp3" type="audio/mp3"/>
</audio>
</div>
<div id="panel">
<div id="set">
<div id="screen">
<div id="count">
<p></p>
</div>
<p style="margin-top:0px;text-align:center">COUNT</p>
</div>
<div id="start">
<div id="start-button"></div>
<p>START</p>
</div>
<div id="strict">
<div id="strict-button"></div>
<p>STRICT</p>
</div>
</div>
<div id="power">
<div id="off">
<p>OFF</p>
</div>
<div id="power-status">
<div id="btn"></div>
</div>
<div id="on">
<p>ON</p>
</div>
</div>
</div>
</div>
</div>
<p id="me">Coded and Designed by: <a> K.B Manasse</a></p>
</div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
<script src="js/index.js"></script>
</body>
</html>
"use strict";
$(document).ready(function(){
// variable that will be used.
let count;
let count2;
let delay;
const colorArray = ["green", "blue", "red", "yellow"];
let gameSequence = [];
let onOff = false;
let strictMode = false;
// this function make action for each active or clicked button.
let gameColor = {
green: function () {
document.getElementById("s1").play();
$("#green").css("background-color","white");
setTimeout(function(){
$("#green").css("background-color","green");
}, 1000);
},
blue: function () {
document.getElementById("s2").play();
$("#blue").css("background-color","white");
setTimeout(function(){
$("#blue").css("background-color","blue");
}, 1000);
},
red: function () {
document.getElementById("s3").play();
$("#red").css("background-color","white");
setTimeout(function(){
$("#red").css("background-color","red");
}, 1000);
},
yellow: function () {
document.getElementById("s4").play();
$("#yellow").css("background-color","white");
setTimeout(function(){
$("#yellow").css("background-color","yellow");
}, 1000);
}
}
// this function start the game whith new random color.
function start(){
count = 0;
count2 = 0;
gameSequence = [];
let firstColor = colorArray[Math.round(Math.random()*3)];
gameSequence.push(firstColor);
gameColor[firstColor].call(gameColor);
}
//this function show the color that must to be clicked in order.
function gameRun() {
let x = 0;
let i = setInterval(function () {
gameColor[gameSequence[x]].call(gameColor);
console.log(x);
x++;
if (x === gameSequence.length){
clearInterval(i);
}
}, 1500);
}
// this function check that user click the right button.
function checkGame(color) {
if (color === gameSequence[count2] && gameSequence[count2+1] !== undefined){
gameColor[color].call(gameColor);
count2++;
}
else if (color === gameSequence[count2] && gameSequence[count2+1] === undefined){
count++;
count2=0;
if (count === 20){
$("#count p").text(count);
setTimeout(function () {
$("#count p").text("WIN!");
}, 2000);
count = 0;
count2 = 0;
gameSequence = [];
} else {
gameColor[color].call(gameColor);
if(count < 10){
$("#count p").text("0"+count);
} else {
$("#count p").text(count);
}
let firstColor = colorArray[Math.round(Math.random()*3)];
gameSequence.push(firstColor);
setTimeout(function () {
gameRun();
}, 1000);
}
}
else {
if(strictMode === true){
count2=0;
count=0;
errorGame();
setTimeout(function () {
start();
}, 3000);
} else {
errorGame();
gameRun();
count2=0;
}
}
}
// this function alert error.
function errorGame(){
document.getElementById("s5").play();
$("#count p").text("!!");
setTimeout(function () {
$("#count p").text("");
}, 500);
setTimeout(function () {
$("#count p").text("!!");
}, 500);
setTimeout(function () {
$("#count p").text("");
}, 500);
setTimeout(function () {
if(count < 10){
$("#count p").text("0"+count);
} else {
$("#count p").text(count);
}
}, 500);
}
// button to turn on or off the game.
$("#btn").click(function(){
count = 0;
$("#btn").toggleClass("btn-on");
gameSequence = [];
if(onOff === false){
$("#count p").text("--");
onOff = true;
} else if ( onOff === true){
$("#count p").text("");
onOff = false;
}
})
// this button start the game when he is clicked.
$("#start-button").click(function () {
count = 0;
if (onOff === true){
start();
$("#count p").text("00");
}
})
// to enable the strict mode.
$("#strict-button").click(function () {
if (onOff === true){
if (strictMode === false){
$("#strict-button").css("background-color", "#f50847");
strictMode = true;
} else {
$("#strict-button").css("background-color", "#e3e4f5");
strictMode = false;
}
}
})
$("#green").click(function () {
checkGame("green");
})
$("#blue").click(function () {
checkGame("blue");
})
$("#yellow").click(function () {
checkGame("yellow");
})
$("#red").click(function () {
checkGame("red");
})
})
<!--
Copyright (c) 2017 by KOUAME BEHOUBA MANASSE (http://codepen.io/python92/pen/gxOEda)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
A Pen created at CodePen.io. You can find this one at http://codepen.io/python92/pen/gxOEda.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment