-
Notifications
You must be signed in to change notification settings - Fork 0
/
simon.html
102 lines (86 loc) · 2.13 KB
/
simon.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<!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>
<style>
body {
text-align: center;
}
.btn {
height: 150px;
width: 150px;
border-radius: 20%;
border: 8px solid black;
margin: 1rem;
}
.btn-container {
display: flex;
justify-content: center;
}
.red {
background-color: rgb(239, 105, 105);
}
.yellow {
background-color: rgb(253, 122, 7);
}
.green {
background-color: rgb(46, 143, 175);
}
.purple {
background-color: rgb(219, 100, 219);
}
.flash {
background-color: white;
}
</style>
</head>
<body>
<h1>Simon Says 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">1</div>
<div class="btn yellow" type="button">2</div>
</div>
<div class="line-two">
<div class="btn green" type="button">3</div>
<div class="btn purple" type="button">4</div>
</div>
</div>
<script>
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 btnFlash(btn) {
btn.classList.add("flash");
setTimeout(function () {
btn.classList.remove("flash");
}, 1000); // Reduced the duration to make the flash more noticeable
}
function levelup() {
level++;
h2.innerText = `Level ${level}`;
// Random button choose
let randIdx = Math.floor(Math.random() * 3); // Use 4 since there are 4 buttons
let randColor = btns[randIdx];
let randbtn = document.querySelector(`.${randColor}`);
console.log(randIdx);
console.log(randColor);
console.log(randbtn);
btnFlash(randbtn);
}
</script>
</body>
</html>