IA améliorée pour fantômes: chasse du personnage après cerise + difficulté progressive adaptée au niveau

This commit is contained in:
2025-11-28 19:03:47 +01:00
parent 1a38182209
commit 20f8b93f19

97
game.js
View File

@ -130,6 +130,8 @@ let totalDots = 0;
const TEST_MODE = true; const TEST_MODE = true;
let cherriesEaten = 0; let cherriesEaten = 0;
let isChangingLevel = false; let isChangingLevel = false;
let cherryEatenRecently = false;
let cherryEatenTimer = 0;
class Pacman { class Pacman {
constructor() { constructor() {
@ -139,7 +141,8 @@ class Pacman {
this.nextDirection = 0; this.nextDirection = 0;
this.mouthAngle = 0; this.mouthAngle = 0;
this.mouthOpen = true; this.mouthOpen = true;
this.speed = 0.15; this.baseSpeed = 0.15;
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;
this.colorAnimation = 0; this.colorAnimation = 0;
@ -227,6 +230,9 @@ class Pacman {
bonuses = bonuses.filter(b => !(b.x === this.x && b.y === this.y && b.type === BONUS_CHERRY)); bonuses = bonuses.filter(b => !(b.x === this.x && b.y === this.y && b.type === BONUS_CHERRY));
cherriesEaten++; cherriesEaten++;
cherryEatenRecently = true;
cherryEatenTimer = Math.max(150, 300 - (level - 1) * 20);
console.log('Après incrémentation, cherriesEaten:', cherriesEaten, 'TEST_MODE:', TEST_MODE, 'isChangingLevel:', isChangingLevel); console.log('Après incrémentation, cherriesEaten:', cherriesEaten, 'TEST_MODE:', TEST_MODE, 'isChangingLevel:', isChangingLevel);
if (TEST_MODE && cherriesEaten >= 2 && !isChangingLevel) { if (TEST_MODE && cherriesEaten >= 2 && !isChangingLevel) {
@ -276,18 +282,27 @@ class Ghost {
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;
this.moveCounter = 0; this.moveCounter = 0;
this.moveInterval = 30;
} }
updateSpeed() { updateSpeed() {
this.speed = this.baseSpeed * (1 + (level - 1) * 0.15); this.speed = this.baseSpeed * (1 + (level - 1) * 0.2);
} }
update() { update() {
if (!gameRunning) return; if (!gameRunning) return;
if (cherryEatenTimer > 0) {
cherryEatenTimer--;
} else {
cherryEatenRecently = false;
}
this.moveInterval = Math.max(15, 30 - (level - 1) * 2);
this.moveCounter++; this.moveCounter++;
if (this.moveCounter > 30 || !this.canMove(this.direction)) { if (this.moveCounter > this.moveInterval || !this.canMove(this.direction)) {
const possibleDirections = []; const possibleDirections = [];
for (let i = 0; i < 4; i++) { for (let i = 0; i < 4; i++) {
if (this.canMove(i)) { if (this.canMove(i)) {
@ -296,7 +311,11 @@ class Ghost {
} }
if (possibleDirections.length > 0) { if (possibleDirections.length > 0) {
this.direction = possibleDirections[Math.floor(Math.random() * possibleDirections.length)]; if (cherryEatenRecently) {
this.direction = this.getDirectionToPacman(possibleDirections);
} else {
this.direction = possibleDirections[Math.floor(Math.random() * possibleDirections.length)];
}
} }
this.moveCounter = 0; this.moveCounter = 0;
} }
@ -317,6 +336,56 @@ class Ghost {
this.y = Math.floor(this.pixelY / CELL_SIZE); this.y = Math.floor(this.pixelY / CELL_SIZE);
} }
getDirectionToPacman(possibleDirections) {
const dx = [0, 1, 0, -1];
const dy = [-1, 0, 1, 0];
let bestDirection = possibleDirections[0];
let minDistance = Infinity;
for (let dir of possibleDirections) {
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)
);
if (distance < minDistance) {
minDistance = distance;
bestDirection = dir;
}
}
return bestDirection;
}
getDirectionToPacman(possibleDirections) {
const dx = [0, 1, 0, -1];
const dy = [-1, 0, 1, 0];
let bestDirection = possibleDirections[0];
let minDistance = Infinity;
for (let dir of possibleDirections) {
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)
);
if (distance < minDistance) {
minDistance = distance;
bestDirection = dir;
}
}
return bestDirection;
}
canMove(direction) { canMove(direction) {
const dx = [0, 1, 0, -1]; const dx = [0, 1, 0, -1];
const dy = [-1, 0, 1, 0]; const dy = [-1, 0, 1, 0];
@ -624,16 +693,17 @@ function nextLevel() {
bonuses = []; bonuses = [];
console.log('Réinitialisation de Pacman et des fantômes'); console.log('Réinitialisation de Pacman et des fantômes');
pacman = new Pacman(); pacman = new Pacman();
ghosts[0] = new Ghost(14, 11, '#ff0000'); pacman.speed = pacman.baseSpeed * (1 + (level - 1) * 0.05);
ghosts[1] = new Ghost(15, 11, '#ff00ff'); ghosts[0] = new Ghost(14, 11, '#ff0000');
ghosts[2] = new Ghost(14, 12, '#00ffff'); ghosts[1] = new Ghost(15, 11, '#ff00ff');
ghosts[3] = new Ghost(15, 12, '#ffa500'); ghosts[2] = new Ghost(14, 12, '#00ffff');
ghosts[3] = new Ghost(15, 12, '#ffa500');
for (let ghost of ghosts) { for (let ghost of ghosts) {
ghost.updateSpeed(); ghost.updateSpeed();
} }
placeBonuses(); placeBonuses();
console.log('Bonus placés, nombre:', bonuses.length); console.log('Bonus placés, nombre:', bonuses.length);
@ -866,6 +936,7 @@ function initGame() {
restartBtn.style.display = 'none'; restartBtn.style.display = 'none';
pacman = new Pacman(); pacman = new Pacman();
pacman.speed = pacman.baseSpeed * (1 + (level - 1) * 0.05);
ghosts[0] = new Ghost(14, 11, '#ff0000'); ghosts[0] = new Ghost(14, 11, '#ff0000');
ghosts[1] = new Ghost(15, 11, '#ff00ff'); ghosts[1] = new Ghost(15, 11, '#ff00ff');
ghosts[2] = new Ghost(14, 12, '#00ffff'); ghosts[2] = new Ghost(14, 12, '#00ffff');