-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameLogic.js
108 lines (89 loc) · 3.32 KB
/
gameLogic.js
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
let score = 0;
let wordList = [];
let currentWord = '';
let wordTimer;
let playerName = '';
const topScores = JSON.parse(localStorage.getItem('topScores')) || [];
document.addEventListener('DOMContentLoaded', () => {
fetch('words.txt')
.then(response => response.text())
.then(data => {
wordList = data.split('\n');
displayTopScores();
});
});
function startGame() {
playerName = document.getElementById('playerName').value.trim();
if (!playerName) {
alert('Introduce Tu Nombre Para Comenzar.');
return;
}
document.getElementById('startScreen').style.display = 'none';
document.getElementById('gameScreen').style.display = 'block';
document.getElementById('userInput').value = '';
changeWord();
setInterval(updateTime, 1000);
document.getElementById('userInput').addEventListener('input', checkInput);
}
function changeWord() {
if (wordTimer) clearTimeout(wordTimer);
currentWord = wordList[Math.floor(Math.random() * wordList.length)].trim();
document.getElementById('wordToType').textContent = currentWord;
wordTimer = setTimeout(() => {
saveScore();
resetScore();
changeWord();
}, 3000);
}
function checkInput() {
const input = document.getElementById('userInput').value.trim();
if (input === currentWord) {
score++;
document.getElementById('scoreDisplay').textContent = score;
document.getElementById('userInput').value = '';
changeWord();
// Verificar si el puntaje alcanza un múltiplo de 10 y reproducir sonido de diez palabras
if (score % 10 === 0) {
playTenWordsSound();
}
}
}
function resetScore() {
score = 0;
document.getElementById('scoreDisplay').textContent = score;
document.getElementById('userInput').value = '';
}
function saveScore() {
const existingPlayerIndex = topScores.findIndex(player => player.name === playerName);
if (existingPlayerIndex !== -1) {
// El jugador ya está en la lista; actualizamos su puntuación
topScores[existingPlayerIndex].score = Math.max(topScores[existingPlayerIndex].score, score);
} else {
// El jugador no está en la lista; lo agregamos como una nueva entrada
topScores.push({ name: playerName, score: score });
topScores.sort((a, b) => b.score - a.score);
if (topScores.length > 5) topScores.pop(); // Limitar a 5 jugadores
}
localStorage.setItem('topScores', JSON.stringify(topScores));
displayTopScores();
// Verificar si el puntaje es mayor que 0 y alcanza un múltiplo de 10, y reproducir sonido de diez palabras
if (score > 0 && score % 10 === 0) {
playTenWordsSound();
}
}
function displayTopScores() {
const scoreList = document.getElementById('scoreList');
scoreList.innerHTML = topScores.map(score => `<li>${score.name}: ${score.score}</li>`).join('');
}
function updateTime() {
const now = new Date();
const hours = now.getHours().toString().padStart(2, '0');
const minutes = now.getMinutes().toString().padStart(2, '0');
document.getElementById('timeDisplay').textContent = `${hours}:${minutes}`;
}
// SONIDO AL +10
score++;
// Si el puntaje alcanza un múltiplo de 10, reproducir sonido de diez palabras
if (score % 10 === 0) {
playTenWordsSound();
}