Вот пример кода HTML/CSS/JS в одном файле для пародии на игру Pac-Man, где призраки — квадратики, а Pac-Man — кружок:
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<title>Пародия Pac-Man</title>
<style>
canvas {
background-color: black;
display: block;
margin: 0 auto;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="500" height="500"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const context = canvas.getContext('2d');
const pacman = {
x: 250,
y: 250,
radius: 15,
color: 'yellow',
speed: 3,
dx: 0,
dy: 0
};
const ghosts = [];
const ghostCount = 4;
function createGhosts() {
for (let i = 0; i < ghostCount; i++) {
ghosts.push({
x: Math.random() * (canvas.width - 30) + 15,
y: Math.random() * (canvas.height - 30) + 15,
size: 20,
color: 'red',
speed: 2,
dx: Math.random() < 0.5 ? -1 : 1,
dy: Math.random() < 0.5 ? -1 : 1
});
}
}
function drawPacman() {
context.beginPath();
context.arc(pacman.x, pacman.y, pacman.radius, 0, Math.PI * 2);
context.fillStyle = pacman.color;
context.fill();
context.closePath();
}
function drawGhosts() {
ghosts.forEach(ghost => {
context.fillStyle = ghost.color;
context.fillRect(ghost.x - ghost.size / 2, ghost.y - ghost.size / 2, ghost.size, ghost.size);
});
}
function movePacman() {
pacman.x += pacman.dx * pacman.speed;
pacman.y += pacman.dy * pacman.speed;
// Ограничение движения Pac-Man по краям канваса
if (pacman.x - pacman.radius < 0) pacman.x = pacman.radius;
if (pacman.x + pacman.radius > canvas.width) pacman.x = canvas.width - pacman.radius;
if (pacman.y - pacman.radius < 0) pacman.y = pacman.radius;
if (pacman.y + pacman.radius > canvas.height) pacman.y = canvas.height - pacman.radius;
}
function moveGhosts() {
ghosts.forEach(ghost => {
ghost.x += ghost.dx * ghost.speed;
ghost.y += ghost.dy * ghost.speed;
// Отражение призраков от стен
if (ghost.x - ghost.size / 2 < 0 || ghost.x + ghost.size / 2 > canvas.width) {
ghost.dx *= -1;
}
if (ghost.y - ghost.size / 2 < 0 || ghost.y + ghost.size / 2 > canvas.height) {
ghost.dy *= -1;
}
});
}
function detectCollisions() {
ghosts.forEach(ghost => {
const dx = pacman.x - ghost.x;
const dy = pacman.y - ghost.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < pacman.radius + ghost.size / 2) {
alert('Игра окончена!');
document.location.reload();
}
});
}
function updateGame() {
context.clearRect(0, 0, canvas.width, canvas.height);
movePacman();
moveGhosts();
drawPacman();
drawGhosts();
detectCollisions();
requestAnimationFrame(updateGame);
}
document.addEventListener('keydown', event => {
if (event.key === 'ArrowRight') {
pacman.dx = 1;
pacman.dy = 0;
}
if (event.key === 'ArrowLeft') {
pacman.dx = -1;
pacman.dy = 0;
}
if (event.key === 'ArrowUp') {
pacman.dx = 0;
pacman.dy = -1;
}
if (event.key === 'ArrowDown') {
pacman.dx = 0;
pacman.dy = 1;
}
});
document.addEventListener('keyup', event => {
if (
event.key === 'ArrowRight' ||
event.key === 'ArrowLeft' ||
event.key === 'ArrowUp' ||
event.key === 'ArrowDown'
) {
pacman.dx = 0;
pacman.dy = 0;
}
});
createGhosts();
updateGame();
</script>
</body>
</html>
Сохраните этот код в файл с расширением .html и откройте его в браузере. Вы сможете управлять Pac-Man с помощью клавиш со стрелками, избегая столкновений с квадратными призраками.