Skip to content
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

Update high scrore in this game #162

Open
wants to merge 1 commit into
base: main
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
31 changes: 31 additions & 0 deletions Simon Game/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!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">
</head>

<body>
<h1>Simon says Game</h1>
<h3></h3>
<h2>Press any Key to Start</h2>

<div class="btn-container">
<div class="line-one">
<div class="btn red" type="button" id="red"></div>
<div class="btn yellow" type="button" id="yellow"></div>
</div>
<div class="line-two">
<div class="btn green" type="button" id="green"></div>
<div class="btn purple" type="button" id="purple"></div>
</div>


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

</html>
101 changes: 101 additions & 0 deletions Simon Game/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
let gameSeq = [];
let userSeq = [];
let btns = ["red", "yellow", "green", "purple"];

let started = false;
let level = 0;

let h2 = document.querySelector("h2");

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

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

function userFlash(btn) {
btn.classList.add("userFlash")
setTimeout(() => {
btn.classList.remove("userFlash")
}, 250);
}

function levelUp() {
userSeq = [];
level++;
h2.innerText = `Level : ${level}`;
let randomIndex = Math.floor(Math.random() * 3) + 1;
let randomColor = btns[randomIndex];
let randBtn = document.querySelector(`.${randomColor}`)
gameSeq.push(randomColor);
gameFlash(randBtn);
}

function checkAns(idx) {
if (userSeq[idx] === gameSeq[idx]) {
if (userSeq.length == gameSeq.length) {
setTimeout(levelUp, 1500);
}
} else {
h2.innerHTML = `Game Over !, <b>Your Score was ${level}</b> <br> Press Any Key To Start`;
document.querySelector("body").style.backgroundColor = "red";
setTimeout(function () {
document.querySelector("body").style.backgroundColor = "white";
}, 250);

highScore();
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 (allbtns of allBtns) {
allbtns.addEventListener("click", btnPress);
}


function reset() {
started = false;
gameSeq = [];
userSeq = [];
level = 0;
}

let HighScore = true;
let highScoreValue = 0;


function highScore() {
if(HighScore == true){
let h3 = document.querySelector("h3");
h3.innerText = `Your HighScore = ${highScoreValue}`;
}
if (level > highScoreValue) {
HighScore = true;
highScoreValue = level;
let h3 = document.querySelector("h3");
h3.innerText = `New High Score = ${highScoreValue}`;
} else {
HighScore = false;
}
};

39 changes: 39 additions & 0 deletions Simon Game/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
body{
text-align: center;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

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

.btn{
height: 200px;
width: 200px;
border: 4px solid black;
border-radius: 20px;
margin: 1.5rem;
cursor: pointer;
}

.red{
background-color: red;
}
.yellow{
background-color: yellow;
}
.green{
background-color: green;
}
.purple{
background-color: purple;
}

.flash{
background-color: white;
}

.userFlash{
background-color: black;
}