Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SudiptaPaul-31 committed Dec 9, 2023
0 parents commit 9ca90a3
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 0 deletions.
Binary file added console.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simon Says Game</title>
<link rel="stylesheet" href="style.css">
<link rel="shortcut icon" href="console.png" type="image/x-icon">

</head>
<body>
<h1>Simon Game</h1>
<h2>Press any key to start the game</h2>

<div class="btn-container">
<div class="line-one">
<div class="btn red" type="button" id="red">1</div>
<div class="btn yellow" type="button" id="yellow">2</div>
</div>

<div class="line-two">
<div class="btn green" type="button" id="green">3</div>
<div class="btn purple" type="button" id="purple">4</div>
</div>

</div>
<script src="script.js"></script>

</body>
</html>
84 changes: 84 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
let gameSeq = [];
let userSeq = [];

let btns = ["yellow", "red", "green", "purple"];
let started = false;
let level = 0;
let h2 = document.querySelector("h2");

document.addEventListener("keypress",function(){
if(started == false) {
console.log("game is started");
started = true;
levelUp();
}
});

function gameFlash(btn) {
btn.classList.add("flash");
setTimeout(function() {
btn.classList.remove("flash");
},350);
}

function userflash(btn) {
btn.classList.add("userflash");
setTimeout(function() {
btn.classList.remove("userflash");
},350);
}

function levelUp(){
userSeq = [];
level++;
h2.innerText = `Level ${level}`;

let randIdx = Math.floor(Math.random() * 3);
let randColor = btns[randIdx];
let randBtn = document.querySelector(`.${randColor}`);
gameSeq.push(randColor);
console.log(gameSeq);
gameFlash(randBtn);


}

function checkAns(idx) {

if(userSeq[idx] === gameSeq[idx]){
if(userSeq.length == gameSeq.length) {
setTimeout(levelUp, 500);
}
} else{
h2.innerHTML = `Game Over! Your score was <b>${level}</b> <br>
Press any key to start.`;
document.querySelector("body").style.backgroundColor ="red";
setTimeout(function(){
document.querySelector("body").style.backgroundColor ="white";
},150);
reset();
}
}

function btnPress() {

let btn = this;
userflash(btn);

userColor = btn.getAttribute("id");
userSeq.push(userColor);

checkAns(userSeq.length-1);
}

let allBtns = document.querySelectorAll(".btn");
for(btn of allBtns) {
btn.addEventListener("click",btnPress);
}

function reset() {
started = false;
gameSeq = [];
userSeq = [];
level = 0;
}
40 changes: 40 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
body{
text-align: center;
}

.btn{
height: 200px;
width: 200px;
border-radius: 20%;
border: 10px solid black;
margin: 2rem;
}

.btn-container{
display: flex;
justify-content: center;
}

.yellow{
background-color: #f99b45;
}

.red{
background-color: #d95980;
}

.green{
background-color: #63aac0;
}

.purple{
background-color: #7e29d9;
}

.flash{
background-color: white;
}

.userflash {
background-color: #4e5663;
}

0 comments on commit 9ca90a3

Please sign in to comment.