IA améliorée pour fantômes: chasse du personnage après cerise + difficulté progressive adaptée au niveau
This commit is contained in:
77
game.js
77
game.js
@ -130,6 +130,8 @@ let totalDots = 0;
|
||||
const TEST_MODE = true;
|
||||
let cherriesEaten = 0;
|
||||
let isChangingLevel = false;
|
||||
let cherryEatenRecently = false;
|
||||
let cherryEatenTimer = 0;
|
||||
|
||||
class Pacman {
|
||||
constructor() {
|
||||
@ -139,7 +141,8 @@ class Pacman {
|
||||
this.nextDirection = 0;
|
||||
this.mouthAngle = 0;
|
||||
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.pixelY = this.y * CELL_SIZE + CELL_SIZE / 2;
|
||||
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));
|
||||
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);
|
||||
|
||||
if (TEST_MODE && cherriesEaten >= 2 && !isChangingLevel) {
|
||||
@ -276,18 +282,27 @@ class Ghost {
|
||||
this.pixelX = this.x * CELL_SIZE + CELL_SIZE / 2;
|
||||
this.pixelY = this.y * CELL_SIZE + CELL_SIZE / 2;
|
||||
this.moveCounter = 0;
|
||||
this.moveInterval = 30;
|
||||
}
|
||||
|
||||
updateSpeed() {
|
||||
this.speed = this.baseSpeed * (1 + (level - 1) * 0.15);
|
||||
this.speed = this.baseSpeed * (1 + (level - 1) * 0.2);
|
||||
}
|
||||
|
||||
update() {
|
||||
if (!gameRunning) return;
|
||||
|
||||
if (cherryEatenTimer > 0) {
|
||||
cherryEatenTimer--;
|
||||
} else {
|
||||
cherryEatenRecently = false;
|
||||
}
|
||||
|
||||
this.moveInterval = Math.max(15, 30 - (level - 1) * 2);
|
||||
|
||||
this.moveCounter++;
|
||||
|
||||
if (this.moveCounter > 30 || !this.canMove(this.direction)) {
|
||||
if (this.moveCounter > this.moveInterval || !this.canMove(this.direction)) {
|
||||
const possibleDirections = [];
|
||||
for (let i = 0; i < 4; i++) {
|
||||
if (this.canMove(i)) {
|
||||
@ -296,8 +311,12 @@ class Ghost {
|
||||
}
|
||||
|
||||
if (possibleDirections.length > 0) {
|
||||
if (cherryEatenRecently) {
|
||||
this.direction = this.getDirectionToPacman(possibleDirections);
|
||||
} else {
|
||||
this.direction = possibleDirections[Math.floor(Math.random() * possibleDirections.length)];
|
||||
}
|
||||
}
|
||||
this.moveCounter = 0;
|
||||
}
|
||||
|
||||
@ -317,6 +336,56 @@ class Ghost {
|
||||
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) {
|
||||
const dx = [0, 1, 0, -1];
|
||||
const dy = [-1, 0, 1, 0];
|
||||
@ -626,6 +695,7 @@ function nextLevel() {
|
||||
|
||||
console.log('Réinitialisation de Pacman et des fantômes');
|
||||
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');
|
||||
@ -866,6 +936,7 @@ function initGame() {
|
||||
restartBtn.style.display = 'none';
|
||||
|
||||
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');
|
||||
|
||||
Reference in New Issue
Block a user