-
Notifications
You must be signed in to change notification settings - Fork 0
/
word game.html
199 lines (174 loc) · 5.28 KB
/
word game.html
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
<!DOCTYPE html>
<html>
<head>
<title>Word Pronunciation Game</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f8f8f8;
margin: 0;
padding: 20px;
text-align: center;
}
h1 {
font-size: 36px;
color: #FF4081;
margin-bottom: 30px;
}
#word {
font-size: 48px;
color: #4CAF50;
margin-bottom: 20px;
}
button {
font-size: 24px;
background-color: #FF4081;
color: #fff;
border: none;
padding: 10px 20px;
margin: 10px;
cursor: pointer;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #E91E63;
}
.correct {
font-size: 36px;
color: #4CAF50;
animation: flash 1s infinite;
}
@keyframes flash {
0% {
opacity: 1;
}
50% {
opacity: 0;
}
100% {
opacity: 1;
}
}
.microphone-indicator {
width: 10px;
height: 10px;
border-radius: 50%;
background-color: #ccc;
display: inline-block;
margin-left: 5px;
animation: pulse 1s infinite;
}
@keyframes pulse {
0% {
transform: scale(1);
background-color: #FF4081;
}
50% {
transform: scale(1.2);
}
100% {
transform: scale(1);
background-color: #FF4081;
}
}
</style>
</head>
<body>
<h1>Word Pronunciation Game</h1>
<div id="word"></div>
<button onclick="toggleListening()">Toggle Listening</button>
<span id="microphone-indicator" class="microphone-indicator"></span>
<script>
// Array of My Little Pony character names
var words = ['Twilight Sparkle', 'Rainbow Dash', 'Pinkie Pie', 'Rarity', 'Applejack', 'Fluttershy', 'Spike'];
// Display a random word from the array
var wordIndex = Math.floor(Math.random() * words.length);
var randomWord = words[wordIndex];
document.getElementById('word').textContent = randomWord;
// Initialize speech recognition
var recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;
recognition.onstart = function() {
document.getElementById('microphone-indicator').style.animation = 'pulse 1s infinite';
};
recognition.onend = function() {
document.getElementById('microphone-indicator').style.animation = '';
};
recognition.onaudiostart = function() {
document.getElementById('microphone-indicator').style.backgroundColor = '#FF4081';
};
recognition.onaudioend = function() {
document.getElementById('microphone-indicator').style.backgroundColor = '#ccc';
};
recognition.onresult = function(event) {
var result = event.results[event.results.length - 1][0].transcript.toLowerCase();
console.log('Recognized:', result);
checkWord(result);
};
// Check if the user input matches the random word
function checkWord(userInput) {
var wordElement = document.getElementById('word');
var correctElement = wordElement.querySelector('h2.correct');
if (correctElement) {
return;
}
if (userInput === randomWord.toLowerCase()) {
displayMessage("Congratulations!", true);
speakText("Congratulations! You have read " + randomWord + " correctly!", function() {
moveNext();
});
}
}
// Display message and flash "Correct!" if correct
function displayMessage(message, isCorrect) {
var wordElement = document.getElementById('word');
if (isCorrect) {
var correctElement = document.createElement('h2');
correctElement.textContent = message;
correctElement.classList.add('correct');
wordElement.appendChild(correctElement);
}
}
// Speak text using speech synthesis
function speakText(text, callback) {
var speechSynthesis = window.speechSynthesis;
var speechUtterance = new SpeechSynthesisUtterance(text);
speechUtterance.voice = getVoice('Microsoft Maisie Online (Natural) - English (United Kingdom)');
speechSynthesis.speak(speechUtterance);
speechUtterance.onend = callback;
}
// Get the voice by name
function getVoice(name) {
var voices = window.speechSynthesis.getVoices();
for (var i = 0; i < voices.length; i++) {
if (voices[i].name === name) {
return voices[i];
}
}
return null;
}
// Move to the next word
function moveNext() {
// Remove the previous word and message
var wordElement = document.getElementById('word');
wordElement.textContent = '';
// Get the next random word
wordIndex = (wordIndex + 1) % words.length;
randomWord = words[wordIndex];
wordElement.textContent = randomWord;
recognition.start();
}
// Toggle speech recognition
function toggleListening() {
if (recognition.isStarted) {
recognition.stop();
recognition.isStarted = false;
} else {
recognition.start();
recognition.isStarted = true;
}
}
</script>
</body>
</html>