Skip to content

Commit c1bb179

Browse files
authored
Add color memory game
1 parent 56c3d1a commit c1bb179

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Simon says agame</title>
7+
<link rel="stylesheet" href="style.css">
8+
</head>
9+
<body>
10+
<h1>color_memory_game</h1>
11+
<h2>Press any key to start</h2>
12+
<div class="btn-container">
13+
<div class="line-one">
14+
<div class="btn red" type="button" id="red">1</div>
15+
<div class="btn green" type="button" id="green">2</div>
16+
</div>
17+
<div class="line-two">
18+
<div class="btn yellow" type="button" id="yellow">3</div>
19+
<div class="btn purple" type="button" id="purple">4</div>
20+
</div>
21+
22+
</div>
23+
<script src="script.js"></script>
24+
</body>
25+
</html>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Color Memory Game
2+
3+
A fun and interactive memory game built with HTML, CSS, and JavaScript where players need to remember and select the color sequence
4+
5+
## Features
6+
7+
- Dynamic color sequences
8+
- Progressive difficulty
9+
- Responsive design
10+
11+
## How to Play
12+
13+
1. Press Start to begin
14+
2. Watch the color sequence
15+
3. Click the colors in the same order
16+
4. Advance to longer sequences as you succeed
17+
18+
## Technologies Used
19+
20+
- HTML5
21+
- CSS3
22+
- JavaScript
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
let gameSeq=[];
2+
let userSeq=[];
3+
let started=false;
4+
let level=0;
5+
let btns=["yellow",'red','purple','green'];
6+
let h2=document.querySelector('h2');
7+
document.addEventListener("keypress",function(){
8+
if(started==false){
9+
console.log("game started");
10+
started=true;
11+
12+
levelUp();
13+
}
14+
15+
})
16+
function btnflash(btn){
17+
btn.classList.add("flash");
18+
setTimeout(function(){
19+
btn.classList.remove("flash");
20+
},700)
21+
}
22+
23+
function levelUp(){
24+
userSeq=[];
25+
level++;
26+
h2.innerText=`level${level}`;
27+
let randcolor=btns[Math.floor(Math.random()*4)];
28+
let btn=document.querySelector(`.${randcolor}`);
29+
gameSeq.push(randcolor);
30+
console.log(gameSeq);
31+
btnflash(btn);
32+
}
33+
34+
35+
function btnpress(){
36+
btnflash(this);
37+
userSeq.push(this.getAttribute("id"));
38+
console.log(userSeq);
39+
checkAns(userSeq.length-1);
40+
}
41+
function checkAns(idx){
42+
43+
if(userSeq[idx]===gameSeq[idx]){
44+
if(gameSeq.length==userSeq.length){
45+
setTimeout(()=>{levelUp()},1000);
46+
}
47+
48+
}
49+
else{
50+
h2.innerHTML=`Game over! Score was <b>${level}</b><br>Press any key to restart`;
51+
document.querySelector("body").style.backgroundColor="red";
52+
setTimeout(()=>{
53+
document.querySelector("body").style.backgroundColor="white";},200);
54+
reset();
55+
}
56+
}
57+
58+
let allBtns=document.querySelectorAll(".btn");
59+
for(butns of allBtns){
60+
butns.addEventListener("click",btnpress);
61+
}
62+
63+
function reset(){
64+
gameSeq=[];
65+
userSeq=[];
66+
started=false;
67+
level=0;
68+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
.btn-container{
12+
display: flex;
13+
justify-content: center;
14+
flex-wrap: wrap;
15+
/* gap: 20px; */
16+
}
17+
.red{
18+
background-color: #d95980;
19+
}
20+
.green{
21+
background-color: #63aac0;
22+
}
23+
.yellow{
24+
background-color: #f99b45;
25+
}
26+
.purple{
27+
background-color: rgb(226, 127, 226);
28+
}
29+
.flash{
30+
background-color: white;
31+
}

0 commit comments

Comments
 (0)