Spaces:
Running

pacman / index.html
Engelko's picture
Add 2 files
c411cb0 verified
Raw History Blame Contribute Delete
28.2 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tailwind Pacman</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
@keyframes pacman-mouth {
0% { clip-path: polygon(50% 50%, 100% 0%, 100% 100%); }
50% { clip-path: polygon(50% 50%, 100% 25%, 100% 75%); }
100% { clip-path: polygon(50% 50%, 100% 0%, 100% 100%); }
}
@keyframes ghost-float {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-5px); }
}
@keyframes scared-ghost {
0%, 100% { background-color: #2563eb; }
50% { background-color: #93c5fd; }
}
.pacman {
animation: pacman-mouth 0.5s infinite;
clip-path: polygon(50% 50%, 100% 0%, 100% 100%);
}
.ghost {
animation: ghost-float 2s infinite;
}
.scared {
animation: scared-ghost 0.5s infinite;
}
#game-board {
position: relative;
background-color: #111827;
border: 4px solid #f59e0b;
}
.wall {
background-color: #1e40af;
}
.pellet {
width: 8px;
height: 8px;
background-color: #fef3c7;
border-radius: 50%;
}
.power-pellet {
width: 16px;
height: 16px;
background-color: #fef3c7;
border-radius: 50%;
animation: pulse 1s infinite;
}
@keyframes pulse {
0%, 100% { transform: scale(1); opacity: 1; }
50% { transform: scale(1.3); opacity: 0.8; }
}
</style>
</head>
<body class="bg-gray-900 text-white min-h-screen flex flex-col items-center justify-center p-4">
<div class="text-center mb-6">
<h1 class="text-4xl font-bold text-yellow-300 mb-2">Tailwind Pacman</h1>
<div class="flex justify-center items-center gap-8 mb-4">
<div class="flex items-center">
<div class="w-6 h-6 bg-yellow-300 rounded-full mr-2"></div>
<span>Score: <span id="score">0</span></span>
</div>
<div class="flex items-center">
<div class="w-6 h-6 bg-red-500 rounded-full mr-2"></div>
<span>Lives: <span id="lives">3</span></span>
</div>
</div>
<button id="start-btn" class="bg-green-600 hover:bg-green-700 text-white font-bold py-2 px-6 rounded-full transition">
Start Game
</button>
</div>
<div id="game-board" class="grid grid-cols-28 grid-rows-31 gap-0 w-[560px] h-[620px] relative overflow-hidden">
<!-- Game elements will be generated by JavaScript -->
</div>
<div class="mt-6 text-center">
<p class="text-gray-400">Use arrow keys to move. Eat all pellets to win!</p>
<p class="text-gray-400 mt-2">Eat power pellets to scare ghosts!</p>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
// Game constants
const GRID_SIZE = 28;
const CELL_SIZE = 20;
const PACMAN_SPEED = 2;
const GHOST_SPEED = 1.5;
const SCARED_DURATION = 10000; // 10 seconds
// Game state
let score = 0;
let lives = 3;
let gameRunning = false;
let scaredTimer = null;
let ghostsScared = false;
let pelletsRemaining = 0;
// DOM elements
const gameBoard = document.getElementById('game-board');
const scoreDisplay = document.getElementById('score');
const livesDisplay = document.getElementById('lives');
const startBtn = document.getElementById('start-btn');
// Game elements
let pacman = null;
let ghosts = [];
let pellets = [];
let powerPellets = [];
let walls = [];
// Movement
let direction = 'right';
let nextDirection = 'right';
let moving = false;
// Maze layout (1 = wall, 0 = path, p = pellet, P = power pellet)
const mazeLayout = [
'1111111111111111111111111111',
'1pppppppppppp1pppppppppppp1',
'1p1111p11111p1p11111p1111p1',
'1P1111p11111p1p11111p1111P1',
'1p1111p11111p1p11111p1111p1',
'1pppppppppppppppppppppppp1',
'1p1111p1p111111111p1p1111p1',
'1p1111p1p111111111p1p1111p1',
'1ppppppp1pppp1pppp1ppppppp1',
'1111111p1110p1p0111p1111111',
'0000011p1110p1p0111p1100000',
'0000011p100000000011p1100000',
'0000011p100111111011p1100000',
'1111111p100100001011p1111111',
'0000000ppp00p00000ppp0000000',
'1111111p100111111011p1111111',
'0000011p100000000011p1100000',
'0000011p100111111011p1100000',
'1111111p100p11111011p1111111',
'1ppppppppppp1ppppppppppppp1',
'1p1111p11111p1p11111p1111p1',
'1p1111p11111p1p11111p1111p1',
'1Ppppp1pppppp0pppppp1ppppP1',
'111p1p1p1p1111111p1p1p1p1111',
'111p1p1p1p1111111p1p1p1p1111',
'1ppppp1pppppp1pppppp1pppppp1',
'1p1111111111p1p11111111111p1',
'1p1111111111p1p11111111111p1',
'1ppppppppppppppppppppppppp1',
'1111111111111111111111111111'
];
// Initialize game
function initGame() {
clearGame();
createMaze();
createPacman();
createGhosts();
updateDisplay();
}
// Clear game elements
function clearGame() {
gameBoard.innerHTML = '';
ghosts = [];
pellets = [];
powerPellets = [];
walls = [];
pelletsRemaining = 0;
direction = 'right';
nextDirection = 'right';
moving = false;
clearTimeout(scaredTimer);
ghostsScared = false;
}
// Create maze from layout
function createMaze() {
for (let y = 0; y < mazeLayout.length; y++) {
for (let x = 0; x < mazeLayout[y].length; x++) {
const cell = document.createElement('div');
cell.className = 'absolute';
cell.style.width = `${CELL_SIZE}px`;
cell.style.height = `${CELL_SIZE}px`;
cell.style.left = `${x * CELL_SIZE}px`;
cell.style.top = `${y * CELL_SIZE}px`;
switch (mazeLayout[y][x]) {
case '1':
cell.classList.add('wall');
walls.push({ x, y });
break;
case 'p':
createPellet(x, y);
pelletsRemaining++;
break;
case 'P':
createPowerPellet(x, y);
pelletsRemaining++;
break;
}
gameBoard.appendChild(cell);
}
}
}
// Create a pellet
function createPellet(x, y) {
const pellet = document.createElement('div');
pellet.className = 'pellet absolute';
pellet.style.left = `${x * CELL_SIZE + CELL_SIZE / 2 - 4}px`;
pellet.style.top = `${y * CELL_SIZE + CELL_SIZE / 2 - 4}px`;
gameBoard.appendChild(pellet);
pellets.push({ element: pellet, x, y });
}
// Create a power pellet
function createPowerPellet(x, y) {
const powerPellet = document.createElement('div');
powerPellet.className = 'power-pellet absolute';
powerPellet.style.left = `${x * CELL_SIZE + CELL_SIZE / 2 - 8}px`;
powerPellet.style.top = `${y * CELL_SIZE + CELL_SIZE / 2 - 8}px`;
gameBoard.appendChild(powerPellet);
powerPellets.push({ element: powerPellet, x, y });
}
// Create Pacman
function createPacman() {
pacman = document.createElement('div');
pacman.className = 'pacman bg-yellow-300 absolute rounded-full';
pacman.style.width = `${CELL_SIZE}px`;
pacman.style.height = `${CELL_SIZE}px`;
pacman.style.left = `${14 * CELL_SIZE}px`;
pacman.style.top = `${23 * CELL_SIZE}px`;
pacman.style.transform = 'rotate(0deg)';
gameBoard.appendChild(pacman);
}
// Create ghosts
function createGhosts() {
const ghostColors = ['bg-red-500', 'bg-pink-500', 'bg-cyan-400', 'bg-orange-400'];
const ghostPositions = [
{ x: 13, y: 11 },
{ x: 14, y: 11 },
{ x: 13, y: 14 },
{ x: 14, y: 14 }
];
ghostPositions.forEach((pos, index) => {
const ghost = document.createElement('div');
ghost.className = `ghost ${ghostColors[index]} absolute rounded-t-full`;
ghost.style.width = `${CELL_SIZE}px`;
ghost.style.height = `${CELL_SIZE}px`;
ghost.style.left = `${pos.x * CELL_SIZE}px`;
ghost.style.top = `${pos.y * CELL_SIZE}px`;
// Add ghost eyes
const eyes = document.createElement('div');
eyes.className = 'flex justify-between absolute w-full px-1 top-1';
eyes.innerHTML = `
<div class="w-2 h-2 bg-white rounded-full flex items-center justify-center">
<div class="w-1 h-1 bg-black rounded-full"></div>
</div>
<div class="w-2 h-2 bg-white rounded-full flex items-center justify-center">
<div class="w-1 h-1 bg-black rounded-full"></div>
</div>
`;
ghost.appendChild(eyes);
gameBoard.appendChild(ghost);
ghosts.push({
element: ghost,
x: pos.x,
y: pos.y,
direction: ['up', 'down', 'left', 'right'][Math.floor(Math.random() * 4)],
speed: GHOST_SPEED,
isScared: false
});
});
}
// Update score and lives display
function updateDisplay() {
scoreDisplay.textContent = score;
livesDisplay.textContent = lives;
}
// Start the game
function startGame() {
if (gameRunning) return;
initGame();
gameRunning = true;
startBtn.textContent = 'Restart Game';
moving = true;
// Game loop
const gameLoop = setInterval(() => {
if (!gameRunning) {
clearInterval(gameLoop);
return;
}
movePacman();
moveGhosts();
checkCollisions();
// Win condition
if (pelletsRemaining === 0) {
gameOver(true);
}
}, 16); // ~60fps
}
// Move Pacman
function movePacman() {
if (!moving) return;
let newX = Math.floor(parseInt(pacman.style.left) / CELL_SIZE);
let newY = Math.floor(parseInt(pacman.style.top) / CELL_SIZE);
// Check if Pacman is centered in a cell to change direction
const pacmanLeft = parseInt(pacman.style.left);
const pacmanTop = parseInt(pacman.style.top);
const centeredX = pacmanLeft % CELL_SIZE === 0;
const centeredY = pacmanTop % CELL_SIZE === 0;
if (centeredX && centeredY) {
// Try to change direction if possible
const tempDirection = direction;
direction = nextDirection;
if (canMove(newX, newY, direction)) {
// Direction changed successfully
} else if (canMove(newX, newY, tempDirection)) {
direction = tempDirection;
} else {
direction = tempDirection;
moving = false;
return;
}
// Update Pacman's rotation
switch (direction) {
case 'up': pacman.style.transform = 'rotate(-90deg)'; break;
case 'down': pacman.style.transform = 'rotate(90deg)'; break;
case 'left': pacman.style.transform = 'rotate(180deg)'; break;
case 'right': pacman.style.transform = 'rotate(0deg)'; break;
}
}
// Move Pacman
switch (direction) {
case 'up':
pacman.style.top = `${parseInt(pacman.style.top) - PACMAN_SPEED}px`;
break;
case 'down':
pacman.style.top = `${parseInt(pacman.style.top) + PACMAN_SPEED}px`;
break;
case 'left':
pacman.style.left = `${parseInt(pacman.style.left) - PACMAN_SPEED}px`;
break;
case 'right':
pacman.style.left = `${parseInt(pacman.style.left) + PACMAN_SPEED}px`;
break;
}
// Handle tunnel/wrap-around
if (parseInt(pacman.style.left) < -CELL_SIZE) {
pacman.style.left = `${GRID_SIZE * CELL_SIZE}px`;
} else if (parseInt(pacman.style.left) > GRID_SIZE * CELL_SIZE) {
pacman.style.left = `${-CELL_SIZE}px`;
}
// Check for pellet collision
checkPelletCollision();
}
// Move ghosts
function moveGhosts() {
ghosts.forEach(ghost => {
let ghostX = Math.floor(parseInt(ghost.element.style.left) / CELL_SIZE);
let ghostY = Math.floor(parseInt(ghost.element.style.top) / CELL_SIZE);
// Check if ghost is centered in a cell to change direction
const ghostLeft = parseInt(ghost.element.style.left);
const ghostTop = parseInt(ghost.element.style.top);
const centeredX = ghostLeft % CELL_SIZE === 0;
const centeredY = ghostTop % CELL_SIZE === 0;
if (centeredX && centeredY) {
// Get possible directions (not opposite current direction)
const possibleDirections = ['up', 'down', 'left', 'right'].filter(dir => {
if (dir === 'up' && ghost.direction === 'down') return false;
if (dir === 'down' && ghost.direction === 'up') return false;
if (dir === 'left' && ghost.direction === 'right') return false;
if (dir === 'right' && ghost.direction === 'left') return false;
return canMove(ghostX, ghostY, dir);
});
// Choose random direction
if (possibleDirections.length > 0) {
ghost.direction = possibleDirections[Math.floor(Math.random() * possibleDirections.length)];
}
}
// Move ghost
switch (ghost.direction) {
case 'up':
ghost.element.style.top = `${parseInt(ghost.element.style.top) - ghost.speed}px`;
break;
case 'down':
ghost.element.style.top = `${parseInt(ghost.element.style.top) + ghost.speed}px`;
break;
case 'left':
ghost.element.style.left = `${parseInt(ghost.element.style.left) - ghost.speed}px`;
break;
case 'right':
ghost.element.style.left = `${parseInt(ghost.element.style.left) + ghost.speed}px`;
break;
}
// Handle tunnel/wrap-around
if (parseInt(ghost.element.style.left) < -CELL_SIZE) {
ghost.element.style.left = `${GRID_SIZE * CELL_SIZE}px`;
} else if (parseInt(ghost.element.style.left) > GRID_SIZE * CELL_SIZE) {
ghost.element.style.left = `${-CELL_SIZE}px`;
}
});
}
// Check if movement is possible
function canMove(x, y, dir) {
let newX = x;
let newY = y;
switch (dir) {
case 'up': newY--; break;
case 'down': newY++; break;
case 'left': newX--; break;
case 'right': newX++; break;
}
// Check boundaries
if (newX < 0 || newX >= GRID_SIZE || newY < 0 || newY >= mazeLayout.length) {
return false;
}
// Check walls
return mazeLayout[newY][newX] !== '1';
}
// Check pellet collisions
function checkPelletCollision() {
const pacmanX = Math.round(parseInt(pacman.style.left) / CELL_SIZE);
const pacmanY = Math.round(parseInt(pacman.style.top) / CELL_SIZE);
// Check regular pellets
pellets.forEach((pellet, index) => {
if (pellet.x === pacmanX && pellet.y === pacmanY) {
pellet.element.remove();
pellets.splice(index, 1);
score += 10;
pelletsRemaining--;
updateDisplay();
}
});
// Check power pellets
powerPellets.forEach((pellet, index) => {
if (pellet.x === pacmanX && pellet.y === pacmanY) {
pellet.element.remove();
powerPellets.splice(index, 1);
score += 50;
pelletsRemaining--;
scareGhosts();
updateDisplay();
}
});
}
// Scare ghosts (make them vulnerable)
function scareGhosts() {
ghostsScared = true;
ghosts.forEach(ghost => {
ghost.isScared = true;
ghost.element.classList.remove('bg-red-500', 'bg-pink-500', 'bg-cyan-400', 'bg-orange-400');
ghost.element.classList.add('scared');
ghost.speed = GHOST_SPEED * 0.7; // Slow down when scared
});
// Reset scared state after duration
clearTimeout(scaredTimer);
scaredTimer = setTimeout(() => {
ghostsScared = false;
ghosts.forEach(ghost => {
ghost.isScared = false;
ghost.element.classList.remove('scared');
ghost.element.classList.add('bg-red-500', 'bg-pink-500', 'bg-cyan-400', 'bg-orange-400');
ghost.speed = GHOST_SPEED;
});
}, SCARED_DURATION);
}
// Check collisions with ghosts
function checkCollisions() {
const pacmanX = Math.round(parseInt(pacman.style.left) / CELL_SIZE);
const pacmanY = Math.round(parseInt(pacman.style.top) / CELL_SIZE);
ghosts.forEach((ghost, index) => {
const ghostX = Math.round(parseInt(ghost.element.style.left) / CELL_SIZE);
const ghostY = Math.round(parseInt(ghost.element.style.top) / CELL_SIZE);
if (pacmanX === ghostX && pacmanY === ghostY) {
if (ghost.isScared) {
// Eat ghost
ghost.element.remove();
ghosts.splice(index, 1);
score += 200;
updateDisplay();
// Respawn ghost after delay
setTimeout(() => {
const ghost = document.createElement('div');
ghost.className = `ghost bg-red-500 absolute rounded-t-full`;
ghost.style.width = `${CELL_SIZE}px`;
ghost.style.height = `${CELL_SIZE}px`;
ghost.style.left = `${14 * CELL_SIZE}px`;
ghost.style.top = `${11 * CELL_SIZE}px`;
const eyes = document.createElement('div');
eyes.className = 'flex justify-between absolute w-full px-1 top-1';
eyes.innerHTML = `
<div class="w-2 h-2 bg-white rounded-full flex items-center justify-center">
<div class="w-1 h-1 bg-black rounded-full"></div>
</div>
<div class="w-2 h-2 bg-white rounded-full flex items-center justify-center">
<div class="w-1 h-1 bg-black rounded-full"></div>
</div>
`;
ghost.appendChild(eyes);
gameBoard.appendChild(ghost);
ghosts.push({
element: ghost,
x: 14,
y: 11,
direction: ['up', 'down', 'left', 'right'][Math.floor(Math.random() * 4)],
speed: GHOST_SPEED,
isScared: false
});
}, 3000);
} else {
// Lose life
lives--;
updateDisplay();
if (lives <= 0) {
gameOver(false);
} else {
// Reset positions
pacman.style.left = `${14 * CELL_SIZE}px`;
pacman.style.top = `${23 * CELL_SIZE}px`;
direction = 'right';
nextDirection = 'right';
pacman.style.transform = 'rotate(0deg)';
ghosts.forEach(g => {
g.element.style.left = `${14 * CELL_SIZE}px`;
g.element.style.top = `${11 * CELL_SIZE}px`;
});
// Pause briefly
moving = false;
setTimeout(() => {
moving = true;
}, 1000);
}
}
}
});
}
// Game over
function gameOver(won) {
gameRunning = false;
moving = false;
if (won) {
alert(`Congratulations! You won with a score of ${score}!`);
} else {
alert(`Game Over! Your score was ${score}.`);
}
}
// Event listeners
startBtn.addEventListener('click', startGame);
document.addEventListener('keydown', (e) => {
if (!gameRunning) return;
switch (e.key) {
case 'ArrowUp':
nextDirection = 'up';
break;
case 'ArrowDown':
nextDirection = 'down';
break;
case 'ArrowLeft':
nextDirection = 'left';
break;
case 'ArrowRight':
nextDirection = 'right';
break;
}
// Start moving if not already
if (!moving) {
direction = nextDirection;
moving = true;
}
});
});
</script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=Engelko/pacman" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>