-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
253 lines (222 loc) · 7.36 KB
/
script.js
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
var palabras =['ALURA', 'AHORCADO', 'ORACLE', 'HTML', 'CSS', 'JAVASCRIPT']
var tablero = document.getElementById('horca').getContext('2d');
var letras = [];
var palabraCorrecta = "";
var palabraSecreta = "";
var errores = 11;
var aciertos = 0;
function escojerPalabraSecreta(){
var palabra = palabras[Math.floor(Math.random()*palabras.length)]
palabraSecreta = palabra
console.log(palabra)
return palabraSecreta
}
function dibujarLineas(){
tablero.LineWidth = 6
tablero.LineCap = "round"
tablero.LineJoin = "round"
tablero.strokeStyle = "#0A3871"
tablero.beginPath()
var ancho=600/palabraSecreta.length
for(let i = 0; i < palabraSecreta.length ; i++){
tablero.moveTo(500+(ancho*i),640)
tablero.lineTo(550+(ancho*i),640)
}
tablero.stroke()
tablero.closePath()
}
dibujarLineas(escojerPalabraSecreta())
function escribirLetraCorrecta(index){
tablero.font = 'bold 52px Arial';
tablero.LineWidth=6
tablero.LineCap="round"
tablero.LineJoin="round"
tablero.fillStyle= "#0A3871"
var ancho=600/palabraSecreta.length
tablero.fillText(palabraSecreta[index],505+(ancho*index),620)
}
function escribirLetraIncorrecta(letra, errorsLeft){
tablero.font = 'bold 40px Arial';
tablero.LineWidth=6
tablero.LineCap="round"
tablero.LineJoin="round"
tablero.fillStyle= "#0A3871"
tablero.fillText(letra, 535+(40*(10-errorsLeft)), 710,40)
}
function verificarLetraClickeada(key){
if (letras.length<1 || letras.indexOf(key)<0)
{
letras.push(key)
return false
}
else{
letras.push(key)
return true
}
}
//function countChars(palabraSecreta){
// var maxLength = letras;
// var strLength = palabraSecreta;
// if(strLength === maxLength){
// alert("Ganaste");
// }
//}
//contarletraClickeadas()
//function contarletraClickeadas(){
// if (letras.length === palabraSecreta.length)
// alert("Palabra Agregada");
// console.log(letras);
//}
function adicionarLetraCorrecta(i) {
palabraCorrecta += palabraSecreta[i].toUpperCase()
}
function adicionarLetraIncorrecta(letra) {
if(palabraSecreta.indexOf(letra)<=0){
errores-=1
}
//aca agrego la funcion del ahorcado() es para que verifique que cuando resta los errores dibuje una linea
Ahorcado()
// countChars()
}
document.onkeydown = (e) => {
let letra=e.key.toUpperCase()
if(!verificarLetraClickeada(e.key)){
if(palabraSecreta.includes(letra)){
console.log(letra)
adicionarLetraCorrecta(palabraSecreta.indexOf(letra))
for (let i=0;i<palabraSecreta.length;i++){
if(palabraSecreta[i]===letra){
escribirLetraCorrecta(i)
}
}
}
else{
if (!verificarLetraClickeada(e.key)) return
adicionarLetraIncorrecta(letra)
escribirLetraIncorrecta(letra,errores)
}
}
}
//funcion de mostrar el fondo que tapa el juego lo use para probar que funcionara
//function Mostrar(){
// document.getElementById("fondo-menu").style.display = "block";
//}
//funcion de oculta el fondo que tapa el juego, lo use para probar que funcionara
// function Ocultar(){
// document.getElementById("fondo-menu").style.display = "none";
//}
//Aca es donde se hace la transicion de activar o desactivar el tablero, funciona moviendo el plano para adelante o para atras
function Comenzar(){
document.getElementById("fondo-menu").style.zIndex = -1;
document.getElementById("boton-start").style.display = "none";
document.getElementById("boton-agregar").style.zIndex = -1;
document.getElementById("h1ahorcado").style.zIndex = -1;
document.getElementById("nuevaPalabra").style.zIndex = -1;
//escucharLetras();
}
//Función para reiniciar el juego/pagina en este caso
function Restart(){
location.reload()
}
// esta es la funcion del dibujo de la horca y el personaje (la idea era hacer imagenes en photoshop e ir habilitandolas a medida que vaya
// marcando los errores, pero preferi hacer que el canvas dibujara al muñeco y la horca)
function Ahorcado() {
console.log(errores)
if(errores === 10){ // Base del dibujo
tablero.beginPath()
tablero.moveTo(90, 640);
tablero.lineTo(260, 640);
tablero.stroke();
tablero.closePath();
}
if(errores === 9){ // Mastil
tablero.beginPath()
tablero.moveTo(115, 640);
tablero.lineTo(115, 460);
tablero.stroke();
tablero.closePath();
}
if(errores === 8){ // Linea transversal de arriba
tablero.beginPath()
tablero.moveTo(113, 460);
tablero.lineTo(190, 460);
tablero.stroke();
tablero.closePath();
}
if(errores === 7){ //Soga
tablero.beginPath()
tablero.moveTo(190, 460);
tablero.lineTo(190, 485);
tablero.stroke();
tablero.closePath();
}
if(errores === 6){ //Cabeza (lo tome de un compañero porque no se me ocurria como)
tablero.beginPath()
tablero.beginPath();
tablero.arc(190, 500, 18, 0, 2 * Math.PI, false);
tablero.stroke();
tablero.closePath();
}
if(errores === 5){ // tronco
tablero.beginPath()
tablero.moveTo(190, 520);
tablero.lineTo(190, 580);
tablero.stroke();
tablero.closePath();
}
if(errores === 4){ //brazo derecho
tablero.beginPath()
tablero.moveTo(190, 525);
tablero.lineTo(222, 540);
tablero.stroke();
tablero.closePath();
}
if(errores === 3){ //Brazo izquierdo
tablero.beginPath()
tablero.moveTo(190, 525);
tablero.lineTo(158, 540);
tablero.stroke();
tablero.closePath();
}
if(errores === 2){ //Pierna Derecha
tablero.beginPath()
tablero.moveTo(190, 580);
tablero.lineTo(165, 600);
tablero.stroke();
tablero.closePath();
}
if(errores === 1){ //Pierna Izquierda
tablero.beginPath()
tablero.moveTo(190, 580)
tablero.lineTo(222, 600);
tablero.stroke();
tablero.closePath();
// alert("Ahorcado!");
}
if(errores === 1){ // Cartel de perder el juego
document.getElementById("h1perdiste").style.zIndex = 2;
document.getElementById("fondo-menu").style.zIndex = 2;
document.getElementById("boton_reiniciar").style.zIndex = 2;
}
}
function verificarpalabras(){ //sentencia no terminada
if (palabraSecreta === palabraCorrecta){
cartelganador()
}
}
function cartelganador(){ //sentencia no terminada
document.getElementById("h1ganaste").style.zIndex = 2;
}
function agregarpalabras(){ //funcion para agregar palabras y verifique si ya existen
var nuevapalabra = null;
nuevapalabra = document.getElementById("nuevaPalabra").value.toUpperCase();
if (palabras.includes(nuevapalabra)||nuevapalabra === "") {
alert("Palabra Repetida y Casillero Vacio");
document.getElementById("nuevaPalabra").value = "";
} else {
palabras.push(nuevapalabra);
alert("Palabra Agregada");
document.getElementById("nuevaPalabra").value = "";
console.log(palabras);
}
}