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
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Learn Morse Code the Fun Way - Morse Hero</title>
<meta name="description"
content="Morse Hero is a fun, interactive game to learn Morse code. Practice your skills with audio playback and visual hints to master Morse code quickly.">
<meta name="keywords" content="morse code, morse code game, learn morse code, morse code practice, morse hero">
<meta name="robots" content="index, follow">
<script src="https://cdn.usefathom.com/script.js" data-site="NEBPAKSA" defer></script>
<script src="https://cdn.jsdelivr.net/npm/cw@latest/dist/cw.min.js"></script>
<meta name="canonical" href="https://morsehero.com">
<link rel="apple-touch-icon" sizes="180x180" href="/ico/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/ico/icon-192.png">
<link rel="icon" type="image/png" sizes="16x16" href="/ico/icon-192.png">
<link rel="manifest" href="/site.webmanifest">
<meta name="theme-color" content="#2c3e50">
<!-- Google Fonts -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link
href="https://fonts.googleapis.com/css2?family=Fredoka:wght@400;600&family=Poppins:wght@400;600;700&display=swap"
rel="stylesheet">
<!-- Main Stylesheet -->
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1><a href="/" class="title-link">Morse Hero</a></h1>
<div id="startScreen">
<p>Learn Morse code the fun way!</p>
<button id="startButton" class="start-button">Start</button>
<div class="chart-link">
<a href="/chart/">View Morse Code Chart</a>
</div>
</div>
<div id="gameArea">
<div class="score-container">
<div class="score-widget">
<div class="score-info">
<div class="score-label">Score:</div>
<div class="score-value">
<span id="score">0</span> / <span id="total">0</span>
</div>
</div>
<div class="score-tracker" id="scoreTracker"></div>
</div>
</div>
<div class="game-container">
<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 class="hint-setting">
<input type="checkbox" id="hintMode" name="hintMode">
<label for="hintMode">Show Hints</label>
</div>
</div>
</div>
<footer class="footer">
<p>Need help? View the <a href="/chart/">Morse Code Chart</a>. </p>
<p>Created with <a href="https://www.mastercw.com/cw.js/">CW.js</a>. For a complete and professional
Morse Code training solution visit <a href="https://www.mastercw.com">Master CW</a>.</p>
</footer>
</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';
document.body.classList.add('game-active');
// 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');
const hintModeCheckbox = document.getElementById('hintMode');
optionButtons.forEach((button, index) => {
button.addEventListener('click', function () {
checkAnswer(button);
});
// Remove number labels - we'll add character labels dynamically in newRound
const existingLabel = button.querySelector('.key-label');
if (existingLabel) {
button.removeChild(existingLabel);
}
});
// Add keyboard listener for character keys
document.addEventListener('keydown', (event) => {
// Only process if we're in the game area (not on start screen)
if (gameArea.style.display === 'block') {
const key = event.key.toUpperCase();
// Find if any button has this character
for (let i = 0; i < optionButtons.length; i++) {
const charSpan = optionButtons[i].querySelector('.char-display');
if (charSpan && charSpan.textContent === key && !optionButtons[i].disabled) {
// Simulate button press with visual feedback
optionButtons[i].classList.add('key-pressed');
setTimeout(() => {
optionButtons[i].classList.remove('key-pressed');
checkAnswer(optionButtons[i]);
}, 100);
break;
}
}
}
});
hintModeCheckbox.addEventListener('change', updateButtonsWithHints);
// 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');
// Clear button content
button.innerHTML = '';
});
// 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++) {
// Create a span for the character
const charSpan = document.createElement('span');
charSpan.className = 'char-display';
charSpan.textContent = options[i];
optionButtons[i].appendChild(charSpan);
}
// Update buttons with hints if hint mode is enabled
updateButtonsWithHints();
// immediately play sound
playMorse();
}
// Update buttons with Morse code hints if hint mode is enabled
function updateButtonsWithHints() {
if (hintModeCheckbox.checked) {
optionButtons.forEach(button => {
// Remove any existing hint spans
const existingHint = button.querySelector('.morse-hint');
if (existingHint) {
button.removeChild(existingHint);
}
// Get the character from the button's char-display span
const charSpan = button.querySelector('.char-display');
if (!charSpan) return;
const char = charSpan.textContent;
// Get the Morse code for the character
const morseCode = cw.codes[char];
if (morseCode) {
// Create a span for the hint
const hintSpan = document.createElement('span');
hintSpan.className = 'morse-hint';
hintSpan.textContent = morseCode.replace(/\./g, '·').replace(/\-/g, '−');
// Add the hint to the button
button.appendChild(hintSpan);
}
});
} else {
// Remove hints if hint mode is disabled
optionButtons.forEach(button => {
const existingHint = button.querySelector('.morse-hint');
if (existingHint) {
button.removeChild(existingHint);
}
});
}
}
// 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;
// Get the character from the button's char-display span
const charSpan = selectedButton.querySelector('.char-display');
const selectedChar = charSpan ? charSpan.textContent : '';
// Create a new score dot
const scoreDot = document.createElement('div');
scoreDot.className = 'score-dot';
if (selectedChar === currentChar) {
// Correct answer
score++;
scoreElement.textContent = score;
selectedButton.classList.add('correct');
scoreDot.classList.add('correct');
} else {
// Wrong answer - mark selected button as incorrect
selectedButton.classList.add('incorrect');
scoreDot.classList.add('incorrect');
// Find and highlight the correct button
optionButtons.forEach(button => {
const buttonCharSpan = button.querySelector('.char-display');
if (buttonCharSpan && buttonCharSpan.textContent === currentChar) {
button.classList.add('correct');
}
});
}
// Add the score dot to the tracker
const scoreTracker = document.getElementById('scoreTracker');
scoreTracker.appendChild(scoreDot);
// Limit the number of dots shown (keep last 20)
if (scoreTracker.children.length > 20) {
scoreTracker.removeChild(scoreTracker.children[0]);
}
// 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>
|