diff --git a/game.js b/game.js index ea5cefb..1ffcd92 100644 --- a/game.js +++ b/game.js @@ -313,7 +313,7 @@ class Ghost { this.y = y; this.color = color; this.direction = Math.floor(Math.random() * 4); - this.baseSpeed = 0.1; + this.baseSpeed = 0.15; this.speed = this.baseSpeed; this.pixelX = this.x * CELL_SIZE + CELL_SIZE / 2; this.pixelY = this.y * CELL_SIZE + CELL_SIZE / 2; @@ -350,7 +350,7 @@ class Ghost { } } - this.moveInterval = Math.max(15, 30 - (level - 1) * 2); + this.moveInterval = Math.max(8, 20 - (level - 1) * 2); this.moveCounter++; @@ -363,10 +363,12 @@ class Ghost { } if (possibleDirections.length > 0) { - if (cherryEatenRecently) { - this.direction = this.getDirectionToPacman(possibleDirections); + if (this.isVulnerable) { + // Fuir le joueur quand vulnérable + this.direction = this.getDirectionAwayFromPacman(possibleDirections); } else { - this.direction = possibleDirections[Math.floor(Math.random() * possibleDirections.length)]; + // Toujours poursuivre le joueur + this.direction = this.getDirectionToPacman(possibleDirections); } } this.moveCounter = 0; @@ -392,16 +394,38 @@ class Ghost { const dx = [0, 1, 0, -1]; const dy = [-1, 0, 1, 0]; + // Prédire la position future de Pacman basée sur sa direction + let targetX = pacman.x; + let targetY = pacman.y; + + // Si Pacman bouge, prédire où il sera + if (pacman.direction !== undefined) { + const futureX = pacman.x + dx[pacman.direction] * 2; + const futureY = pacman.y + dy[pacman.direction] * 2; + if (futureX >= 0 && futureX < COLS && futureY >= 0 && futureY < ROWS) { + targetX = futureX; + targetY = futureY; + } + } + let bestDirection = possibleDirections[0]; let minDistance = Infinity; + // Éviter de revenir en arrière si possible + const oppositeDirection = (this.direction + 2) % 4; + for (let dir of possibleDirections) { + // Éviter la direction opposée sauf si c'est la seule option + if (possibleDirections.length > 1 && dir === oppositeDirection) { + continue; + } + const nextX = this.x + dx[dir]; const nextY = this.y + dy[dir]; const distance = Math.sqrt( - Math.pow(pacman.x - nextX, 2) + - Math.pow(pacman.y - nextY, 2) + Math.pow(targetX - nextX, 2) + + Math.pow(targetY - nextY, 2) ); if (distance < minDistance) { @@ -413,12 +437,12 @@ class Ghost { return bestDirection; } - getDirectionToPacman(possibleDirections) { + getDirectionAwayFromPacman(possibleDirections) { const dx = [0, 1, 0, -1]; const dy = [-1, 0, 1, 0]; let bestDirection = possibleDirections[0]; - let minDistance = Infinity; + let maxDistance = -1; for (let dir of possibleDirections) { const nextX = this.x + dx[dir]; @@ -429,8 +453,8 @@ class Ghost { Math.pow(pacman.y - nextY, 2) ); - if (distance < minDistance) { - minDistance = distance; + if (distance > maxDistance) { + maxDistance = distance; bestDirection = dir; } }