finalizzazione del progetto
This commit is contained in:
+38
-36
@@ -14,8 +14,7 @@ const listaRifiuti = [
|
||||
{ img: 'assets/media/img/fase2/rifiuti/pannolino.png', tipo: 'indifferenziata' }
|
||||
];
|
||||
|
||||
let punteggio = 0;
|
||||
let tempo = 60;
|
||||
let rifiutiTotali = 0;
|
||||
const counter = document.getElementById('counter');
|
||||
const scoreDisplay = document.getElementById('score');
|
||||
|
||||
@@ -143,6 +142,7 @@ function controllaCollisione(rifiuto) {
|
||||
// PORTIAMO IL BIDONE IN PRIMO PIANO
|
||||
bidone.style.zIndex = "2000";
|
||||
|
||||
rifiutiTotali++;
|
||||
if (rifiuto.dataset.tipo === bidone.dataset.type) {
|
||||
// CORRETTO
|
||||
punteggio += 1;
|
||||
@@ -164,7 +164,9 @@ function controllaCollisione(rifiuto) {
|
||||
bidone.style.zIndex = "1"; // Ritorna al suo posto dopo l'effetto
|
||||
}, 400);
|
||||
}
|
||||
|
||||
if (rifiutiTotali >= localStorage.getItem('punteggioFase1')) {
|
||||
mostraFineGioco();
|
||||
}
|
||||
document.getElementById('score').innerText = punteggio;
|
||||
rifiuto.remove();
|
||||
setTimeout(generaRifiuto, 500);
|
||||
@@ -181,23 +183,27 @@ let timerIntervallo;
|
||||
let tempoRimanente;
|
||||
|
||||
function avviaTimer(secondiIniziali) {
|
||||
const displayTimer = document.getElementById('timer');
|
||||
const minuti = document.getElementById('minuti');
|
||||
const secondi = document.getElementById('secondi');
|
||||
|
||||
// Inizializziamo la variabile globale con il valore ricevuto
|
||||
tempoRimanente = secondiIniziali;
|
||||
displayTimer.innerText = tempoRimanente;
|
||||
minuti.innerText = String(Math.floor(tempoRimanente / 60)).padStart(2, '0');
|
||||
secondi.innerText = String(tempoRimanente % 60).padStart(2, '0');
|
||||
|
||||
// Puliamo timer precedenti
|
||||
clearInterval(timerIntervallo);
|
||||
|
||||
timerIntervallo = setInterval(() => {
|
||||
tempoRimanente--; // Sottraiamo un secondo alla variabile globale
|
||||
displayTimer.innerText = tempoRimanente; // Aggiorniamo lo schermo
|
||||
minuti.innerText = String(Math.floor(tempoRimanente / 60)).padStart(2, '0');
|
||||
secondi.innerText = String(tempoRimanente % 60).padStart(2, '0');
|
||||
|
||||
if (tempoRimanente <= 0) {
|
||||
clearInterval(timerIntervallo);
|
||||
tempoRimanente = 0;
|
||||
displayTimer.innerText = "0";
|
||||
minuti.innerText = "00";
|
||||
secondi.innerText = "00";
|
||||
mostraFineGioco();//LISA E' COMPITO TUO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
|
||||
}
|
||||
}, 1000);
|
||||
@@ -236,36 +242,32 @@ function riposizionaSulBancone(rifiuto) {
|
||||
function mostraFineGioco() {
|
||||
// Blocca la generazione di nuovi rifiuti
|
||||
clearInterval(timerIntervallo);
|
||||
|
||||
// Mostra un messaggio finale più gradevole
|
||||
const messaggio = document.createElement('div');
|
||||
messaggio.id = 'fine-gioco';
|
||||
messaggio.innerHTML = `
|
||||
<h2>Tempo scaduto!</h2>
|
||||
<p>Il tuo punteggio finale è: <strong>${punteggio}</strong></p>
|
||||
<button id="restart-btn">Gioca di nuovo</button>
|
||||
`;
|
||||
messaggio.style.position = 'absolute';
|
||||
messaggio.style.top = '50%';
|
||||
messaggio.style.left = '50%';
|
||||
messaggio.style.transform = 'translate(-50%, -50%)';
|
||||
messaggio.style.backgroundColor = 'rgba(255, 255, 255, 0.9)';
|
||||
messaggio.style.padding = '20px';
|
||||
messaggio.style.borderRadius = '10px';
|
||||
messaggio.style.textAlign = 'center';
|
||||
messaggio.style.zIndex = '3000';
|
||||
|
||||
document.body.appendChild(messaggio);
|
||||
|
||||
// Salva il punteggio nel localStorage
|
||||
localStorage.setItem('punteggioFase2', punteggio);
|
||||
|
||||
// Aggiunge il pulsante per ricominciare
|
||||
document.getElementById('restart-btn').addEventListener('click', () => {
|
||||
document.body.removeChild(messaggio);
|
||||
punteggio = 0;
|
||||
scoreDisplay.innerText = punteggio;
|
||||
avviaTimer(60); // o il tempo che vuoi
|
||||
generaRifiuto();
|
||||
const username = localStorage.getItem('username') || 'Anonimo';
|
||||
const score1 = parseInt(localStorage.getItem('punteggioFase1')) || 0;
|
||||
const score2 = parseInt(localStorage.getItem('punteggioFase2')) || 0;
|
||||
const scoreT = 10_000 * ((score1 / MAX_SCORE) + (score2 / MAX_SCORE)) / 2;
|
||||
const data_partita = new Date().toISOString().split('T')[0]; // Formato YYYY-MM-DD
|
||||
fetch('http://localhost:8888/php/save_score.php', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
nome: username,
|
||||
score1: score1,
|
||||
score2: score2,
|
||||
scoreT: scoreT,
|
||||
data_partita: data_partita
|
||||
})
|
||||
})
|
||||
.then(response => response.text())
|
||||
.then(result => {
|
||||
console.log("Punteggio salvato con successo:", result);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error("Errore nella richiesta:", error);
|
||||
});
|
||||
window.location = "?pagina=end";
|
||||
}
|
||||
Reference in New Issue
Block a user