791 lines
25 KiB
HTML
791 lines
25 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="fr" data-theme="dark">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
|
|
<title>Pac-Man - AP Manageur Arcade</title>
|
|
<style>
|
|
* { box-sizing: border-box; margin: 0; padding: 0; user-select: none; }
|
|
|
|
:root, html[data-theme="dark"] {
|
|
--bg: #071326;
|
|
--card-bg: #0d1d36;
|
|
--panel-box: #132746;
|
|
--border: #223c68;
|
|
--text: #eef4ff;
|
|
--muted: #9aa8c5;
|
|
--blue: #5ca7ff;
|
|
--blue-btn-text: #071326;
|
|
--maze-wall: #1e40af;
|
|
--maze-bg: #030712;
|
|
--dot-color: #fef08a;
|
|
--power-color: #ffffff;
|
|
}
|
|
html[data-theme="light"] {
|
|
--bg: #f2f5fa;
|
|
--card-bg: #ffffff;
|
|
--panel-box: #e9eef7;
|
|
--border: #d2dcec;
|
|
--text: #15203a;
|
|
--muted: #5b6b8c;
|
|
--blue: #1e6ef0;
|
|
--blue-btn-text: #ffffff;
|
|
--maze-wall: #2563eb;
|
|
--maze-bg: #0f172a;
|
|
--dot-color: #fde047;
|
|
--power-color: #ffffff;
|
|
}
|
|
html[data-theme="winxp"] {
|
|
--bg: #ece9d8;
|
|
--card-bg: #ece9d8;
|
|
--panel-box: #ffffff;
|
|
--border: #7f9db9;
|
|
--text: #000000;
|
|
--muted: #444444;
|
|
--blue: #0055ea;
|
|
--blue-btn-text: #ffffff;
|
|
--maze-wall: #0055ea;
|
|
--maze-bg: #000000;
|
|
--dot-color: #ffff00;
|
|
--power-color: #ffffff;
|
|
font-family: 'Tahoma', sans-serif !important;
|
|
}
|
|
html[data-theme="macos"][data-macos-mode="light"],
|
|
html[data-theme="macos"]:not([data-macos-mode="dark"]) {
|
|
--bg: #f5f5f7;
|
|
--card-bg: #ffffff;
|
|
--panel-box: #f2f2f7;
|
|
--border: rgba(0, 0, 0, 0.12);
|
|
--text: #1d1d1f;
|
|
--muted: #86868b;
|
|
--blue: #0071e3;
|
|
--blue-btn-text: #ffffff;
|
|
--maze-wall: #0071e3;
|
|
--maze-bg: #1c1c1e;
|
|
--dot-color: #ffcc00;
|
|
--power-color: #ffffff;
|
|
}
|
|
html[data-theme="macos"][data-macos-mode="dark"] {
|
|
--bg: #1e1e1e;
|
|
--card-bg: #2d2d2d;
|
|
--panel-box: #3a3a3c;
|
|
--border: rgba(255, 255, 255, 0.15);
|
|
--text: #f5f5f7;
|
|
--muted: #98989d;
|
|
--blue: #0a84ff;
|
|
--blue-btn-text: #ffffff;
|
|
--maze-wall: #0a84ff;
|
|
--maze-bg: #000000;
|
|
--dot-color: #ffd60a;
|
|
--power-color: #ffffff;
|
|
}
|
|
html[data-theme="futuristic"] {
|
|
--bg: #050811;
|
|
--card-bg: #091124;
|
|
--panel-box: #0f1c3a;
|
|
--border: #00f0ff;
|
|
--text: #e6f6ff;
|
|
--muted: #64b5f6;
|
|
--blue: #00f0ff;
|
|
--blue-btn-text: #050811;
|
|
--maze-wall: #00f0ff;
|
|
--maze-bg: #020713;
|
|
--dot-color: #fef08a;
|
|
--power-color: #00f0ff;
|
|
}
|
|
html[data-theme="hacker"] {
|
|
--bg: #000000;
|
|
--card-bg: #051206;
|
|
--panel-box: #0a1f0c;
|
|
--border: #00ff41;
|
|
--text: #00ff41;
|
|
--muted: #008f11;
|
|
--blue: #00ff41;
|
|
--blue-btn-text: #000000;
|
|
--maze-wall: #00ff41;
|
|
--maze-bg: #000000;
|
|
--dot-color: #a3e635;
|
|
--power-color: #ffffff;
|
|
font-family: 'Courier New', monospace !important;
|
|
}
|
|
|
|
body {
|
|
background: var(--bg);
|
|
color: var(--text);
|
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
justify-content: center;
|
|
min-height: 100vh;
|
|
overflow-x: hidden;
|
|
padding: 4px;
|
|
transition: background 0.3s ease, color 0.3s ease;
|
|
}
|
|
|
|
.game-card {
|
|
background: var(--card-bg);
|
|
border: 1px solid var(--border);
|
|
border-radius: 14px;
|
|
padding: 10px 12px;
|
|
box-shadow: 0 10px 30px rgba(0,0,0,0.5), 0 0 20px rgba(92,167,255,0.12);
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
max-width: 480px;
|
|
width: 100%;
|
|
}
|
|
|
|
.header-bar {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
width: 100%;
|
|
margin-bottom: 10px;
|
|
padding: 6px 10px;
|
|
background: var(--panel-box);
|
|
border-radius: 8px;
|
|
border: 1px solid var(--border);
|
|
font-size: 13px;
|
|
}
|
|
|
|
.scores-wrap {
|
|
display: flex;
|
|
gap: 16px;
|
|
font-weight: 700;
|
|
font-family: monospace;
|
|
font-size: 14px;
|
|
}
|
|
.score-val { color: var(--blue); }
|
|
.lives-val { color: #facc15; font-size: 16px; }
|
|
|
|
.canvas-wrap {
|
|
position: relative;
|
|
background: var(--maze-bg);
|
|
border: 2px solid var(--border);
|
|
border-radius: 10px;
|
|
overflow: hidden;
|
|
box-shadow: inset 0 0 15px rgba(0,0,0,0.6);
|
|
display: flex;
|
|
justify-content: center;
|
|
}
|
|
|
|
canvas {
|
|
display: block;
|
|
background: transparent;
|
|
}
|
|
|
|
.overlay {
|
|
position: absolute;
|
|
inset: 0;
|
|
background: rgba(3, 7, 18, 0.88);
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
justify-content: center;
|
|
gap: 12px;
|
|
z-index: 10;
|
|
backdrop-filter: blur(3px);
|
|
}
|
|
.overlay h2 {
|
|
font-size: 26px;
|
|
color: #ef4444;
|
|
letter-spacing: 2px;
|
|
text-transform: uppercase;
|
|
}
|
|
.overlay h2.win { color: #32d583; }
|
|
|
|
.btn {
|
|
background: var(--blue);
|
|
color: var(--blue-btn-text);
|
|
border: 0;
|
|
padding: 8px 22px;
|
|
border-radius: 6px;
|
|
font-weight: 700;
|
|
font-size: 13px;
|
|
cursor: pointer;
|
|
transition: all 0.15s ease;
|
|
}
|
|
.btn:hover { opacity: 0.9; transform: translateY(-1px); }
|
|
|
|
.touch-controls {
|
|
display: grid;
|
|
grid-template-columns: repeat(3, 50px);
|
|
gap: 6px;
|
|
margin-top: 10px;
|
|
justify-content: center;
|
|
}
|
|
.touch-btn {
|
|
width: 50px;
|
|
height: 44px;
|
|
background: var(--panel-box);
|
|
border: 1px solid var(--border);
|
|
border-radius: 8px;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
font-size: 18px;
|
|
color: var(--text);
|
|
cursor: pointer;
|
|
user-select: none;
|
|
}
|
|
.touch-btn:active {
|
|
background: var(--blue);
|
|
color: var(--blue-btn-text);
|
|
}
|
|
|
|
.controls-hint {
|
|
margin-top: 8px;
|
|
font-size: 11px;
|
|
color: var(--muted);
|
|
text-align: center;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<div class="game-card">
|
|
<div class="header-bar">
|
|
<div>🟡 <strong>Pac-Man</strong></div>
|
|
<div class="scores-wrap">
|
|
<div>SCORE: <span id="scoreEl" class="score-val">0</span></div>
|
|
<div>HI: <span id="hiScoreEl" class="score-val">0</span></div>
|
|
<div id="livesEl" class="lives-val">🟡🟡🟡</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="canvas-wrap">
|
|
<canvas id="pacmanCanvas" width="380" height="420"></canvas>
|
|
|
|
<div id="gameOverlay" class="overlay" style="display:none">
|
|
<h2 id="overlayTitle">GAME OVER</h2>
|
|
<div id="overlayScore" style="color:var(--blue);font-weight:700"></div>
|
|
<button id="overlayBtn" class="btn" onclick="initGame()">REJOUER</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="touch-controls">
|
|
<div></div>
|
|
<div class="touch-btn" onclick="setNextDir(0, -1)">⬆️</div>
|
|
<div></div>
|
|
<div class="touch-btn" onclick="setNextDir(-1, 0)">⬅️</div>
|
|
<div class="touch-btn" onclick="setNextDir(0, 1)">⬇️</div>
|
|
<div class="touch-btn" onclick="setNextDir(1, 0)">➡️</div>
|
|
</div>
|
|
|
|
<div id="pacInstructions" class="controls-hint">
|
|
Flèches directionnelles ou ZQSD pour guider Pac-Man. Mangez toutes les pac-gommes et fuyez les fantômes !
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
// Theme & i18n
|
|
function setTheme(theme, macosMode) {
|
|
if (!theme) theme = 'dark';
|
|
document.documentElement.dataset.theme = theme;
|
|
if (theme === 'macos' && macosMode) {
|
|
document.documentElement.dataset.macosMode = macosMode;
|
|
} else {
|
|
delete document.documentElement.dataset.macosMode;
|
|
}
|
|
}
|
|
|
|
const urlParams = new URLSearchParams(window.location.search);
|
|
setTheme(urlParams.get('theme') || 'dark', urlParams.get('macosMode'));
|
|
|
|
const I18N_PAC = {
|
|
fr: {
|
|
game_over: "GAME OVER",
|
|
level_cleared: "NIVEAU RÉUSSI ! 🎉",
|
|
replay_btn: "REJOUER",
|
|
next_level_btn: "NIVEAU SUIVANT ➡️",
|
|
instructions: "Flèches directionnelles ou ZQSD pour guider Pac-Man. Mangez toutes les pac-gommes et fuyez les fantômes !"
|
|
},
|
|
en: {
|
|
game_over: "GAME OVER",
|
|
level_cleared: "STAGE CLEARED! 🎉",
|
|
replay_btn: "PLAY AGAIN",
|
|
next_level_btn: "NEXT STAGE ➡️",
|
|
instructions: "Arrow keys or WASD to control Pac-Man. Eat all dots and avoid the ghosts!"
|
|
}
|
|
};
|
|
|
|
let currentPacLang = 'fr';
|
|
function applyPacLang(lang) {
|
|
currentPacLang = (lang && (lang.startsWith('en') ? 'en' : 'fr')) || 'fr';
|
|
const dict = I18N_PAC[currentPacLang] || I18N_PAC.fr;
|
|
const pi = document.getElementById('pacInstructions');
|
|
if (pi) pi.textContent = dict.instructions;
|
|
}
|
|
applyPacLang(urlParams.get('lang'));
|
|
|
|
window.addEventListener('message', e => {
|
|
if (e.data && e.data.type === 'themeChange') setTheme(e.data.theme, e.data.macosMode);
|
|
if (e.data && e.data.type === 'langChange') applyPacLang(e.data.lang);
|
|
});
|
|
|
|
// Sound Synthesizer
|
|
let audioCtx = null;
|
|
function playSnd(type) {
|
|
try {
|
|
if (!audioCtx) audioCtx = new (window.AudioContext || window.webkitAudioContext)();
|
|
if (audioCtx.state === 'suspended') audioCtx.resume();
|
|
const now = audioCtx.currentTime;
|
|
|
|
if (type === 'dot') {
|
|
const osc = audioCtx.createOscillator();
|
|
const g = audioCtx.createGain();
|
|
osc.connect(g); g.connect(audioCtx.destination);
|
|
osc.type = 'triangle';
|
|
osc.frequency.setValueAtTime(320, now);
|
|
osc.frequency.linearRampToValueAtTime(560, now + 0.04);
|
|
g.gain.setValueAtTime(0.04, now);
|
|
g.gain.exponentialRampToValueAtTime(0.001, now + 0.04);
|
|
osc.start(now); osc.stop(now + 0.04);
|
|
} else if (type === 'power') {
|
|
const osc = audioCtx.createOscillator();
|
|
const g = audioCtx.createGain();
|
|
osc.connect(g); g.connect(audioCtx.destination);
|
|
osc.type = 'sine';
|
|
osc.frequency.setValueAtTime(440, now);
|
|
osc.frequency.linearRampToValueAtTime(880, now + 0.15);
|
|
g.gain.setValueAtTime(0.08, now);
|
|
g.gain.exponentialRampToValueAtTime(0.001, now + 0.15);
|
|
osc.start(now); osc.stop(now + 0.15);
|
|
} else if (type === 'eat_ghost') {
|
|
const osc = audioCtx.createOscillator();
|
|
const g = audioCtx.createGain();
|
|
osc.connect(g); g.connect(audioCtx.destination);
|
|
osc.type = 'sawtooth';
|
|
osc.frequency.setValueAtTime(200, now);
|
|
osc.frequency.linearRampToValueAtTime(800, now + 0.2);
|
|
g.gain.setValueAtTime(0.08, now);
|
|
g.gain.exponentialRampToValueAtTime(0.001, now + 0.2);
|
|
osc.start(now); osc.stop(now + 0.2);
|
|
} else if (type === 'die') {
|
|
const osc = audioCtx.createOscillator();
|
|
const g = audioCtx.createGain();
|
|
osc.connect(g); g.connect(audioCtx.destination);
|
|
osc.type = 'sawtooth';
|
|
osc.frequency.setValueAtTime(500, now);
|
|
osc.frequency.linearRampToValueAtTime(100, now + 0.4);
|
|
g.gain.setValueAtTime(0.1, now);
|
|
g.gain.exponentialRampToValueAtTime(0.001, now + 0.4);
|
|
osc.start(now); osc.stop(now + 0.4);
|
|
} else if (type === 'win') {
|
|
[523.25, 659.25, 783.99, 1046.5].forEach((f, i) => {
|
|
const osc = audioCtx.createOscillator();
|
|
const g = audioCtx.createGain();
|
|
osc.connect(g); g.connect(audioCtx.destination);
|
|
osc.type = 'triangle';
|
|
osc.frequency.setValueAtTime(f, now + i * 0.08);
|
|
g.gain.setValueAtTime(0.07, now + i * 0.08);
|
|
g.gain.exponentialRampToValueAtTime(0.001, now + i * 0.08 + 0.2);
|
|
osc.start(now + i * 0.08); osc.stop(now + i * 0.08 + 0.2);
|
|
});
|
|
}
|
|
} catch (e) {}
|
|
}
|
|
|
|
// Canvas & Game Map (19 cols x 21 rows)
|
|
// 1: Wall, 0: Dot, 2: Power Pellet, 3: Empty / Ghost house, 4: Pacman spawn
|
|
const MAP_TEMPLATE = [
|
|
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
|
|
[1,2,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,2,1],
|
|
[1,0,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,0,1],
|
|
[1,0,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,0,1],
|
|
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
|
|
[1,0,1,1,0,1,0,1,1,1,1,1,0,1,0,1,1,0,1],
|
|
[1,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1],
|
|
[1,1,1,1,0,1,1,1,3,1,3,1,1,1,0,1,1,1,1],
|
|
[3,3,3,1,0,1,3,3,3,3,3,3,3,1,0,1,3,3,3],
|
|
[1,1,1,1,0,1,3,1,1,3,1,1,3,1,0,1,1,1,1],
|
|
[3,3,3,3,0,3,3,1,3,3,3,1,3,3,0,3,3,3,3],
|
|
[1,1,1,1,0,1,3,1,1,1,1,1,3,1,0,1,1,1,1],
|
|
[3,3,3,1,0,1,3,3,3,3,3,3,3,1,0,1,3,3,3],
|
|
[1,1,1,1,0,1,0,1,1,1,1,1,0,1,0,1,1,1,1],
|
|
[1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1],
|
|
[1,0,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,0,1],
|
|
[1,2,0,1,0,0,0,0,0,4,0,0,0,0,0,1,0,2,1],
|
|
[1,1,0,1,0,1,0,1,1,1,1,1,0,1,0,1,0,1,1],
|
|
[1,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1],
|
|
[1,0,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,0,1],
|
|
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
|
|
];
|
|
|
|
const canvas = document.getElementById('pacmanCanvas');
|
|
const ctx = canvas.getContext('2d');
|
|
const TILE = 20;
|
|
const COLS = 19;
|
|
const ROWS = 21;
|
|
|
|
let map = [];
|
|
let totalDots = 0;
|
|
let eatenDots = 0;
|
|
let score = 0;
|
|
let highScore = parseInt(localStorage.getItem('ap_pacman_hi') || '0', 10);
|
|
let lives = 3;
|
|
let isGameOver = false;
|
|
let frightenedTimer = 0;
|
|
|
|
// Pac-man state
|
|
const pacman = {
|
|
x: 9, y: 16,
|
|
pixelX: 9 * TILE, pixelY: 16 * TILE,
|
|
dirX: 0, dirY: 0,
|
|
nextDirX: 0, nextDirY: 0,
|
|
speed: 2,
|
|
mouthAngle: 0.2,
|
|
mouthDir: 1
|
|
};
|
|
|
|
// Ghosts
|
|
const GHOST_COLORS = ['#ff0000', '#ffb8ff', '#00ffff', '#ffb852'];
|
|
let ghosts = [];
|
|
|
|
function initGhosts() {
|
|
ghosts = [
|
|
{ x: 9, y: 8, pixelX: 9 * TILE, pixelY: 8 * TILE, dirX: 0, dirY: -1, color: GHOST_COLORS[0], speed: 1.8, isEaten: false },
|
|
{ x: 8, y: 10, pixelX: 8 * TILE, pixelY: 10 * TILE, dirX: 1, dirY: 0, color: GHOST_COLORS[1], speed: 1.6, isEaten: false },
|
|
{ x: 9, y: 10, pixelX: 9 * TILE, pixelY: 10 * TILE, dirX: 0, dirY: -1, color: GHOST_COLORS[2], speed: 1.6, isEaten: false },
|
|
{ x: 10, y: 10, pixelX: 10 * TILE, pixelY: 10 * TILE, dirX: -1, dirY: 0, color: GHOST_COLORS[3], speed: 1.5, isEaten: false }
|
|
];
|
|
}
|
|
|
|
function initGame() {
|
|
document.getElementById('gameOverlay').style.display = 'none';
|
|
map = MAP_TEMPLATE.map(row => [...row]);
|
|
totalDots = 0;
|
|
eatenDots = 0;
|
|
for (let r = 0; r < ROWS; r++) {
|
|
for (let c = 0; c < COLS; c++) {
|
|
if (map[r][c] === 0 || map[r][c] === 2) totalDots++;
|
|
}
|
|
}
|
|
|
|
score = 0;
|
|
lives = 3;
|
|
isGameOver = false;
|
|
frightenedTimer = 0;
|
|
updateUI();
|
|
resetPositions();
|
|
}
|
|
|
|
function resetPositions() {
|
|
pacman.x = 9;
|
|
pacman.y = 16;
|
|
pacman.pixelX = 9 * TILE;
|
|
pacman.pixelY = 16 * TILE;
|
|
pacman.dirX = 0;
|
|
pacman.dirY = 0;
|
|
pacman.nextDirX = 0;
|
|
pacman.nextDirY = 0;
|
|
initGhosts();
|
|
}
|
|
|
|
function updateUI() {
|
|
document.getElementById('scoreEl').textContent = score;
|
|
document.getElementById('hiScoreEl').textContent = highScore;
|
|
let hearts = '';
|
|
for (let i = 0; i < lives; i++) hearts += '🟡';
|
|
document.getElementById('livesEl').textContent = hearts || '💀';
|
|
}
|
|
|
|
function setNextDir(dx, dy) {
|
|
pacman.nextDirX = dx;
|
|
pacman.nextDirY = dy;
|
|
if (pacman.dirX === 0 && pacman.dirY === 0) {
|
|
pacman.dirX = dx;
|
|
pacman.dirY = dy;
|
|
}
|
|
}
|
|
|
|
function canMove(tileX, tileY) {
|
|
if (tileX < 0 || tileX >= COLS) return true; // warp tunnel
|
|
if (tileY < 0 || tileY >= ROWS) return false;
|
|
return map[tileY][tileX] !== 1;
|
|
}
|
|
|
|
function update() {
|
|
if (!isGameOver) {
|
|
// Pacman mouth animation
|
|
pacman.mouthAngle += 0.04 * pacman.mouthDir;
|
|
if (pacman.mouthAngle >= 0.4 || pacman.mouthAngle <= 0.04) {
|
|
pacman.mouthDir *= -1;
|
|
}
|
|
|
|
// Frightened timer countdown
|
|
if (frightenedTimer > 0) {
|
|
frightenedTimer--;
|
|
}
|
|
|
|
// Turning at grid tile center
|
|
const onGridX = (pacman.pixelX % TILE === 0);
|
|
const onGridY = (pacman.pixelY % TILE === 0);
|
|
|
|
if (onGridX && onGridY) {
|
|
const curTileX = Math.round(pacman.pixelX / TILE);
|
|
const curTileY = Math.round(pacman.pixelY / TILE);
|
|
pacman.x = curTileX;
|
|
pacman.y = curTileY;
|
|
|
|
// Eat dot / power pellet
|
|
if (curTileY >= 0 && curTileY < ROWS && curTileX >= 0 && curTileX < COLS) {
|
|
if (map[curTileY][curTileX] === 0) {
|
|
map[curTileY][curTileX] = 3;
|
|
score += 10;
|
|
eatenDots++;
|
|
playSnd('dot');
|
|
checkWin();
|
|
} else if (map[curTileY][curTileX] === 2) {
|
|
map[curTileY][curTileX] = 3;
|
|
score += 50;
|
|
eatenDots++;
|
|
frightenedTimer = 350; // ~6 seconds
|
|
playSnd('power');
|
|
checkWin();
|
|
}
|
|
}
|
|
|
|
if (score > highScore) {
|
|
highScore = score;
|
|
localStorage.setItem('ap_pacman_hi', highScore);
|
|
}
|
|
updateUI();
|
|
|
|
// Try turn in requested direction
|
|
if (canMove(curTileX + pacman.nextDirX, curTileY + pacman.nextDirY)) {
|
|
pacman.dirX = pacman.nextDirX;
|
|
pacman.dirY = pacman.nextDirY;
|
|
}
|
|
|
|
// If blocked in current direction, stop
|
|
if (!canMove(curTileX + pacman.dirX, curTileY + pacman.dirY)) {
|
|
pacman.dirX = 0;
|
|
pacman.dirY = 0;
|
|
}
|
|
}
|
|
|
|
// Move Pacman
|
|
pacman.pixelX += pacman.dirX * pacman.speed;
|
|
pacman.pixelY += pacman.dirY * pacman.speed;
|
|
|
|
// Wrap tunnel left-right
|
|
if (pacman.pixelX < -TILE / 2) pacman.pixelX = (COLS - 0.5) * TILE;
|
|
if (pacman.pixelX > (COLS - 0.5) * TILE) pacman.pixelX = -TILE / 2;
|
|
|
|
// Move Ghosts
|
|
ghosts.forEach(g => {
|
|
const gOnGridX = (g.pixelX % TILE === 0);
|
|
const gOnGridY = (g.pixelY % TILE === 0);
|
|
|
|
if (gOnGridX && gOnGridY) {
|
|
const gx = Math.round(g.pixelX / TILE);
|
|
const gy = Math.round(g.pixelY / TILE);
|
|
g.x = gx;
|
|
g.y = gy;
|
|
|
|
const possibleDirs = [
|
|
{ dx: 1, dy: 0 }, { dx: -1, dy: 0 }, { dx: 0, dy: 1 }, { dx: 0, dy: -1 }
|
|
].filter(d => {
|
|
// Don't turn immediately 180 degrees backwards unless forced
|
|
if (d.dx === -g.dirX && d.dy === -g.dirY) return false;
|
|
return canMove(gx + d.dx, gy + d.dy);
|
|
});
|
|
|
|
if (possibleDirs.length > 0) {
|
|
// Simple AI: target Pacman or random when frightened
|
|
if (frightenedTimer > 0 || Math.random() < 0.3) {
|
|
const pick = possibleDirs[Math.floor(Math.random() * possibleDirs.length)];
|
|
g.dirX = pick.dx;
|
|
g.dirY = pick.dy;
|
|
} else {
|
|
// Best direction towards Pacman
|
|
possibleDirs.sort((a, b) => {
|
|
const distA = Math.hypot((gx + a.dx) - pacman.x, (gy + a.dy) - pacman.y);
|
|
const distB = Math.hypot((gx + b.dx) - pacman.x, (gy + b.dy) - pacman.y);
|
|
return distA - distB;
|
|
});
|
|
g.dirX = possibleDirs[0].dx;
|
|
g.dirY = possibleDirs[0].dy;
|
|
}
|
|
} else if (!canMove(gx + g.dirX, gy + g.dirY)) {
|
|
g.dirX *= -1;
|
|
g.dirY *= -1;
|
|
}
|
|
}
|
|
|
|
const curGhostSpeed = (frightenedTimer > 0) ? 1.0 : g.speed;
|
|
g.pixelX += g.dirX * curGhostSpeed;
|
|
g.pixelY += g.dirY * curGhostSpeed;
|
|
|
|
// Ghost Wrap
|
|
if (g.pixelX < -TILE / 2) g.pixelX = (COLS - 0.5) * TILE;
|
|
if (g.pixelX > (COLS - 0.5) * TILE) g.pixelX = -TILE / 2;
|
|
|
|
// Collision detection with Pacman
|
|
const dist = Math.hypot(g.pixelX - pacman.pixelX, g.pixelY - pacman.pixelY);
|
|
if (dist < TILE * 0.75) {
|
|
if (frightenedTimer > 0 && !g.isEaten) {
|
|
// Eat ghost
|
|
g.isEaten = true;
|
|
g.pixelX = 9 * TILE;
|
|
g.pixelY = 10 * TILE;
|
|
score += 200;
|
|
playSnd('eat_ghost');
|
|
updateUI();
|
|
} else if (!g.isEaten) {
|
|
// Pacman dies
|
|
lives--;
|
|
playSnd('die');
|
|
updateUI();
|
|
if (lives <= 0) {
|
|
gameOver(false);
|
|
} else {
|
|
resetPositions();
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
draw();
|
|
requestAnimationFrame(update);
|
|
}
|
|
|
|
function checkWin() {
|
|
if (eatenDots >= totalDots) {
|
|
gameOver(true);
|
|
}
|
|
}
|
|
|
|
function gameOver(isWin) {
|
|
isGameOver = true;
|
|
const overlay = document.getElementById('gameOverlay');
|
|
const title = document.getElementById('overlayTitle');
|
|
const scoreTxt = document.getElementById('overlayScore');
|
|
const btn = document.getElementById('overlayBtn');
|
|
const dict = I18N_PAC[currentPacLang] || I18N_PAC.fr;
|
|
|
|
overlay.style.display = 'flex';
|
|
scoreTxt.textContent = `Score final : ${score}`;
|
|
if (isWin) {
|
|
title.textContent = dict.level_cleared;
|
|
title.className = 'win';
|
|
btn.textContent = dict.next_level_btn;
|
|
playSnd('win');
|
|
} else {
|
|
title.textContent = dict.game_over;
|
|
title.className = '';
|
|
btn.textContent = dict.replay_btn;
|
|
}
|
|
}
|
|
|
|
function draw() {
|
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
// Draw Maze
|
|
const wallColor = getComputedStyle(document.documentElement).getPropertyValue('--maze-wall').trim() || '#1e40af';
|
|
const dotColor = getComputedStyle(document.documentElement).getPropertyValue('--dot-color').trim() || '#fde047';
|
|
const powerColor = getComputedStyle(document.documentElement).getPropertyValue('--power-color').trim() || '#ffffff';
|
|
|
|
for (let r = 0; r < ROWS; r++) {
|
|
for (let c = 0; c < COLS; c++) {
|
|
const type = map[r][c];
|
|
const px = c * TILE;
|
|
const py = r * TILE;
|
|
|
|
if (type === 1) {
|
|
ctx.fillStyle = wallColor;
|
|
ctx.fillRect(px, py, TILE, TILE);
|
|
ctx.strokeStyle = 'rgba(0,0,0,0.2)';
|
|
ctx.strokeRect(px + 1, py + 1, TILE - 2, TILE - 2);
|
|
} else if (type === 0) {
|
|
ctx.fillStyle = dotColor;
|
|
ctx.beginPath();
|
|
ctx.arc(px + TILE / 2, py + TILE / 2, 2.5, 0, Math.PI * 2);
|
|
ctx.fill();
|
|
} else if (type === 2) {
|
|
// Power pellet pulsing
|
|
const pulse = (Math.sin(performance.now() * 0.008) + 1) * 2 + 4;
|
|
ctx.fillStyle = powerColor;
|
|
ctx.beginPath();
|
|
ctx.arc(px + TILE / 2, py + TILE / 2, pulse, 0, Math.PI * 2);
|
|
ctx.fill();
|
|
}
|
|
}
|
|
}
|
|
|
|
// Draw Pac-Man
|
|
const pmX = pacman.pixelX + TILE / 2;
|
|
const pmY = pacman.pixelY + TILE / 2;
|
|
let baseRot = 0;
|
|
if (pacman.dirX === 1) baseRot = 0;
|
|
else if (pacman.dirX === -1) baseRot = Math.PI;
|
|
else if (pacman.dirY === 1) baseRot = Math.PI * 0.5;
|
|
else if (pacman.dirY === -1) baseRot = Math.PI * 1.5;
|
|
|
|
ctx.fillStyle = '#facc15';
|
|
ctx.beginPath();
|
|
ctx.arc(
|
|
pmX, pmY, TILE * 0.44,
|
|
baseRot + pacman.mouthAngle * Math.PI,
|
|
baseRot + (2 - pacman.mouthAngle) * Math.PI
|
|
);
|
|
ctx.lineTo(pmX, pmY);
|
|
ctx.fill();
|
|
|
|
// Draw Ghosts
|
|
ghosts.forEach(g => {
|
|
const gx = g.pixelX + TILE / 2;
|
|
const gy = g.pixelY + TILE / 2;
|
|
const isScared = frightenedTimer > 0;
|
|
const isBlinking = frightenedTimer < 100 && Math.floor(frightenedTimer / 10) % 2 === 0;
|
|
|
|
ctx.fillStyle = isScared ? (isBlinking ? '#ffffff' : '#1d4ed8') : g.color;
|
|
|
|
// Ghost body & rounded head
|
|
ctx.beginPath();
|
|
ctx.arc(gx, gy - 2, TILE * 0.42, Math.PI, 0, false);
|
|
ctx.lineTo(gx + TILE * 0.42, gy + TILE * 0.4);
|
|
// Tentacles
|
|
ctx.lineTo(gx + TILE * 0.2, gy + TILE * 0.25);
|
|
ctx.lineTo(gx, gy + TILE * 0.4);
|
|
ctx.lineTo(gx - TILE * 0.2, gy + TILE * 0.25);
|
|
ctx.lineTo(gx - TILE * 0.42, gy + TILE * 0.4);
|
|
ctx.closePath();
|
|
ctx.fill();
|
|
|
|
// Eyes
|
|
const eyeOffsetX = g.dirX * 2;
|
|
const eyeOffsetY = g.dirY * 2;
|
|
ctx.fillStyle = '#ffffff';
|
|
ctx.beginPath();
|
|
ctx.arc(gx - 4 + eyeOffsetX, gy - 4 + eyeOffsetY, 3, 0, Math.PI * 2);
|
|
ctx.arc(gx + 4 + eyeOffsetX, gy - 4 + eyeOffsetY, 3, 0, Math.PI * 2);
|
|
ctx.fill();
|
|
|
|
ctx.fillStyle = isScared ? '#ff0000' : '#1e3a8a';
|
|
ctx.beginPath();
|
|
ctx.arc(gx - 4 + eyeOffsetX * 1.5, gy - 4 + eyeOffsetY * 1.5, 1.5, 0, Math.PI * 2);
|
|
ctx.arc(gx + 4 + eyeOffsetX * 1.5, gy - 4 + eyeOffsetY * 1.5, 1.5, 0, Math.PI * 2);
|
|
ctx.fill();
|
|
});
|
|
}
|
|
|
|
// Keyboard controls
|
|
window.addEventListener('keydown', e => {
|
|
if (['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', ' '].includes(e.key)) {
|
|
e.preventDefault();
|
|
}
|
|
if (e.key === 'ArrowUp' || e.key === 'z' || e.key === 'Z' || e.key === 'w' || e.key === 'W') setNextDir(0, -1);
|
|
else if (e.key === 'ArrowDown' || e.key === 's' || e.key === 'S') setNextDir(0, 1);
|
|
else if (e.key === 'ArrowLeft' || e.key === 'q' || e.key === 'Q' || e.key === 'a' || e.key === 'A') setNextDir(-1, 0);
|
|
else if (e.key === 'ArrowRight' || e.key === 'd' || e.key === 'D') setNextDir(1, 0);
|
|
});
|
|
|
|
initGame();
|
|
update();
|
|
</script>
|
|
</body>
|
|
</html> |