-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforca.js
168 lines (158 loc) · 4.65 KB
/
forca.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
function nextWord(x, y, scale) {
actualWord++
rightChars=[]
let index = actualWord - 1 < 0 ? 0 : actualWord - 1
word = palavras[index]
size = word.length
cleanText(x, y, word, scale)
if (actualWord >= palavras.length) {
// limpar texto e colocar mensagem de sucesso
points = 0
draw = false
ctx.clearRect(0, 0, canvas.width, canvas.height);
alert("Você venceu!")
lista.style.display="flex"
canvas.style.display="none"
lista.innerHTML=""
exibitionSwitch();
iniciar.disabled=true
gameOver = true
success = true
}
else {
word = palavras[actualWord]
size = word.length
console.log(word)
console.log(size)
for (let i = 0; i < size; i++) {
drawUnderline(x, y, scale, i,strokeColor)
}
}
}
function exibitionSwitch() {
gameOver=!gameOver
let display = gameOver ? "flex" : "none"
adicionarPalavra.style.display = display
input.style.display = display
iniciar.style.display = display
points = []
}
function startChecker(x, y, scale) {
let checker = setInterval(() => {
let item = errors.length
if (gameOver) {
palavras = []
// função game over
if(!success){
iniciar.disabled=true
ctx.clearRect(0, 0, canvas.width, canvas.height);
alert("Game over!")
}
gameOver=false
canvas.style.display="none"
lista.style.display="flex"
lista.innerHTML=""
exibitionSwitch();
clearInterval(checker);
}
else {
if (currentRightChars >= size) {
nextWord(x, y, scale)
}
}
if (draw) {
switch (item) {
case 6:
drawArmLeg(x, y + (30 * scale), "l", scale,strokeColor)
gameOver = true;
case 5:
drawArmLeg(x, y + (30 * scale), "r", scale,strokeColor)
case 4:
drawArmLeg(x, y, "l", scale,strokeColor)
case 3:
drawArmLeg(x, y, "r", scale,strokeColor)
case 2:
drawBody(x, y, scale,strokeColor)
case 1:
drawHead(x, y, scale,strokeColor)
case 0:
drawCadafalso(x, y, scale,strokeColor);
break;
}
draw = false;
}
}, 100)
}
function startGame() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
canvas.style.display="flex"
// limpar mensagem
// zerar variáveis
errors = [];
lista.style.display="none"
draw = true;
actualWord = -1
erroLetterIndex = 0
rightChars = []
currentRightChars = 0
word = ""
size = 0
fontStyle = "serif"
messagePosition = 5
success=false
exibitionSwitch()
nextWord(mainx, mainy, scale)
startChecker(mainx, mainy, scale);
}
function getIndexes(array, element) {
let indexes = [];
var idx = array.indexOf(element);
while (idx != -1) {
indexes.push(idx);
idx = array.indexOf(element, idx + 1);
}
return indexes
}
document.addEventListener("keypress", async event => {
// colocar uppercase
let letter = event.key.toUpperCase()
if (!gameOver) {
let current = rightChars.indexOf(letter)
let erro = errors.indexOf(letter)
if(current<0&& erro<0){
let indexes = getIndexes(palavras[actualWord], letter)
for (index of indexes) {
if (index > -1 ) {
drawText(letter, mainx, mainy, scale, fontSize, index, strokeColor)
rightChars.push(letter)
currentRightChars++
}
}
if (indexes.length == 0) {
errors.push(letter)
let index = errors.length - 1
let y = mainy + (45 * scale)
drawText(letter, mainx, y, scale, fontSize, index)
draw = true
}
}
}
})
adicionarPalavra.addEventListener("click", () => {
// remover acentos
// desabilitar botão iniciar jogo se não tiver palavras cadastradas
let texto = input.value.toUpperCase()
input.value = ""
if(!(texto=="")){
iniciar.disabled = false
var li = document.createElement("li");
li.textContent=texto
lista.appendChild(li);
palavras.push(texto)
}
})
iniciar.addEventListener("click", () => {
// Só ficar disponível se tiver uma palavra.
startGame()
}
)