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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Morse Hero</title>
<script src="https://cwjs.mastercw.com/cw.min.js"></script>
<style>
body {
font-family: 'Fredoka One', 'Baloo', 'Arial Rounded MT Bold', sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
text-align: center;
background-color: #1e2a3a;
color: #fff;
font-size: 20px;
background-image: linear-gradient(45deg, #1e2a3a 0%, #2c3e50 100%);
}
h1 {
color: #f39c12;
text-shadow:
0 0 10px rgba(243, 156, 18, 0.7),
3px 3px 0px #e74c3c;
font-size: 4em;
margin-bottom: 30px;
letter-spacing: 2px;
}
.game-container {
background-color: #34495e;
border-radius: 25px;
padding: 35px;
margin-bottom: 30px;
box-shadow: 0 15px 30px rgba(0, 0, 0, 0.3),
0 0 20px rgba(243, 156, 18, 0.3);
border: 5px solid #3498db;
}
.options {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 25px;
margin-top: 35px;
}
button {
cursor: pointer;
font-family: 'Fredoka One', 'Baloo', 'Arial Rounded MT Bold', sans-serif;
transition: all 0.3s ease;
font-weight: bold;
letter-spacing: 1px;
text-shadow: 2px 2px 0px rgba(0, 0, 0, 0.3);
}
.start-button {
background-color: #e74c3c;
color: white;
border: none;
border-radius: 50px;
padding: 25px 50px;
font-size: 32px;
margin: 30px 0;
box-shadow: 0 8px 0 #c0392b,
0 0 20px rgba(231, 76, 60, 0.4);
text-transform: uppercase;
}
.start-button:hover {
background-color: #f45c4c;
transform: translateY(-5px);
box-shadow: 0 13px 0 #c0392b,
0 0 30px rgba(231, 76, 60, 0.6);
}
.start-button:active {
transform: translateY(3px);
box-shadow: 0 5px 0 #c0392b;
}
.option-button {
background-color: #3498db;
color: white;
border: none;
border-radius: 20px;
font-size: 36px;
padding: 25px 15px;
box-shadow: 0 8px 0 #2980b9,
0 0 15px rgba(52, 152, 219, 0.5);
min-height: 120px;
display: flex;
align-items: center;
justify-content: center;
}
.option-button:hover {
background-color: #4aa3df;
transform: translateY(-5px);
box-shadow: 0 13px 0 #2980b9,
0 0 25px rgba(52, 152, 219, 0.7);
}
.option-button:active {
transform: translateY(3px);
box-shadow: 0 5px 0 #2980b9;
}
.option-button.correct {
background-color: #2ecc71;
box-shadow: 0 8px 0 #27ae60,
0 0 30px rgba(46, 204, 113, 0.8);
animation: pulse-green 0.5s infinite alternate;
}
.option-button.incorrect {
background-color: #e74c3c;
box-shadow: 0 8px 0 #c0392b,
0 0 30px rgba(231, 76, 60, 0.8);
animation: shake 0.5s;
}
@keyframes pulse-green {
from {
box-shadow: 0 8px 0 #27ae60, 0 0 20px rgba(46, 204, 113, 0.8);
}
to {
box-shadow: 0 8px 0 #27ae60, 0 0 40px rgba(46, 204, 113, 1);
}
}
@keyframes shake {
0%,
100% {
transform: translateX(0);
}
20%,
60% {
transform: translateX(-10px);
}
40%,
80% {
transform: translateX(10px);
}
}
.feedback {
height: 40px;
margin-top: 30px;
font-weight: bold;
font-size: 28px;
color: #f39c12;
text-shadow: 0 0 10px rgba(243, 156, 18, 0.5);
}
.score {
font-size: 32px;
font-weight: bold;
color: #f39c12;
margin-bottom: 30px;
text-shadow: 2px 2px 0px rgba(0, 0, 0, 0.3),
0 0 10px rgba(243, 156, 18, 0.3);
}
#gameArea {
display: none;
}
#startScreen {
margin: 50px 0;
background-color: #34495e;
padding: 40px;
border-radius: 25px;
box-shadow: 0 15px 30px rgba(0, 0, 0, 0.3),
0 0 20px rgba(231, 76, 60, 0.3);
border: 5px dashed #e74c3c;
}
#startScreen p {
font-size: 26px;
margin-bottom: 40px;
color: #fff;
line-height: 1.5;
text-shadow: 0 0 10px rgba(255, 255, 255, 0.2);
}
select {
padding: 15px 25px;
font-size: 24px;
border-radius: 15px;
border: 3px solid #3498db;
background-color: #2c3e50;
color: #fff;
font-family: 'Fredoka One', 'Baloo', 'Arial Rounded MT Bold', sans-serif;
cursor: pointer;
box-shadow: 0 0 15px rgba(52, 152, 219, 0.3);
}
.settings {
background-color: #34495e;
padding: 25px;
border-radius: 20px;
margin-top: 30px;
font-size: 24px;
border: 3px solid #3498db;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2),
0 0 15px rgba(52, 152, 219, 0.3);
}
.settings label {
font-size: 24px;
margin-right: 15px;
color: #3498db;
font-weight: bold;
text-shadow: 0 0 10px rgba(52, 152, 219, 0.3);
}
</style>
</head>
<body>
<h1>Morse Hero</h1>
<div id="startScreen">
<p>Learn Morse code the fun way! Listen to the sound and pick the correct character.</p>
<button id="startButton" class="start-button">Start Game</button>
</div>
<div id="gameArea">
<div class="game-container">
<div class="score">Score: <span id="score">0</span> / <span id="total">0</span></div>
<div class="options">
<button class="option-button" id="option1"></button>
<button class="option-button" id="option2"></button>
<button class="option-button" id="option3"></button>
<button class="option-button" id="option4"></button>
</div>
<div class="feedback" id="feedback"></div>
</div>
<div class="settings">
<label for="wpmSelect">Speed:</label>
<select id="wpmSelect">
<option value="10">10 WPM</option>
<option value="15">15 WPM</option>
<option value="20" selected>20 WPM</option>
<option value="25">25 WPM</option>
<option value="30">30 WPM</option>
</select>
</div>
</div>
<script>
// Game variables
let actx = null;
// Define available characters
const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const numbers = '0123456789';
const specialChars = '.,?/=';
const allChars = letters + numbers + specialChars;
// Game state
let currentChar = '';
let score = 0;
let totalPlayed = 0;
// DOM elements
const startButton = document.getElementById('startButton');
const startScreen = document.getElementById('startScreen');
const gameArea = document.getElementById('gameArea');
// Start button event listener
startButton.addEventListener('click', function () {
// Initialize audio context only when the start button is clicked
try {
actx = cw.initAudioContext({ tone: 600 });
// Hide start screen and show game area
startScreen.style.display = 'none';
gameArea.style.display = 'block';
// Initialize the game
init();
} catch (error) {
console.error('Failed to initialize audio context:', error);
alert('Failed to initialize audio. Please try again.');
}
});
// Initialize the game
function init() {
// DOM elements for the game
const optionButtons = [
document.getElementById('option1'),
document.getElementById('option2'),
document.getElementById('option3'),
document.getElementById('option4')
];
const feedbackElement = document.getElementById('feedback');
const scoreElement = document.getElementById('score');
const totalElement = document.getElementById('total');
const wpmSelect = document.getElementById('wpmSelect');
optionButtons.forEach(button => {
button.addEventListener('click', function () {
checkAnswer(button);
});
});
// Start the game
newRound();
// Generate a new round
function newRound() {
// Clear feedback
feedbackElement.textContent = '';
// Reset all buttons to default state
optionButtons.forEach(button => {
button.disabled = false;
button.classList.remove('correct', 'incorrect');
});
// Generate a random character
const randomIndex = Math.floor(Math.random() * allChars.length);
currentChar = allChars[randomIndex];
// Generate 3 wrong options
let options = [currentChar];
while (options.length < 4) {
const randomIndex = Math.floor(Math.random() * allChars.length);
const randomChar = allChars[randomIndex];
if (!options.includes(randomChar)) {
options.push(randomChar);
}
}
// Shuffle options
options = shuffleArray(options);
// Set button text
for (let i = 0; i < 4; i++) {
optionButtons[i].textContent = options[i];
}
// immediately play sound
playMorse();
}
// Play the morse code
function playMorse() {
const wpm = parseInt(wpmSelect.value);
// Play the character with the existing audio context
cw.play(currentChar, {
actx: actx,
wpm: wpm
});
}
// Check the user's answer
function checkAnswer(selectedButton) {
// Disable all buttons
optionButtons.forEach(button => {
button.disabled = true;
});
totalPlayed++;
totalElement.textContent = totalPlayed;
const selectedChar = selectedButton.textContent;
if (selectedChar === currentChar) {
// Correct answer
score++;
scoreElement.textContent = score;
selectedButton.classList.add('correct');
} else {
// Wrong answer - mark selected button as incorrect
selectedButton.classList.add('incorrect');
// Find and highlight the correct button
optionButtons.forEach(button => {
if (button.textContent === currentChar) {
button.classList.add('correct');
}
});
}
// Start a new round after a delay
setTimeout(newRound, 1000);
}
}
// Utility function to shuffle an array
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
</script>
</body>
</html>
|