-
Notifications
You must be signed in to change notification settings - Fork 0
/
simonTalk.html
227 lines (194 loc) · 5.51 KB
/
simonTalk.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Simon Dice</title>
<style>
body {
margin: 0;
background: #dedede;
display: flex;
align-items: center;
height: 100vh;
font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
}
.gameboard {
height: 100vh;
width: 100vh;
border-radius: 50%;
overflow: hidden;
margin: 0 auto;
max-height: 60vh;
max-width: 60vh;
}
.color {
width: 50%;
height: 50%;
display: inline-block;
}
.left {
float: left;
}
.right {
float: left;
}
.celeste {
background: #22a6b3;
}
.celeste.light {
background: #7ed6df;
}
.violeta {
background: #be2edd;
}
.violeta.light {
background: #e056fd;
}
.naranja {
background: #f0932b;
}
.naranja.light {
background: #ffbe76;
}
.verde {
background: #6ab04c;
}
.verde.light {
background: #badc58;
}
.btn-start {
width: 400px;
height: 100px;
background: #ecf0f1;
color: #2c3e50;
font-size: 2.5rem;
position: absolute;
top: calc(50% - 50px);
left: calc(50% - 200px);
}
.hide {
display: none;
}
</style>
</head>
<body>
<div class="gameboard">
<div id="celeste" class="color celeste left" data-color="celeste"></div>
<div id="violeta" class="color violeta right" data-color="violeta"></div>
<div id="naranja" class="color naranja left" data-color="naranja"></div>
<div id="verde" class="color verde right" data-color="verde"></div>
<button id="btnEmpezar" class="btn-start" onclick="play()">Play game!</button>
</div>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script>
const celeste = document.getElementById('celeste')
const violeta = document.getElementById('violeta')
const naranja = document.getElementById('naranja')
const verde = document.getElementById('verde')
const btnEmpezar = document.getElementById('btnEmpezar')
class Game {
constructor() {
this.initialize()
}
initialize() {
this.selectColor = this.selectColor.bind(this)
toggleButton()
this.level = 1
this.lastLevel = 10
this.colorSequence = []
this.colors = [
celeste,
violeta,
naranja,
verde,
]
this.generateSequence()
setTimeout(()=> this.chargeLevel(), 500)
}
chargeLevel() {
this.secuenceToVerify = [...this.colorSequence]
this.higthLigthSequence()
this.addColorClick()
}
generateSequence() {
this.colorSequence = new Array(this.lastLevel)
.fill(this.colors[0])
.map(color => this.colors[Math.floor(Math.random()*4)])
}
higthLigthSequence() {
for (let index = 0; index < this.level; index++) {
const color = this.colorSequence[index]
setTimeout(()=> this.higthLigthColor(color), 1000 * index)
}
}
higthLigthColor(color) {
color.classList.add('light')
setTimeout(()=> this.dimColor(color), 350)
}
dimColor(color) {
color.classList.remove('light')
}
nextLevel () {
this.level++
this.deleteColorClick()
this.chargeLevel()
}
addColorClick() {
this.colors.forEach((color)=>{
color.addEventListener('click', this.selectColor)
})
}
deleteColorClick() {
this.colors.forEach((color)=>{
color.removeEventListener('click', this.selectColor)
})
}
selectColor(event) {
const color = event.target
this.higthLigthColor(color)
setTimeout(()=> this.verifySecuence(color), 500)
}
async winLevel() {
if (this.level === this.lastLevel) {
this.winGame()}
else {
await swal('Simon talk', `You win level ${this.level}. Now more dificult`, 'success')
setTimeout(()=> this.nextLevel(), 1000)
}
}
async loseGame() {
await swal('Simon talk', 'You lose, try in a new game', 'error')
this.resetGame()
}
async winGame() {
await swal('Simon talk', '!!You win the game', 'success')
this.resetGame()
}
resetGame() {
this.deleteColorClick()
toggleButton()
}
verifySecuence(color) {
if(this.secuenceToVerify.length !== this.colorSequence.length-this.level) {
if(color === this.secuenceToVerify[0]) {
this.secuenceToVerify.shift()
}else{
this.loseGame()
}
}
this.secuenceToVerify.length === this.colorSequence.length-this.level && this.winLevel()
}
}
function play() {
var game = new Game()
}
function toggleButton() {
if (btnEmpezar.classList.contains('hide')) {
btnEmpezar.classList.remove('hide')
} else {
btnEmpezar.classList.add('hide')
}
}
</script>
</body>
</html>