🎮ArcadeLab

巴黎防区图 · 1642

by EpicCoder88
461 lines11.1 KB
▶ Play
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">
<title>巴黎防区图 · 1642</title>
<style>
  * { box-sizing: border-box; margin: 0; padding: 0; }
  html, body { width: 100%; height: 100%; overflow: hidden; background: #0d0a07; }
  canvas { display: block; touch-action: none; }
  #hud {
    position: fixed; left: 12px; top: 12px;
    font-family: Georgia, "Noto Serif SC", serif;
    color: #c9b489; font-size: 12px; line-height: 1.7;
    background: rgba(20,16,12,.78);
    border: 1px solid #5a4529; border-radius: 6px;
    padding: 8px 11px; pointer-events: none;
    letter-spacing: .5px;
  }
  #hud b { color: #f0c674; font-weight: normal; }
  #hud .tip { color: #8f7a55; font-size: 11px; }
  #btnReset {
    position: fixed; right: 12px; top: 12px;
    font-family: Georgia, "Noto Serif SC", serif; font-size: 13px;
    background: rgba(40,30,20,.9); color: #e6d3a8;
    border: 1px solid #5a4529; border-radius: 6px;
    padding: 8px 12px; cursor: pointer;
  }
  #btnReset:active { background: #5b3b22; }
</style>
</head>
<body>
<canvas id="c"></canvas>
<div id="hud">
  巴黎防区图 · 1642<br>
  缩放 <b id="zoomTxt">1.0×</b> · 防区 <b id="cellTxt">—</b><br>
  <span class="tip">双指缩放 / 拖动平移</span>
</div>
<button id="btnReset">重置视野</button>

<script>
/* ========== 基础 ========== */
const WORLD_W = 200;
const WORLD_H = 280;

const canvas = document.getElementById('c');
const ctx = canvas.getContext('2d');
let W = 0, H = 0, dpr = 1;

const cam = { x: WORLD_W / 2, y: WORLD_H / 2, scale: 2 };
let minScale = 0.5, maxScale = 40;
let selectedCell = null;

/* ========== 布局数据(世界坐标 200×280) ========== */

// 外墙轮廓(大致包住巴黎与近郊)
const OUTER_WALL = [
  [12, 22], [176, 18], [190, 110], [184, 186],
  [168, 258], [32, 262], [10, 186], [6, 96]
];

// 老城核心:右岸、左岸
const INNER_RIGHT = [[36, 56], [150, 52], [162, 138], [40, 142]];
const INNER_LEFT  = [[48, 182], [142, 176], [150, 228], [54, 234]];

// 四座主要城门
const GATES = [
  { x: 100,  y: 20,    name: '圣但尼门' },
  { x: 140,  y: 18.5,  name: '圣马丁门' },
  { x: 188.4, y: 130,  name: '圣安托万门' },
  { x: 145,  y: 258.7, name: '圣贝尔纳门' }
];

// 地标
const LANDMARKS = [
  { x: 112, y: 166, name: '圣母院' },
  { x: 58,  y: 100, name: '卢浮宫' },
  { x: 172, y: 126, name: '巴士底' },
  { x: 78,  y: 202, name: '索邦' }
];

/* ========== 工具 ========== */
const clamp = (v, a, b) => Math.max(a, Math.min(b, v));

function pathPolygon(pts) {
  ctx.beginPath();
  ctx.moveTo(pts[0][0], pts[0][1]);
  for (let i = 1; i < pts.length; i++) ctx.lineTo(pts[i][0], pts[i][1]);
  ctx.closePath();
}

function screenToWorld(sx, sy) {
  return {
    x: (sx - W / 2) / cam.scale + cam.x,
    y: (sy - H / 2) / cam.scale + cam.y
  };
}

function clampCam() {
  const hw = W / 2 / cam.scale;
  const hh = H / 2 / cam.scale;
  cam.x = WORLD_W <= hw * 2 ? WORLD_W / 2 : clamp(cam.x, hw, WORLD_W - hw);
  cam.y = WORLD_H <= hh * 2 ? WORLD_H / 2 : clamp(cam.y, hh, WORLD_H - hh);
}

/* ========== 绘制 ========== */
function draw() {
  ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
  ctx.fillStyle = '#0d0a07';
  ctx.fillRect(0, 0, W, H);

  ctx.save();
  ctx.translate(W / 2, H / 2);
  ctx.scale(cam.scale, cam.scale);
  ctx.translate(-cam.x, -cam.y);

  drawWorld();

  ctx.restore();
}

function drawWorld() {
  // 城外土地
  ctx.fillStyle = '#c2b68c';
  ctx.fillRect(0, 0, WORLD_W, WORLD_H);

  // 郊外农田纹理
  drawFields();

  // 城墙以内
  pathPolygon(OUTER_WALL);
  ctx.fillStyle = '#ded1a8';
  ctx.fill();

  // 塞纳河
  drawRiver();

  // 西岱岛
  drawIsland();

  // 老城核心区(略深)
  ctx.fillStyle = 'rgba(176, 152, 100, 0.28)';
  pathPolygon(INNER_RIGHT); ctx.fill();
  pathPolygon(INNER_LEFT);  ctx.fill();

  // 城墙
  drawWalls();

  // 城门
  drawGates();

  // 地标
  drawLandmarks();

  // 网格
  drawGrid();

  // 选中格
  drawSelection();
}

function drawFields() {
  ctx.save();
  ctx.strokeStyle = 'rgba(140, 128, 80, 0.28)';
  ctx.lineWidth = 0.5;
  for (let y = 6; y < WORLD_H; y += 8) {
    for (let x = 4; x < WORLD_W; x += 12) {
      const off = (x + y) % 3;
      ctx.beginPath();
      ctx.moveTo(x + off, y);
      ctx.lineTo(x + off + 7, y);
      ctx.stroke();
    }
  }
  ctx.restore();
}

function drawRiver() {
  ctx.save();
  ctx.lineCap = 'round';

  const trace = () => {
    ctx.beginPath();
    ctx.moveTo(-8, 184);
    ctx.bezierCurveTo(45, 180, 75, 168, 115, 166);
    ctx.bezierCurveTo(155, 164, 175, 156, 208, 152);
  };

  trace();
  ctx.strokeStyle = 'rgba(90, 110, 110, 0.35)';
  ctx.lineWidth = 19;
  ctx.stroke();

  trace();
  ctx.strokeStyle = '#7d9db0';
  ctx.lineWidth = 16;
  ctx.stroke();

  ctx.restore();
}

function drawIsland() {
  ctx.save();
  ctx.beginPath();
  ctx.ellipse(110, 166, 20, 5.2, -0.03, 0, Math.PI * 2);
  ctx.fillStyle = '#d6c79c';
  ctx.fill();
  ctx.strokeStyle = 'rgba(90, 70, 40, 0.5)';
  ctx.lineWidth = 0.6;
  ctx.stroke();
  ctx.restore();
}

function drawWalls() {
  ctx.save();
  ctx.lineJoin = 'round';

  // 外墙
  ctx.strokeStyle = '#6b4a2a';
  ctx.lineWidth = 2.2;
  pathPolygon(OUTER_WALL);
  ctx.stroke();

  // 内墙(老城墙,虚线)
  ctx.strokeStyle = '#5a3d22';
  ctx.lineWidth = 1.4;
  ctx.setLineDash([2, 1.5]);
  pathPolygon(INNER_RIGHT); ctx.stroke();
  pathPolygon(INNER_LEFT);  ctx.stroke();

  ctx.restore();
}

function drawGates() {
  ctx.save();
  for (const g of GATES) {
    ctx.fillStyle = '#3b2a18';
    ctx.fillRect(g.x - 1.6, g.y - 1.6, 3.2, 3.2);
    ctx.strokeStyle = '#d9b36c';
    ctx.lineWidth = 0.5;
    ctx.strokeRect(g.x - 1.6, g.y - 1.6, 3.2, 3.2);
  }
  ctx.restore();
}

function drawLandmarks() {
  ctx.save();
  ctx.textAlign = 'center';
  ctx.textBaseline = 'top';

  const showLandmarkText = cam.scale > 5;

  for (const m of LANDMARKS) {
    ctx.fillStyle = 'rgba(90, 60, 30, 0.75)';
    ctx.fillRect(m.x - 1.2, m.y - 1.2, 2.4, 2.4);

    if (showLandmarkText) {
      ctx.fillStyle = '#4a3420';
      ctx.font = '4px "Noto Serif SC", serif';
      ctx.fillText(m.name, m.x, m.y + 2.6);
    }
  }

  // 城门名
  if (cam.scale > 6) {
    ctx.fillStyle = '#6b3b1a';
    ctx.font = '3.4px "Noto Serif SC", serif';
    ctx.textBaseline = 'bottom';
    for (const g of GATES) {
      ctx.fillText(g.name, g.x, g.y - 2.2);
    }
  }

  ctx.restore();
}

function drawGrid() {
  const s = cam.scale;
  let step = 1;
  if (s < 4) step = 10;
  else if (s < 10) step = 5;

  ctx.save();
  ctx.strokeStyle = 'rgba(70, 55, 35, 0.42)';
  ctx.lineWidth = 1 / s;
  ctx.setLineDash([1.5 / s, 1.5 / s]);

  ctx.beginPath();
  for (let x = 0; x <= WORLD_W; x += step) {
    ctx.moveTo(x, 0);
    ctx.lineTo(x, WORLD_H);
  }
  for (let y = 0; y <= WORLD_H; y += step) {
    ctx.moveTo(0, y);
    ctx.lineTo(WORLD_W, y);
  }
  ctx.stroke();
  ctx.restore();
}

function drawSelection() {
  if (!selectedCell) return;
  ctx.save();
  ctx.fillStyle = 'rgba(180, 110, 30, 0.35)';
  ctx.fillRect(selectedCell.x, selectedCell.y, 1, 1);
  ctx.strokeStyle = '#8a3a10';
  ctx.lineWidth = 2 / cam.scale;
  ctx.strokeRect(selectedCell.x, selectedCell.y, 1, 1);
  ctx.restore();
}

/* ========== 缩放 / 平移 ========== */
const pointers = new Map();
let dragStart = null;
let pinch = null;
let downPos = null;

function getPos(e) {
  const r = canvas.getBoundingClientRect();
  return { x: e.clientX - r.left, y: e.clientY - r.top };
}

canvas.addEventListener('pointerdown', e => {
  canvas.setPointerCapture(e.pointerId);
  const p = getPos(e);
  pointers.set(e.pointerId, p);

  if (pointers.size === 1) {
    dragStart = { x: p.x, y: p.y, camX: cam.x, camY: cam.y };
    downPos = { x: p.x, y: p.y, t: Date.now() };
  } else if (pointers.size === 2) {
    const [a, b] = [...pointers.values()];
    const dist = Math.hypot(b.x - a.x, b.y - a.y);
    const mid = { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };
    pinch = {
      dist,
      scale: cam.scale,
      worldMid: screenToWorld(mid.x, mid.y)
    };
    dragStart = null;
  }
});

canvas.addEventListener('pointermove', e => {
  if (!pointers.has(e.pointerId)) return;
  const p = getPos(e);
  pointers.set(e.pointerId, p);

  if (pointers.size === 1 && dragStart) {
    cam.x = dragStart.camX - (p.x - dragStart.x) / cam.scale;
    cam.y = dragStart.camY - (p.y - dragStart.y) / cam.scale;
    clampCam();
    draw();
  } else if (pointers.size === 2 && pinch) {
    const [a, b] = [...pointers.values()];
    const dist = Math.hypot(b.x - a.x, b.y - a.y);
    const mid = { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };

    cam.scale = clamp(pinch.scale * (dist / pinch.dist), minScale, maxScale);
    cam.x = pinch.worldMid.x - (mid.x - W / 2) / cam.scale;
    cam.y = pinch.worldMid.y - (mid.y - H / 2) / cam.scale;
    clampCam();
    updateHud();
    draw();
  }
});

function endPointer(e) {
  if (!pointers.has(e.pointerId)) return;
  pointers.delete(e.pointerId);

  if (pointers.size === 0) {
    if (downPos && e.type === 'pointerup') {
      const p = getPos(e);
      const moved = Math.hypot(p.x - downPos.x, p.y - downPos.y);
      if (moved < 8 && Date.now() - downPos.t < 400) handleClick(p);
    }
    dragStart = null;
    pinch = null;
    downPos = null;
  } else if (pointers.size === 1) {
    const p = [...pointers.values()][0];
    dragStart = { x: p.x, y: p.y, camX: cam.x, camY: cam.y };
    pinch = null;
  }
}

canvas.addEventListener('pointerup', endPointer);
canvas.addEventListener('pointercancel', endPointer);

canvas.addEventListener('wheel', e => {
  e.preventDefault();
  const p = getPos(e);
  const w = screenToWorld(p.x, p.y);
  const factor = e.deltaY < 0 ? 1.15 : 1 / 1.15;

  cam.scale = clamp(cam.scale * factor, minScale, maxScale);
  cam.x = w.x - (p.x - W / 2) / cam.scale;
  cam.y = w.y - (p.y - H / 2) / cam.scale;
  clampCam();
  updateHud();
  draw();
}, { passive: false });

function handleClick(p) {
  const w = screenToWorld(p.x, p.y);
  const cx = Math.floor(w.x);
  const cy = Math.floor(w.y);

  if (cx < 0 || cx >= WORLD_W || cy < 0 || cy >= WORLD_H) {
    selectedCell = null;
    document.getElementById('cellTxt').textContent = '—';
    draw();
    return;
  }

  selectedCell = { x: cx, y: cy };
  document.getElementById('cellTxt').textContent = `${cx + 1}, ${cy + 1}`;
  draw();
}

/* ========== 视图控制 ========== */
function updateHud() {
  document.getElementById('zoomTxt').textContent = cam.scale.toFixed(1) + '×';
}

function resize() {
  dpr = window.devicePixelRatio || 1;
  W = window.innerWidth;
  H = window.innerHeight;
  canvas.width = Math.round(W * dpr);
  canvas.height = Math.round(H * dpr);
  canvas.style.width = W + 'px';
  canvas.style.height = H + 'px';
}

function fitView() {
  const s = Math.min(W / WORLD_W, H / WORLD_H) * 0.9;
  minScale = s * 0.7;
  maxScale = 40;
  cam.scale = s;
  cam.x = WORLD_W / 2;
  cam.y = WORLD_H / 2;
  updateHud();
  draw();
}

document.getElementById('btnReset').addEventListener('click', fitView);

window.addEventListener('resize', () => {
  resize();
  clampCam();
  draw();
});

/* ========== 启动 ========== */
resize();
fitView();
</script>
</body>
</html>

Game Source: 巴黎防区图 · 1642

Creator: EpicCoder88

Libraries: none

Complexity: complex (461 lines, 11.1 KB)

The full source code is displayed above on this page.

Remix Instructions

To remix this game, copy the source code above and modify it. Add a ARCADELAB header at the top with "remix_of: 1642-epiccoder88-mwfyrzwc" to link back to the original. Then publish at arcadelab.ai/publish.