Skip to content

Commit a946b54

Browse files
authored
Merge pull request #80 from Deepasha1406/main
Added Simon Game
2 parents fbfe7c7 + cd9584d commit a946b54

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

Shooting-game/Simon game/app.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
let gamesq=[];
2+
let usersq=[];
3+
let btns = ["pink","orange","skyBlue","blue"];
4+
5+
let started = false;
6+
let level = 0;
7+
8+
let h2 = document.querySelector("h2");
9+
10+
document.addEventListener("keypress",function(){
11+
if(started==false){
12+
console.log("game started");
13+
started = true;
14+
levelUp();
15+
}
16+
});
17+
18+
function gameFlash(btn){
19+
btn.classList.add("flash");
20+
setTimeout(function(){
21+
btn.classList.remove("flash")
22+
},250);
23+
}
24+
25+
function userFlash(btn){
26+
btn.classList.add("userflash");
27+
setTimeout(function(){
28+
btn.classList.remove("userflash")
29+
},250);
30+
}
31+
32+
function levelUp(){
33+
usersq=[];
34+
level++;
35+
h2.innerText = `level ${level}`;
36+
37+
let ranidx = Math.floor(Math.random() * 3);
38+
let rancol = btns[ranidx];
39+
let renbtn = document.querySelector(`.${rancol}`);
40+
gamesq.push(rancol);
41+
console.log(gamesq)
42+
gameFlash(renbtn);
43+
}
44+
45+
function checkans(idx){
46+
if(usersq[idx]==gamesq[idx]){
47+
if(usersq.length==gamesq.length){
48+
setTimeout(levelUp,1000);
49+
}
50+
}else{
51+
h2.innerHTML = `Game Over! Your score was <b>${level}</b> <Br> `;
52+
document.querySelector("body").style.backgroundColor = "red";
53+
setTimeout(function(){
54+
document.querySelector("body").style.backgroundColor = "white";
55+
},150)
56+
57+
}
58+
}
59+
60+
61+
62+
function btnPress(){
63+
let btn = this;
64+
userFlash(btn);
65+
66+
usercol = btn.getAttribute("id");
67+
usersq.push(usercol);
68+
69+
checkans(usersq.length-1);
70+
}
71+
72+
let allbtns = document.querySelectorAll(".btn")
73+
for(btn of allbtns){
74+
btn.addEventListener("click",btnPress);
75+
}
76+
77+
function Reset(){
78+
started = false;
79+
gamesq = [];
80+
usersq = [];
81+
level = 0;
82+
}

Shooting-game/Simon game/index.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<link rel="stylesheet" href="style.css">
8+
<title>Document</title>
9+
</head>
10+
11+
<body>
12+
<h1>Simon Game</h1>
13+
<h2>Press any key to start the game</h2>
14+
<div class="box">
15+
<div class="line-one">
16+
<div class="btn pink" type="button" id="pink">p</div>
17+
<div class="btn skyBlue" type="button" id="skyblue">s</div>
18+
</div>
19+
<div class="line-two">
20+
<div class="btn orange" type="button" id="orange">o</div>
21+
<div class="btn blue" type="button" id="blue">b</div>
22+
</div>
23+
</div>
24+
25+
<script src="app.js"></script>
26+
</body>
27+
28+
</html>

Shooting-game/Simon game/style.css

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
body{
2+
text-align: center;
3+
}
4+
.btn{
5+
height: 200px;
6+
width: 200px;
7+
border-radius: 20%;
8+
border: 10px solid black;
9+
margin: 2rem;
10+
}
11+
12+
.box{
13+
display: flex;
14+
justify-content: center;
15+
}
16+
17+
.pink{
18+
background-color: palevioletred;
19+
}
20+
21+
.skyBlue{
22+
background-color: aquamarine;
23+
}
24+
25+
.orange{
26+
background-color: orange;
27+
}
28+
29+
.blue{
30+
background-color: rgb(79, 79, 201);
31+
}
32+
33+
.flash{
34+
background-color: white;
35+
}
36+
37+
.userflash{
38+
background-color: green;
39+
}

0 commit comments

Comments
 (0)