-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcabodeguerra.html
More file actions
266 lines (227 loc) · 10.5 KB
/
cabodeguerra.html
File metadata and controls
266 lines (227 loc) · 10.5 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
<!DOCTYPE html>
<html lang="pt-pt">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cabo de Guerra: Matemática</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
body {
background: #f0f2f5;
touch-action: manipulation;
user-select: none;
}
canvas {
max-width: 100%;
height: auto;
border-radius: 8px;
box-shadow: 0 10px 25px rgba(0,0,0,0.1);
background: #fff;
}
.key-hint {
font-size: 0.75rem;
color: #666;
}
</style>
</head>
<body class="flex flex-col items-center justify-center min-h-screen p-4">
<div class="mb-4 text-center">
<h1 class="text-3xl font-bold text-gray-800">Cabo de Guerra Matemático</h1>
<p class="text-gray-600">Resolve a conta para puxar a corda!</p>
</div>
<canvas id="gameCanvas" width="800" height="500" class="cursor-default"></canvas>
<div class="mt-6 grid grid-cols-2 gap-8 w-full max-w-3xl">
<!-- Equipa Azul -->
<div class="flex flex-col gap-2">
<span class="text-blue-600 font-bold text-center">Equipa Azul (Teclas 1-4)</span>
<div class="grid grid-cols-2 gap-2" id="blueButtons">
<button onclick="handleInput(0, 'left')" class="bg-blue-500 hover:bg-blue-600 text-white font-bold py-4 rounded-lg shadow-md transition-all active:scale-95 text-xl">--</button>
<button onclick="handleInput(1, 'left')" class="bg-blue-500 hover:bg-blue-600 text-white font-bold py-4 rounded-lg shadow-md transition-all active:scale-95 text-xl">--</button>
<button onclick="handleInput(2, 'left')" class="bg-blue-500 hover:bg-blue-600 text-white font-bold py-4 rounded-lg shadow-md transition-all active:scale-95 text-xl">--</button>
<button onclick="handleInput(3, 'left')" class="bg-blue-500 hover:bg-blue-600 text-white font-bold py-4 rounded-lg shadow-md transition-all active:scale-95 text-xl">--</button>
</div>
</div>
<!-- Equipa Laranja -->
<div class="flex flex-col gap-2">
<span class="text-orange-600 font-bold text-center">Equipa Laranja (Teclas 5-8)</span>
<div class="grid grid-cols-2 gap-2" id="orangeButtons">
<button onclick="handleInput(0, 'right')" class="bg-orange-500 hover:bg-orange-600 text-white font-bold py-4 rounded-lg shadow-md transition-all active:scale-95 text-xl">--</button>
<button onclick="handleInput(1, 'right')" class="bg-orange-500 hover:bg-orange-600 text-white font-bold py-4 rounded-lg shadow-md transition-all active:scale-95 text-xl">--</button>
<button onclick="handleInput(2, 'right')" class="bg-orange-500 hover:bg-orange-600 text-white font-bold py-4 rounded-lg shadow-md transition-all active:scale-95 text-xl">--</button>
<button onclick="handleInput(3, 'right')" class="bg-orange-500 hover:bg-orange-600 text-white font-bold py-4 rounded-lg shadow-md transition-all active:scale-95 text-xl">--</button>
</div>
</div>
</div>
<!-- Modal de Vitória -->
<div id="winnerModal" class="fixed inset-0 bg-black/50 flex items-center justify-center hidden">
<div class="bg-white p-8 rounded-2xl text-center shadow-2xl">
<h2 id="winnerTitle" class="text-4xl font-bold mb-4 text-green-600">Vitória!</h2>
<p id="winnerMsg" class="text-xl text-gray-700 mb-6">A equipa venceu o cabo de guerra.</p>
<button onclick="resetGame()" class="bg-gray-800 hover:bg-gray-900 text-white px-8 py-3 rounded-full font-bold transition-all">Jogar Novamente</button>
</div>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const modal = document.getElementById('winnerModal');
// Configurações
let ropePosition = 0; // -10 a +10
const maxPull = 10;
let questionData = { text: '', options: [], correct: 0 };
let gameOver = false;
let answered = false;
function generateQuestion() {
const a = Math.floor(Math.random() * 10) + 1;
const b = Math.floor(Math.random() * 10) + 1;
const correct = a + b;
let opts = [correct, correct - 1, correct + 1, correct + 2];
// Embaralhar opções
opts = opts.sort(() => Math.random() - 0.5);
questionData = {
text: `${a} + ${b} = ?`,
options: opts,
correct: correct
};
updateUI();
draw();
}
function updateUI() {
const blueBtns = document.querySelectorAll('#blueButtons button');
const orangeBtns = document.querySelectorAll('#orangeButtons button');
questionData.options.forEach((opt, i) => {
blueBtns[i].textContent = opt;
orangeBtns[i].textContent = opt;
});
}
function draw() {
// Limpar
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Chão
ctx.strokeStyle = '#999';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(0, 450);
ctx.lineTo(800, 450);
ctx.stroke();
// Centro dinâmico da corda
const pullVisualScale = 25;
const centerX = 400 + (ropePosition * pullVisualScale);
// Desenhar Linhas de Vitória
ctx.setLineDash([5, 5]);
ctx.strokeStyle = '#ff000033';
ctx.beginPath();
ctx.moveTo(400 - (maxPull * pullVisualScale), 350);
ctx.lineTo(400 - (maxPull * pullVisualScale), 450);
ctx.moveTo(400 + (maxPull * pullVisualScale), 350);
ctx.lineTo(400 + (maxPull * pullVisualScale), 450);
ctx.stroke();
ctx.setLineDash([]);
// Corda
ctx.strokeStyle = '#8B4513';
ctx.lineWidth = 12;
ctx.lineCap = 'round';
ctx.beginPath();
ctx.moveTo(centerX - 300, 410);
ctx.lineTo(centerX + 300, 410);
ctx.stroke();
// Marcador Central da Corda
ctx.fillStyle = '#ff0000';
ctx.fillRect(centerX - 5, 400, 10, 20);
// Personagens Equipa Azul (Esquerda)
drawPerson(centerX - 220, 410, 'blue');
drawPerson(centerX - 280, 410, 'blue');
// Personagens Equipa Laranja (Direita)
drawPerson(centerX + 220, 410, 'orange');
drawPerson(centerX + 280, 410, 'orange');
// Texto da Pergunta
ctx.fillStyle = '#1a202c';
ctx.font = 'bold 48px Inter, sans-serif';
ctx.textAlign = 'center';
ctx.fillText(questionData.text, 400, 80);
// Instrução de Teclado
ctx.font = '14px Inter, sans-serif';
ctx.fillStyle = '#718096';
ctx.fillText("Usa o teclado ou clica nos botões abaixo", 400, 120);
// Barra de Progresso/Posição
ctx.fillStyle = '#edf2f7';
ctx.fillRect(200, 470, 400, 10);
ctx.fillStyle = ropePosition < 0 ? '#3b82f6' : '#f97316';
ctx.fillRect(400, 470, (ropePosition/maxPull) * 200, 10);
}
function drawPerson(x, y, team) {
ctx.fillStyle = team === 'blue' ? '#3b82f6' : '#f97316';
// Corpo
ctx.beginPath();
ctx.arc(x, y - 60, 15, 0, Math.PI * 2); // Cabeça
ctx.fill();
ctx.lineWidth = 8;
ctx.strokeStyle = team === 'blue' ? '#2563eb' : '#ea580c';
ctx.beginPath();
ctx.moveTo(x, y - 45);
ctx.lineTo(x, y - 10); // Tronco
ctx.moveTo(x, y - 35);
ctx.lineTo(x - (team === 'blue' ? -15 : 15), y - 20); // Braços segurando corda
ctx.stroke();
}
function handleInput(index, team) {
if (gameOver || answered) return;
const selected = questionData.options[index];
if (selected === questionData.correct) {
answered = true;
if (team === 'left') {
ropePosition--;
} else {
ropePosition++;
}
checkWin();
// Feedback visual de acerto antes de mudar
draw();
setTimeout(() => {
if (!gameOver) {
generateQuestion();
answered = false;
}
}, 400);
} else {
// Penalidade por erro opcional (ex: tremer o ecrã)
canvas.classList.add('animate-bounce');
setTimeout(() => canvas.classList.remove('animate-bounce'), 300);
}
}
function checkWin() {
if (ropePosition <= -maxPull) {
endGame("Equipa Azul");
} else if (ropePosition >= maxPull) {
endGame("Equipa Laranja");
}
}
function endGame(winnerName) {
gameOver = true;
modal.classList.remove('hidden');
document.getElementById('winnerTitle').textContent = `Vitória da ${winnerName}!`;
document.getElementById('winnerTitle').className = `text-4xl font-bold mb-4 ${winnerName.includes('Azul') ? 'text-blue-600' : 'text-orange-600'}`;
document.getElementById('winnerMsg').textContent = `A corda foi puxada totalmente para o lado ${winnerName.toLowerCase()}.`;
}
function resetGame() {
ropePosition = 0;
gameOver = false;
answered = false;
modal.classList.add('hidden');
generateQuestion();
}
// Listener de Teclado
window.addEventListener('keydown', (e) => {
const key = e.key;
if (["1", "2", "3", "4"].includes(key)) {
handleInput(parseInt(key) - 1, 'left');
} else if (["5", "6", "7", "8"].includes(key)) {
handleInput(parseInt(key) - 5, 'right');
} else if (key === " " && gameOver) {
resetGame();
}
});
// Início
generateQuestion();
</script>
</body>
</html>