🎮ArcadeLab

黑死纪·1642

by EpicCoder88
374 lines9.7 KB
▶ Play
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>黑死纪·1642</title>
<style>
  * { box-sizing: border-box; }
  body {
    margin: 0;
    background: radial-gradient(circle at top, #2b2118, #120d0a 70%);
    color: #e8d8b0;
    font-family: "Georgia", "Noto Serif SC", serif;
    min-height: 100vh;
  }
  #game {
    max-width: 980px;
    margin: 0 auto;
    padding: 18px;
  }
  header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    border-bottom: 1px solid #6b5436;
    padding-bottom: 12px;
  }
  h1 {
    margin: 0;
    font-size: 26px;
    letter-spacing: 2px;
    color: #f0c674;
  }
  .sub { color: #a88f65; font-size: 13px; }
  .stats {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 8px;
    margin: 14px 0;
  }
  .stat {
    background: #221a12;
    border: 1px solid #5c472e;
    padding: 8px 10px;
    border-radius: 6px;
  }
  .stat b { display: block; color: #f0c674; font-size: 18px; margin-top: 4px; }
  .bar-wrap {
    height: 12px;
    background: #1a130d;
    border: 1px solid #5c472e;
    border-radius: 8px;
    overflow: hidden;
    margin: 8px 0 16px;
  }
  .bar {
    height: 100%;
    width: 0%;
    background: linear-gradient(90deg, #7b2d26, #d17a3a);
    transition: width .1s linear;
  }
  .grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
  }
  .card {
    background: #221a12;
    border: 1px solid #5c472e;
    border-radius: 8px;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
  }
  .card h3 { margin: 0; color: #f0c674; font-size: 17px; }
  .card p { margin: 0; color: #b9a47d; font-size: 13px; line-height: 1.5; }
  button {
    background: #5b3b22;
    color: #f4dfb4;
    border: 1px solid #a57843;
    border-radius: 6px;
    padding: 8px 10px;
    cursor: pointer;
    font-family: inherit;
    font-size: 14px;
  }
  button:hover { background: #714b2b; }
  button:active { transform: translateY(1px); }
  .log {
    margin-top: 14px;
    background: #1a130d;
    border: 1px solid #5c472e;
    border-radius: 8px;
    padding: 10px 12px;
    min-height: 120px;
  }
  .log h3 { margin: 0 0 8px; color: #f0c674; font-size: 15px; }
  .log div { font-size: 13px; color: #c8b58c; margin: 4px 0; }
  .danger { color: #ff8b7a !important; }
  .good { color: #9fe08a !important; }
  @media (max-width: 760px) {
    .stats { grid-template-columns: repeat(2, 1fr); }
    .grid { grid-template-columns: 1fr; }
    header { align-items: flex-start; flex-direction: column; }
  }
</style>
</head>
<body>
<div id="game">
  <header>
    <div>
      <h1>黑死纪 · 1642</h1>
      <div class="sub">守住村庄,直到黎明。每20秒一波尸潮。</div>
    </div>
    <button onclick="restart()">重新开始</button>
  </header>

  <div class="stats">
    <div class="stat">银币<b id="silver">0</b></div>
    <div class="stat">粮食<b id="food">0</b></div>
    <div class="stat">波次<b id="wave">0</b></div>
    <div class="stat">城墙<b id="wall">0/0</b></div>
    <div class="stat">下一波<b id="next">0s</b></div>
  </div>

  <div class="bar-wrap"><div class="bar" id="waveBar"></div></div>
  <div class="sub" id="hungry" style="margin-bottom:12px;">粮草:正常</div>

  <div class="grid">
    <div class="card">
      <h3>长矛民兵</h3>
      <p>防御 +5,攻击 +4。维护粮草 0.15/秒。<br>花费:银币20,粮食10</p>
      <button onclick="buy('spear')">征召长矛兵</button>
    </div>
    <div class="card">
      <h3>火枪手</h3>
      <p>攻击 +12,防御 +2。维护粮草 0.25/秒。<br>花费:银币35,粮食15</p>
      <button onclick="buy('musket')">训练火枪手</button>
    </div>
    <div class="card">
      <h3>随军牧师</h3>
      <p>每波后修复少量城墙,提供少量攻防。<br>花费:银币30,粮食10</p>
      <button onclick="buy('priest')">安置牧师</button>
    </div>
    <div class="card">
      <h3>修建粮仓</h3>
      <p>粮食产出 +2/秒。<br>花费:银币25</p>
      <button onclick="buy('farm')">修建粮仓</button>
    </div>
    <div class="card">
      <h3>加固城墙</h3>
      <p>最大城墙 +50,并立即修复50。<br>花费:银币40</p>
      <button onclick="buy('wall')">加固城墙</button>
    </div>
    <div class="card">
      <h3>当前守军</h3>
      <p id="armyInfo">长矛兵 0 · 火枪手 0 · 牧师 0</p>
      <p>粮食不足时,守军战力会下降。</p>
    </div>
  </div>

  <div class="log">
    <h3>战报</h3>
    <div id="logs"></div>
  </div>
</div>

<script>
const COST = {
  spear:  { silver: 20, food: 10 },
  musket: { silver: 35, food: 15 },
  priest: { silver: 30, food: 10 },
  farm:   { silver: 25, food: 0  },
  wall:   { silver: 40, food: 0  }
};

let state;

function newState() {
  return {
    silver: 60,
    food: 50,
    wave: 0,
    wall: 100,
    maxWall: 100,
    nextWaveIn: 20,
    running: true,
    hungry: false,
    units: { spear: 0, musket: 0, priest: 0 },
    buildings: { farm: 0 },
    logs: []
  };
}

function restart() {
  state = newState();
  addLog("1642年冬,村庄外传来低沉的嘶吼。准备防守。");
  render();
}

function addLog(msg) {
  state.logs.unshift(msg);
  state.logs = state.logs.slice(0, 8);
  renderLogs();
}

function renderLogs() {
  const el = document.getElementById("logs");
  el.innerHTML = state.logs.map(m => {
    let cls = "";
    if (m.includes("沦陷") || m.includes("突破") || m.includes("不足")) cls = "danger";
    if (m.includes("守住") || m.includes("奖励")) cls = "good";
    return `<div class="${cls}">${m}</div>`;
  }).join("");
}

function canPay(type) {
  const c = COST[type];
  return state.silver >= c.silver && state.food >= c.food;
}

function pay(type) {
  const c = COST[type];
  state.silver -= c.silver;
  state.food -= c.food;
}

function buy(type) {
  if (!state.running) return;
  if (!canPay(type)) {
    addLog("资源不足,无法执行。");
    return;
  }
  pay(type);

  if (type === "spear") {
    state.units.spear++;
    addLog("一名长矛民兵加入了防线。");
  } else if (type === "musket") {
    state.units.musket++;
    addLog("火枪手登上木塔,火药味弥漫。");
  } else if (type === "priest") {
    state.units.priest++;
    addLog("随军牧师开始安抚村民与伤兵。");
  } else if (type === "farm") {
    state.buildings.farm++;
    addLog("新粮仓建成,粮食产出提高。");
  } else if (type === "wall") {
    state.maxWall += 50;
    state.wall = Math.min(state.maxWall, state.wall + 50);
    addLog("城墙得到加固,守军士气上升。");
  }
  render();
}

function getRates() {
  const maintenance =
    state.units.spear * 0.15 +
    state.units.musket * 0.25 +
    state.units.priest * 0.10;

  const silverRate = 1.2;
  const foodRate = 2 + state.buildings.farm * 2 - maintenance;
  return { silverRate, foodRate };
}

function tick(dt) {
  const rates = getRates();
  state.silver += rates.silverRate * dt;
  state.food += rates.foodRate * dt;

  if (state.food <= 0) {
    state.food = 0;
    state.hungry = true;
  } else {
    state.hungry = false;
  }

  state.nextWaveIn -= dt;
  if (state.nextWaveIn <= 0) {
    fight();
  }
  render();
}

function fight() {
  const wave = state.wave + 1;
  const enemyPower = Math.floor(30 + wave * 15 + wave * wave * 1.2);

  let attack =
    state.units.spear * 4 +
    state.units.musket * 12 +
    state.units.priest * 2;

  let defense =
    state.wall * 0.6 +
    state.units.spear * 5 +
    state.units.musket * 2 +
    state.units.priest * 3;

  if (state.hungry) {
    attack *= 0.6;
    defense *= 0.6;
    addLog("粮草不足,守军战力大幅下降!");
  }

  const totalPower = attack + defense;
  let wallLoss;

  if (totalPower >= enemyPower) {
    wallLoss = Math.max(0, enemyPower * 0.35 - defense * 0.25);
    const silverReward = 15 + wave * 8;
    const foodReward = 10 + wave * 4;
    state.silver += silverReward;
    state.food += foodReward;
    addLog(`第${wave}波尸潮被击退。奖励:银币+${silverReward},粮食+${foodReward}。`);
  } else {
    wallLoss = enemyPower - defense * 0.5;
    addLog(`第${wave}波尸潮突破防线!城墙损失约${Math.floor(wallLoss)}。`);
  }

  state.wall -= Math.floor(wallLoss);

  if (state.units.priest > 0 && state.wall > 0) {
    const repair = state.units.priest * 2;
    state.wall = Math.min(state.maxWall, state.wall + repair);
  }

  if (state.wall <= 0) {
    state.wall = 0;
    state.running = false;
    addLog("村庄沦陷。黑死纪仍在继续……点击“重新开始”再试一次。");
    render();
    return;
  }

  state.wave = wave;
  state.nextWaveIn = 20;
  render();
}

function render() {
  document.getElementById("silver").textContent = Math.floor(state.silver);
  document.getElementById("food").textContent = Math.floor(state.food);
  document.getElementById("wave").textContent = state.wave;
  document.getElementById("wall").textContent = Math.ceil(state.wall) + "/" + state.maxWall;
  document.getElementById("next").textContent = state.running
    ? state.nextWaveIn.toFixed(1) + "s"
    : "已沦陷";

  document.getElementById("hungry").textContent = state.hungry
    ? "粮草:不足!守军战力下降"
    : "粮草:正常";
  document.getElementById("hungry").className = state.hungry ? "sub danger" : "sub";

  const progress = Math.max(0, Math.min(100, (20 - state.nextWaveIn) / 20 * 100));
  document.getElementById("waveBar").style.width = progress + "%";

  document.getElementById("armyInfo").textContent =
    `长矛兵 ${state.units.spear} · 火枪手 ${state.units.musket} · 牧师 ${state.units.priest} · 粮仓 ${state.buildings.farm}`;

  renderLogs();
}

restart();
setInterval(() => {
  if (!state.running) return;
  tick(0.1);
}, 100);
</script>
</body>
</html>

Game Source: 黑死纪·1642

Creator: EpicCoder88

Libraries: none

Complexity: complex (374 lines, 9.7 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" to link back to the original. Then publish at arcadelab.ai/publish.