Contenu du cours
Jeux – débutant
Memory Animaux
0/2

Memory : objets du quotidien

Score : 0
Temps : 0 sec
Bravo ! Vous avez trouvé toutes les paires en secondes.

 

const objets = [
{ name: « téléphone », img: « img/telephone.png » },
{ name: « chaise », img: « img/chaise.png » },
{ name: « table », img: « img/table.png » },
{ name: « lampe », img: « img/lampe.png » },
{ name: « ordinateur », img: « img/ordinateur.png » },
{ name: « clavier », img: « img/clavier.png » },
{ name: « lunettes », img: « img/lunettes.png » },
{ name: « bouteille », img: « img/bouteille.png » },
{ name: « ciseaux », img: « img/ciseaux.png » },
{ name: « brosse », img: « img/brosse.png » },
{ name: « clés », img: « img/cles.png » },
{ name: « montre », img: « img/montre.png » },
{ name: « chaussure », img: « img/chaussure.png » },
{ name: « sac », img: « img/sac.png » },
{ name: « stylo », img: « img/stylo.png » },
{ name: « tasse », img: « img/tasse.png » }
];

let cardData = [];
objets.forEach(objet => {
cardData.push({ type: « word », content: objet.name, name: objet.name });
cardData.push({ type: « img », content: objet.img, name: objet.name });
});
cardData = cardData.sort(() => 0.5 – Math.random());

const board = document.getElementById(« game-board »);
const scoreDisplay = document.getElementById(« score »);
const timerDisplay = document.getElementById(« timer »);
const winMessage = document.getElementById(« win-message »);
const finalTime = document.getElementById(« final-time »);
const replayBtn = document.getElementById(« replay-btn »);
let firstCard = null;
let lockBoard = false;
let score = 0;
let seconds = 0;
let timerInterval = setInterval(() => {
seconds++;
timerDisplay.textContent = seconds;
}, 1000);

function speak(text) {
const utterance = new SpeechSynthesisUtterance(text);
utterance.lang = « fr-FR »;
speechSynthesis.speak(utterance);
}

function createCard(cardInfo) {
const card = document.createElement(« div »);
card.classList.add(« card »);
card.dataset.name = cardInfo.name;
card.dataset.type = cardInfo.type;
card.dataset.flipped = « false »;
card.addEventListener(« click », () => flipCard(card, cardInfo));
return card;
}

function flipCard(card, cardInfo) {
if (lockBoard || card.classList.contains(« matched ») || card.dataset.flipped === « true ») return;

card.dataset.flipped = « true »;
if (cardInfo.type === « img ») {
const img = document.createElement(« img »);
img.src = cardInfo.content;
card.appendChild(img);
} else {
card.textContent = cardInfo.content;
speak(cardInfo.content);
}

if (!firstCard) {
firstCard = { card, info: cardInfo };
} else {
lockBoard = true;
setTimeout(() => {
if (firstCard.info.name === cardInfo.name && firstCard.card !== card) {
firstCard.card.classList.add(« matched »);
card.classList.add(« matched »);
score++;
scoreDisplay.textContent = score;
if (score === objets.length) {
clearInterval(timerInterval);
finalTime.textContent = seconds;
winMessage.style.display = « block »;
replayBtn.style.display = « inline-block »;
}
} else {
resetCard(firstCard.card);
resetCard(card);
}
firstCard = null;
lockBoard = false;
}, 1000);
}
}

function resetCard(card) {
card.innerHTML = «  »;
card.dataset.flipped = « false »;
}

cardData.forEach(cardInfo => {
const card = createCard(cardInfo);
board.appendChild(card);
});