amelioratrion reactiviter fantome

This commit is contained in:
2025-12-01 22:50:37 +01:00
parent eab98868ef
commit da0671f612

46
game.js
View File

@ -313,7 +313,7 @@ class Ghost {
this.y = y; this.y = y;
this.color = color; this.color = color;
this.direction = Math.floor(Math.random() * 4); this.direction = Math.floor(Math.random() * 4);
this.baseSpeed = 0.1; this.baseSpeed = 0.15;
this.speed = this.baseSpeed; this.speed = this.baseSpeed;
this.pixelX = this.x * CELL_SIZE + CELL_SIZE / 2; this.pixelX = this.x * CELL_SIZE + CELL_SIZE / 2;
this.pixelY = this.y * 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++; this.moveCounter++;
@ -363,10 +363,12 @@ class Ghost {
} }
if (possibleDirections.length > 0) { if (possibleDirections.length > 0) {
if (cherryEatenRecently) { if (this.isVulnerable) {
this.direction = this.getDirectionToPacman(possibleDirections); // Fuir le joueur quand vulnérable
this.direction = this.getDirectionAwayFromPacman(possibleDirections);
} else { } else {
this.direction = possibleDirections[Math.floor(Math.random() * possibleDirections.length)]; // Toujours poursuivre le joueur
this.direction = this.getDirectionToPacman(possibleDirections);
} }
} }
this.moveCounter = 0; this.moveCounter = 0;
@ -392,16 +394,38 @@ class Ghost {
const dx = [0, 1, 0, -1]; const dx = [0, 1, 0, -1];
const dy = [-1, 0, 1, 0]; 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 bestDirection = possibleDirections[0];
let minDistance = Infinity; let minDistance = Infinity;
// Éviter de revenir en arrière si possible
const oppositeDirection = (this.direction + 2) % 4;
for (let dir of possibleDirections) { 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 nextX = this.x + dx[dir];
const nextY = this.y + dy[dir]; const nextY = this.y + dy[dir];
const distance = Math.sqrt( const distance = Math.sqrt(
Math.pow(pacman.x - nextX, 2) + Math.pow(targetX - nextX, 2) +
Math.pow(pacman.y - nextY, 2) Math.pow(targetY - nextY, 2)
); );
if (distance < minDistance) { if (distance < minDistance) {
@ -413,12 +437,12 @@ class Ghost {
return bestDirection; return bestDirection;
} }
getDirectionToPacman(possibleDirections) { getDirectionAwayFromPacman(possibleDirections) {
const dx = [0, 1, 0, -1]; const dx = [0, 1, 0, -1];
const dy = [-1, 0, 1, 0]; const dy = [-1, 0, 1, 0];
let bestDirection = possibleDirections[0]; let bestDirection = possibleDirections[0];
let minDistance = Infinity; let maxDistance = -1;
for (let dir of possibleDirections) { for (let dir of possibleDirections) {
const nextX = this.x + dx[dir]; const nextX = this.x + dx[dir];
@ -429,8 +453,8 @@ class Ghost {
Math.pow(pacman.y - nextY, 2) Math.pow(pacman.y - nextY, 2)
); );
if (distance < minDistance) { if (distance > maxDistance) {
minDistance = distance; maxDistance = distance;
bestDirection = dir; bestDirection = dir;
} }
} }