From 28063d9ba876c6fc78e8e351df91e7ca3c9e35dc Mon Sep 17 00:00:00 2001 From: syoul Date: Fri, 28 Nov 2025 19:20:23 +0100 Subject: [PATCH] =?UTF-8?q?Syst=C3=A8me=20de=20vies:=203=20vies=20avec=20a?= =?UTF-8?q?ffichage=20en=20c=C5=93urs,=20red=C3=A9marrage=20du=20niveau=20?= =?UTF-8?q?en=20cas=20de=20perte?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game.js | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++--- index.html | 1 + style.css | 12 ++++++++ 3 files changed, 97 insertions(+), 4 deletions(-) diff --git a/game.js b/game.js index bae9659..e61c0d1 100644 --- a/game.js +++ b/game.js @@ -2,6 +2,7 @@ const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); const scoreElement = document.getElementById('score'); const levelElement = document.getElementById('level'); +const livesElement = document.getElementById('lives'); const statusElement = document.getElementById('status'); const restartBtn = document.getElementById('restartBtn'); const usernameInput = document.getElementById('username'); @@ -131,6 +132,7 @@ let cherriesEaten = 0; let isChangingLevel = false; let cherryEatenRecently = false; let cherryEatenTimer = 0; +let lives = 3; class Pacman { constructor() { @@ -597,10 +599,17 @@ function checkCollisions() { ); if (distance < CELL_SIZE * 0.6) { - gameRunning = false; - statusElement.textContent = 'Game Over !'; - restartBtn.style.display = 'block'; - saveScore(); + lives--; + updateLivesDisplay(); + + if (lives <= 0) { + gameRunning = false; + statusElement.textContent = 'Game Over !'; + restartBtn.style.display = 'block'; + saveScore(); + } else { + restartCurrentLevel(); + } } } } @@ -631,6 +640,75 @@ function gameLoop() { } } +function updateLivesDisplay() { + const hearts = livesElement.querySelectorAll('.heart'); + hearts.forEach((heart, index) => { + if (index < lives) { + heart.style.opacity = '1'; + } else { + heart.style.opacity = '0.3'; + } + }); +} + +function restartCurrentLevel() { + if (isChangingLevel) return; + + console.log('Redémarrage du niveau actuel'); + isChangingLevel = true; + gameRunning = false; + + requestAnimationFrame(() => { + const currentLevel = level; + const mazeIndex = (currentLevel - 1) % mazeVariants.length; + const currentMaze = mazeVariants[mazeIndex]; + + maze = []; + for (let i = 0; i < currentMaze.length; i++) { + maze[i] = [...currentMaze[i]]; + } + + fillEmptySpaces(); + randomizeMaze(); + countDots(); + + bonuses = []; + placeBonuses(); + + pacman = new Pacman(); + pacman.speed = pacman.baseSpeed * (1 + (level - 1) * 0.05); + ghosts[0] = new Ghost(14, 11, '#ff0000'); + ghosts[1] = new Ghost(15, 11, '#ff00ff'); + ghosts[2] = new Ghost(14, 12, '#00ffff'); + ghosts[3] = new Ghost(15, 12, '#ffa500'); + + for (let ghost of ghosts) { + ghost.updateSpeed(); + } + + cherriesEaten = 0; + cherryEatenRecently = false; + cherryEatenTimer = 0; + + statusElement.textContent = `Niveau ${level} - Recommencement !`; + statusElement.style.color = '#ff6b6b'; + + drawMaze(); + pacman.draw(); + for (let ghost of ghosts) { + ghost.draw(); + } + + setTimeout(() => { + isChangingLevel = false; + gameRunning = true; + statusElement.textContent = 'En jeu'; + statusElement.style.color = '#ffd700'; + gameLoop(); + }, 2000); + }); +} + function nextLevel() { console.log('=== nextLevel() appelée ==='); console.log('gameRunning:', gameRunning, 'isChangingLevel:', isChangingLevel); @@ -921,9 +999,11 @@ function initGame() { countDots(); score = 0; level = 1; + lives = 3; cherriesEaten = 0; scoreElement.textContent = score; levelElement.textContent = level; + updateLivesDisplay(); gameRunning = true; statusElement.textContent = 'En jeu'; statusElement.style.color = '#ffd700'; diff --git a/index.html b/index.html index 920077f..2b7eb58 100644 --- a/index.html +++ b/index.html @@ -17,6 +17,7 @@
Score: 0
Niveau: 1
+
Vies:
Prêt à jouer
diff --git a/style.css b/style.css index 7c76600..92a8593 100644 --- a/style.css +++ b/style.css @@ -105,6 +105,18 @@ h1 { color: #ffd700; } +.lives { + font-size: 1.5em; + font-weight: bold; +} + +.lives .heart { + color: #ff0000; + font-size: 1.2em; + margin: 0 2px; + transition: opacity 0.3s; +} + #status { font-size: 1.2em; color: #ffd700;