管道拼图游戏
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
canvas.width = 600;
canvas.height = 600;
// 游戏配置
const GRID_SIZE = 5; // 5x5网格
const TILE_SIZE = 100;
const PIPE_WIDTH = 20;
// 管道类型
const PIPE_TYPES = {
STRAIGHT_H: 0, // 水平直管
STRAIGHT_V: 1, // 垂直直管
CORNER_NE: 2, // 东北拐角
CORNER_NW: 3, // 西北拐角
CORNER_SW: 4, // 西南拐角
CORNER_SE: 5, // 东南拐角
T_N: 6, // 北三通
T_E: 7, // 东三通
T_S: 8, // 南三通
T_W: 9, // 西三通
CROSS: 10 // 十字四通
};
// 特殊元素
const SPECIAL = {
NONE: 0,
TREE: 1,
SOURCE: 2
};
// 方向
const DIRS = {
N: 0,
E: 1,
S: 2,
W: 3
};
// 创建游戏板
let gameBoard = [];
let waterFlow = [];
let solved = false;
// 初始化游戏
function initGame() {
gameBoard = [];
waterFlow = Array(GRID_SIZE).fill().map(() => Array(GRID_SIZE).fill(false));
solved = false;
// 创建随机游戏板
for (let y = 0; y < GRID_SIZE; y++) {
let row = [];
for (let x = 0; x < GRID_SIZE; x++) {
row.push({
type: Math.floor(Math.random() * 11),
rotation: Math.floor(Math.random() * 4),
special: SPECIAL.NONE
});
}
gameBoard.push(row);
}
// 添加水源和树
const sourceX = Math.floor(Math.random() * GRID_SIZE);
const sourceY = Math.floor(Math.random() * GRID_SIZE);
gameBoard[sourceY][sourceX].special = SPECIAL.SOURCE;
// 添加3-5棵树
const treeCount = 3 + Math.floor(Math.random() * 3);
for (let i = 0; i < treeCount; i++) {
let x, y;
do {
x = Math.floor(Math.random() * GRID_SIZE);
y = Math.floor(Math.random() * GRID_SIZE);
} while (gameBoard[y][x].special !== SPECIAL.NONE);
gameBoard[y][x].special = SPECIAL.TREE;
// 确保树和水源有管道连接
if (gameBoard[y][x].type === PIPE_TYPES.STRAIGHT_H ||
gameBoard[y][x].type === PIPE_TYPES.STRAIGHT_V) {
gameBoard[y][x].type = PIPE_TYPES.CROSS;
} else if (gameBoard[y][x].type >= PIPE_TYPES.CORNER_NE &&
gameBoard[y][x].type <= PIPE_TYPES.CORNER_SE) {
gameBoard[y][x].type = PIPE_TYPES.T_N + Math.floor(Math.random() * 4);
}
}
calculateWaterFlow();
drawGame();
}
// 检查管道是否有特定方向的出口
function hasPipeExit(tile, dir) {
const type = tile.type;
const rot = tile.rotation;
// 根据管道类型和旋转角度判断是否有出口
switch (type) {
case PIPE_TYPES.STRAIGHT_H:
return (dir + rot) % 4 === DIRS.E || (dir + rot) % 4 === DIRS.W;
case PIPE_TYPES.STRAIGHT_V:
return (dir + rot) % 4 === DIRS.N || (dir + rot) % 4 === DIRS.S;
case PIPE_TYPES.CORNER_NE:
return (dir + rot) % 4 === DIRS.N || (dir + rot) % 4 === DIRS.E;
case PIPE_TYPES.CORNER_NW:
return (dir + rot) % 4 === DIRS.N || (dir + rot) % 4 === DIRS.W;
case PIPE_TYPES.CORNER_SW:
return (dir + rot) % 4 === DIRS.S || (dir + rot) % 4 === DIRS.W;
case PIPE_TYPES.CORNER_SE:
return (dir + rot) % 4 === DIRS.S || (dir + rot) % 4 === DIRS.E;
case PIPE_TYPES.T_N:
return (dir + rot) % 4 === DIRS.N || (dir + rot) % 4 === DIRS.E || (dir + rot) % 4 === DIRS.W;
case PIPE_TYPES.T_E:
return (dir + rot) % 4 === DIRS.N || (dir + rot) % 4 === DIRS.E || (dir + rot) % 4 === DIRS.S;
case PIPE_TYPES.T_S:
return (dir + rot) % 4 === DIRS.E || (dir + rot) % 4 === DIRS.S || (dir + rot) % 4 === DIRS.W;
case PIPE_TYPES.T_W:
return (dir + rot) % 4 === DIRS.N || (dir + rot) % 4 === DIRS.S || (dir + rot) % 4 === DIRS.W;
case PIPE_TYPES.CROSS:
return true;
default:
return false;
}
}
// 计算水流
function calculateWaterFlow() {
// 重置水流
waterFlow = Array(GRID_SIZE).fill().map(() => Array(GRID_SIZE).fill(false));
// 找到水源
let sourceX = -1, sourceY = -1;
for (let y = 0; y < GRID_SIZE; y++) {
for (let x = 0; x < GRID_SIZE; x++) {
if (gameBoard[y][x].special === SPECIAL.SOURCE) {
sourceX = x;
sourceY = y;
break;
}
}
if (sourceX !== -1) break;
}
// 从水源开始流动
if (sourceX !== -1) {
waterFlow[sourceY][sourceX] = true;
spreadWater(sourceX, sourceY);
}
// 检查是否所有树都有水
let allTreesWatered = true;
for (let y = 0; y < GRID_SIZE; y++) {
for (let x = 0; x < GRID_SIZE; x++) {
if (gameBoard[y][x].special === SPECIAL.TREE && !waterFlow[y][x]) {
allTreesWatered = false;
break;
}
}
if (!allTreesWatered) break;
}
solved = allTreesWatered;
}
// 水流扩散
function spreadWater(x, y) {
const tile = gameBoard[y][x];
// 检查四个方向
const directions = [
{dx: 0, dy: -1, dir: DIRS.N}, // 北
{dx: 1, dy: 0, dir: DIRS.E}, // 东
{dx: 0, dy: 1, dir: DIRS.S}, // 南
{dx: -1, dy: 0, dir: DIRS.W} // 西
];
for (const {dx, dy, dir} of directions) {
const nx = x + dx;
const ny = y + dy;
// 检查边界
if (nx < 0 || nx >= GRID_SIZE || ny < 0 || ny >= GRID_SIZE) continue;
// 已经有水的格子跳过
if (waterFlow[ny][nx]) continue;
// 检查当前格子是否有向该方向的出口
if (!hasPipeExit(tile, dir)) continue;
// 检查相邻格子是否有向当前格子的入口
const oppDir = (dir + 2) % 4; // 相反方向
if (!hasPipeExit(gameBoard[ny][nx], oppDir)) continue;
// 水流可以通过
waterFlow[ny][nx] = true;
spreadWater(nx, ny);
}
}
// 绘制游戏
function drawGame() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// 绘制背景
ctx.fillStyle = '#f0f0f0';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 绘制网格
for (let y = 0; y < GRID_SIZE; y++) {
for (let x = 0; x < GRID_SIZE; x++) {
const tile = gameBoard[y][x];
const centerX = x * TILE_SIZE + TILE_SIZE / 2;
const centerY = y * TILE_SIZE + TILE_SIZE / 2;
// 绘制方块背景
ctx.fillStyle = waterFlow[y][x] ? '#d0f0d0' : '#e0e0e0';
ctx.fillRect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
ctx.strokeStyle = '#888';
ctx.strokeRect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
// 保存当前状态
ctx.save();
// 移动到方块中心并旋转
ctx.translate(centerX, centerY);
ctx.rotate(tile.rotation * Math.PI / 2);
// 绘制管道
ctx.lineWidth = PIPE_WIDTH;
ctx.lineCap = 'round';
ctx.strokeStyle = waterFlow[y][x] ? '#4080ff' : '#888';
ctx.beginPath();
switch (tile.type) {
case PIPE_TYPES.STRAIGHT_H:
ctx.moveTo(-TILE_SIZE/2, 0);
ctx.lineTo(TILE_SIZE/2, 0);
break;
case PIPE_TYPES.STRAIGHT_V:
ctx.moveTo(0, -TILE_SIZE/2);
ctx.lineTo(0, TILE_SIZE/2);
break;
case PIPE_TYPES.CORNER_NE:
ctx.moveTo(0, -TILE_SIZE/2);
ctx.quadraticCurveTo(0, 0, TILE_SIZE/2, 0);
break;
case PIPE_TYPES.CORNER_NW:
ctx.moveTo(0, -TILE_SIZE/2);
ctx.quadraticCurveTo(0, 0, -TILE_SIZE/2, 0);
break;
case PIPE_TYPES.CORNER_SW:
ctx.moveTo(0, TILE_SIZE/2);
ctx.quadraticCurveTo(0, 0, -TILE_SIZE/2, 0);
break;
case PIPE_TYPES.CORNER_SE:
ctx.moveTo(0, TILE_SIZE/2);
ctx.quadraticCurveTo(0, 0, TILE_SIZE/2, 0);
break;
case PIPE_TYPES.T_N:
ctx.moveTo(-TILE_SIZE/2, 0);
ctx.lineTo(TILE_SIZE/2, 0);
ctx.moveTo(0, 0);
ctx.lineTo(0, -TILE_SIZE/2);
break;
case PIPE_TYPES.T_E:
ctx.moveTo(0, -TILE_SIZE/2);
ctx.lineTo(0, TILE_SIZE/2);
ctx.moveTo(0, 0);
ctx.lineTo(TILE_SIZE/2, 0);
break;
case PIPE_TYPES.T_S:
ctx.moveTo(-TILE_SIZE/2, 0);
ctx.lineTo(TILE_SIZE/2, 0);
ctx.moveTo(0, 0);
ctx.lineTo(0, TILE_SIZE/2);
break;
case PIPE_TYPES.T_W:
ctx.moveTo(0, -TILE_SIZE/2);
ctx.lineTo(0, TILE_SIZE/2);
ctx.moveTo(0, 0);
ctx.lineTo(-TILE_SIZE/2, 0);
break;
case PIPE_TYPES.CROSS:
ctx.moveTo(-TILE_SIZE/2, 0);
ctx.lineTo(TILE_SIZE/2, 0);
ctx.moveTo(0, -TILE_SIZE/2);
ctx.lineTo(0, TILE_SIZE/2);
break;
}
ctx.stroke();
// 绘制特殊元素
if (tile.special === SPECIAL.TREE) {
// 绘制树
ctx.fillStyle = '#228B22';
ctx.beginPath();
ctx.moveTo(0, -20);
ctx.lineTo(15, 0);
ctx.lineTo(7, 0);
ctx.lineTo(15, 15);
ctx.lineTo(-15, 15);
ctx.lineTo(-7, 0);
ctx.lineTo(-15, 0);
ctx.closePath();
ctx.fill();
// 树干
ctx.fillStyle = '#8B4513';
ctx.fillRect(-5, 15, 10, 15);
} else if (tile.special === SPECIAL.SOURCE) {
// 绘制水源
ctx.fillStyle = waterFlow[y][x] ? '#4080ff' : '#888';
ctx.beginPath();
ctx.arc(0, 0, 15, 0, Math.PI * 2);
ctx.fill();
// 水波纹
ctx.strokeStyle = '#fff';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.arc(0, 0, 8, 0, Math.PI);
ctx.stroke();
}
// 恢复状态
ctx.restore();
}
}
// 如果游戏完成,显示胜利消息
if (solved) {
ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx