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

Simon Game #193

Open
wants to merge 2 commits 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
Empty file added Rishab Dakhale/README.md
Empty file.
96 changes: 96 additions & 0 deletions Rishab Dakhale/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
let gameseq = [];
let userseq = [];

let game = false;
let level = 0;


let btns = ['red','yellow','green','purple'];

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

document.addEventListener('keypress',function(){
if(game == false){
console.log('game was started');
game = true;
}

levelup ();
})

function gameflash (btn){
btn.classList.add('flash');
setTimeout(function(){
btn.classList.remove('flash');
},250);
}

function userflash (btn){
btn.classList.add('userflash');
setTimeout(function(){
btn.classList.remove('userflash');
},250);
}

let highScore = 0;

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

let randidx = Math.floor(Math.random() *3);
let randcolor = btns[randidx];
let randbtn = document.querySelector(`.${randcolor}`);

gameflash(randbtn);

gameseq.push(randcolor);
console.log(gameseq);
}

function checkans(idx){
if(userseq[idx] === gameseq[idx]) {
if(userseq.length == gameseq.length){
setTimeout(levelup,1000);
}
}else{
if (level > highScore) {
highScore = level;
}
h2.innerHTML = `Game over! Your score was <b>${level}</b> <br> High score was <b>${highScore}</b> <br> Press any key to start the game.`;
let body = document.querySelector('body').style.backgroundColor = 'red';

setTimeout(function(){
document.querySelector('body').style.backgroundColor = 'white';
},250);



reset ();
}
}

function btnpress (){
let btn = this;
userflash(btn);

usercolor = btn.getAttribute('id');
userseq.push(usercolor);

checkans(userseq.length-1);
}

let allbtns = document.querySelectorAll('.button');
for(btn of allbtns){
btn.addEventListener('click', btnpress);
}

function reset (){
gameseq = [];
userseq = [];
level = 0;
}



27 changes: 27 additions & 0 deletions Rishab Dakhale/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simon Say Game</title>
<link rel="stylesheet" href="style.css">
</head>

<body>
<h1>Simon Game</h1>
<h2>press any key to start Game</h2>
<div class="btn-container">
<div class="line-1">
<div class="button red" type="button" id="red"></div>
<div class="button yellow" type="button" id="yellow"></div>
</div>
<div class="line-2">
<div class="button green" type="button" id="green"></div>
<div class="button purple" type="button" id="purple"></div>
</div>
</div>
<script src="app.js"></script>
</body>

</html>
40 changes: 40 additions & 0 deletions Rishab Dakhale/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
body{
text-align: center;
}

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

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

.red{
background-color: #d95980;
}

.yellow{
background-color: #f99b45;
}

.green{
background-color: #819ff9;
}

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

.flash{
background-color: white;
}

.userflash{
background-color: green;
}