-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgamification_script.js
More file actions
101 lines (82 loc) · 3.03 KB
/
gamification_script.js
File metadata and controls
101 lines (82 loc) · 3.03 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
let waterLevel = 40;
let fishPosition = 0;
const maxWaterLevel = 90;
const waterIncrement = 25;
const fishRiseSpeed = 25;
// This function now only updates the topic title on page load
window.onload = function () {
const params = new URLSearchParams(window.location.search);
const selectedTopic = params.get("topic") || "future";
const titleElement = document.getElementById("title");
titleElement.textContent = `Let's talk about your ${selectedTopic}!`;
};
let recognition; // Declare recognition globally for reuse
function startVoiceRecognition() {
if (!('webkitSpeechRecognition' in window)) {
alert("Your browser does not support Speech Recognition.");
return;
}
// Initialize Speech Recognition only when the button is clicked
window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
recognition = new window.SpeechRecognition();
recognition.lang = "en-US";
recognition.interimResults = false;
recognition.maxAlternatives = 1;
// Start voice recognition
recognition.start();
// Display voice input in the memo area
recognition.onresult = (event) => {
const voiceInput = event.results[0][0].transcript;
const memoInput = document.getElementById("memo-input");
// Log the recognized text to the console
console.log("Voice Input Recognized:", voiceInput);
memoInput.value = voiceInput;
submitMemo(); // Automatically submit the memo after voice input
};
// Handle errors in voice recognition
recognition.onerror = (event) => {
console.error("Voice recognition error:", event.error);
alert("Voice recognition failed. Please try again.");
};
recognition.onend = () => {
console.log("Voice recognition ended.");
};
}
// Navigate to other pages
function navigateTo(page) {
window.location.href = page;
}
// Submit memo and update water/fish position
function submitMemo() {
const memoInput = document.getElementById("memo-input");
if (memoInput.value.trim() !== "") {
waterLevel += waterIncrement;
fishPosition += fishRiseSpeed;
const water = document.getElementById("water-level");
const fish = document.getElementById("fish");
water.style.height = `${waterLevel}%`;
fish.style.bottom = `${fishPosition}px`;
// Clear memo input
memoInput.value = "";
// Show popup if the fish escapes
if (waterLevel >= maxWaterLevel) {
showPopup();
}
}
}
function showPopup() {
const popup = document.getElementById("popup");
popup.style.display = "flex";
// Close popup and reset game on click
popup.addEventListener("click", () => {
popup.style.display = "none";
resetGame();
});
}
// Reset game for replayability
function resetGame() {
waterLevel = 20;
fishPosition = 20;
document.getElementById("water-level").style.height = `${waterLevel}%`;
document.getElementById("fish").style.bottom = `${fishPosition}px`;
}