-
Notifications
You must be signed in to change notification settings - Fork 0
/
clase45 - obteniendo el input del usuario.html
176 lines (151 loc) · 4.04 KB
/
clase45 - obteniendo el input del usuario.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
<!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;
}
.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="empezarJuego()">Empezar a jugar!</button>
</div>
<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 Juego {
constructor() {
this.inicializar()
this.generarSecuencia()
this.siguienteNivel()
}
inicializar() {
this.elegirColor = this.elegirColor.bind(this)
btnEmpezar.classList.add('hide')
this.nivel = 1
this.colores = {
celeste,
violeta,
naranja,
verde
}
}
generarSecuencia() {
this.secuencia = new Array(10).fill(0).map(n => Math.floor(Math.random() * 4))
}
siguienteNivel() {
this.iluminarSecuencia()
this.agregarEventosClick()
}
transformarNumeroAColor(numero) {
switch (numero) {
case 0:
return 'celeste'
case 1:
return 'violeta'
case 2:
return 'naranja'
case 3:
return 'verde'
}
}
iluminarSecuencia() {
for (let i = 0; i < this.nivel; i++) {
const color = this.transformarNumeroAColor(this.secuencia[i])
setTimeout(() => this.iluminarColor(color), 1000 * i)
}
}
iluminarColor(color) {
this.colores[color].classList.add('light')
setTimeout(() => this.apagarColor(color), 350)
}
apagarColor(color) {
this.colores[color].classList.remove('light')
}
agregarEventosClick() {
this.colores.celeste.addEventListener('click', this.elegirColor)
this.colores.verde.addEventListener('click', this.elegirColor)
this.colores.violeta.addEventListener('click', this.elegirColor)
this.colores.naranja.addEventListener('click', this.elegirColor)
}
elegirColor(ev) {
console.log(this)
}
}
function empezarJuego() {
window.juego = new Juego()
}
</script>
</body>
</html>