-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
42 lines (37 loc) · 1.46 KB
/
game.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
document.addEventListener('DOMContentLoaded', function () {
let currentIndex = 0;
const songs = document.querySelectorAll('.song');
if (songs.length > 0) {
// Marca la primera cançó com a seleccionada
songs[currentIndex].classList.add('selected');
// Funció per actualitzar la selecció de la cançó
function updateSelection() {
songs.forEach((song, index) => {
song.classList.toggle('selected', index === currentIndex);
});
}
// Escoltar els esdeveniments del teclat
document.addEventListener('keydown', function (event) {
if (event.key === 'ArrowDown') {
// Moure cap avall en la llista
currentIndex = (currentIndex + 1) % songs.length;
updateSelection();
} else if (event.key === 'ArrowUp') {
// Moure cap amunt en la llista
currentIndex = (currentIndex - 1 + songs.length) % songs.length;
updateSelection();
} else if (event.key === 'Enter') {
// Seleccionar la cançó amb Enter
const selectedSong = songs[currentIndex];
const downloadLink = selectedSong.querySelector('a[download]');
if (downloadLink) {
// Redirigir o obrir el fitxer del joc
window.location.href = downloadLink.href;
}
} else if (event.key === 'Escape') {
// Redirigir a index.html quan es premi Escape
window.location.href = 'index.html';
}
});
}
});