html
<!DOCTYPE html>
<html>
<head>
<title>迷宫游戏</title>
<style>
body {
margin: 0;
background: linear-gradient(to bottom, #222, #000);
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
overflow: hidden;
color: white;
font-family: Arial, sans-serif;
}
canvas {
border: 3px solid #4CAF50;
background: #111;
box-shadow: 0 0 20px #4CAF50;
}
.overlay {
position: absolute;
padding: 10px 20px;
background: rgba(0,0,0,0.7);
border: 2px solid #4CAF50;
border-radius: 5px;
display: none;
}
</style>
</head>
<body>
<canvas id="mazeCanvas" width="600" height="600"></canvas>
<div id="winMessage" class="overlay">恭喜!用 ← → ↑ ↓ 键重新开始</div>
<script>
const canvas = document.getElementById('mazeCanvas');
const ctx = canvas.getContext('2d');
const winMessage = document.getElementById('winMessage');
// 迷宫配置
const mazeSize = {
rows: 20,
cols: 20,
cellSize: 30,
wallThickness: 4
};
// 玩家初始位置
let player = {
x: 0,
y: 0,
color: '#FFD700'
};
// 终点位置
let endPoint = {
x: mazeSize.cols - 1,
y: mazeSize.rows - 1
};
// 迷宫数据
let maze = [];
let visited = [];
// 方向键映射
const directions = {
37: {x: -1, y: 0}, // 左
38: {x: 0, y: -1}, // 上
39: {x: 1, y: 0}, // 右
40: {x: 0, y: 1} // 下
};
// 生成迷宫
function generateMaze() {
// 初始化迷宫数据
maze = [];
visited = [];
for (let y = 0; y < mazeSize.rows; y++) {
maze[y] = [];
visited[y] = [];
for (let x = 0; x < mazeSize.cols; x++) {
maze[y][x] = {left: true, top: true, right: true, bottom: true, visited: false};
visited[y][x] = false;
}
}
// DFS生成迷宫
function dfs(x, y) {
visited[y][x] = true;
const dirs = Object.values(directions).sort(() => Math.random() - 0.5);
for (const dir of dirs) {
const nx = x + dir.x;
const ny = y + dir.y;
if (nx >= 0 && ny >= 0 && nx < mazeSize.cols && ny < mazeSize.rows && !visited[ny][nx]) {
// 移除墙壁
if (dir.x === 1) maze[y][x].right = false;
if (dir.x === -1) maze[y][x].left = false;
if (dir.y === 1) maze[y][x].bottom = false;
if (dir.y === -1) maze[y][x].top = false;
if (dir.x === 1) maze[ny][nx].left = false;
if (dir.x === -1) maze[ny][nx].right = false;
if (dir.y === 1) maze[ny][nx].top = false;
if (dir.y === -1) maze[ny][nx].bottom = false;
dfs(nx, ny);
}
}
}
dfs(0, 0);
player.x = 0;
player.y = 0;
winMessage.style.display = 'none';
drawMaze();
}
// 绘制迷宫
function drawMaze() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// 绘制墙壁
ctx.lineWidth = mazeSize.wallThickness;
ctx.strokeStyle = '#4CAF50';
for (let y = 0; y < mazeSize.rows; y++) {
for (let x = 0; x < mazeSize.cols; x++) {
const cell = maze[y][x];
const px = x * mazeSize.cellSize;
const py = y * mazeSize.cellSize;
ctx.beginPath();
// 上墙
if (cell.top) {
ctx.moveTo(px, py);
ctx.lineTo(px + mazeSize.cellSize, py);
}
// 右墙
if (cell.right) {
ctx.moveTo(px + mazeSize.cellSize, py);
ctx.lineTo(px + mazeSize.cellSize, py + mazeSize.cellSize);
}
// 下墙
if (cell.bottom) {
ctx.moveTo(px + mazeSize.cellSize, py + mazeSize.cellSize);
ctx.lineTo(px, py + mazeSize.cellSize);
}
// 左墙
if (cell.left) {
ctx.moveTo(px, py + mazeSize.cellSize);
ctx.lineTo(px, py);
}
ctx.stroke();
}
}
// 绘制起点和终点
ctx.fillStyle = '#2196F3';
ctx.fillRect(0, 0, mazeSize.cellSize/2, mazeSize.cellSize/2);
ctx.fillStyle = '#F44336';
ctx.fillRect(
(mazeSize.cols-1)*mazeSize.cellSize + mazeSize.cellSize/4,
(mazeSize.rows-1)*mazeSize.cellSize + mazeSize.cellSize/4,
mazeSize.cellSize/2,
mazeSize.cellSize/2
);
drawPlayer();
}
// 绘制玩家
function drawPlayer() {
ctx.beginPath();
ctx.arc(
player.x * mazeSize.cellSize + mazeSize.cellSize/2,
player.y * mazeSize.cellSize + mazeSize.cellSize/2,
mazeSize.cellSize/3,
0,
Math.PI*2
);
ctx.fillStyle = player.color;
ctx.fill();
}
// 移动玩家
function movePlayer(dx, dy) {
const newX = player.x + dx;
const newY = player.y + dy;
if (newX >= 0 && newX < mazeSize.cols && newY >= 0 && newY < mazeSize.rows) {
// 检查墙壁碰撞
const currentCell = maze[player.y][player.x];
let canMove = false;
if (dx === 1 && !currentCell.right) canMove = true;
if (dx === -1 && !currentCell.left) canMove = true;
if (dy === 1 && !currentCell.bottom) canMove = true;
if (dy === -1 && !currentCell.top) canMove = true;
if (canMove) {
player.x = newX;
player.y = newY;
drawMaze();
// 检查胜利
if (player.x === endPoint.x && player.y === endPoint.y) {
showWinAnimation();
}
}
}
}
// 显示胜利动画
function showWinAnimation() {
winMessage.style.display = 'block';
let scale = 1;
let direction = 1;
function pulse() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawMaze();
// 脉冲效果
scale += 0.02 * direction;
if (scale > 1.1 || scale < 0.9) direction *= -1;
ctx.save();
ctx.translate(canvas.width/2, canvas.height/2);
ctx.scale(scale, scale);
ctx.translate(-canvas.width/2, -canvas.height/2);
drawPlayer();
ctx.restore();
requestAnimationFrame(pulse);
}
pulse();
setTimeout(() => {
cancelAnimationFrame(pulse);
drawMaze();
}, 2000);
}
// 键盘控制
document.addEventListener('keydown', (e) => {
if (directions[e.keyCode]) {
e.preventDefault();
movePlayer(directions[e.keyCode].x, directions[e.keyCode].y);
} else if ((e.keyCode >= 37 && e.keyCode <= 40) && winMessage.style.display === 'block') {
generateMaze();
}
});
// 初始化游戏
generateMaze();
</script>
</body>
</html>