跳到正文

休闲·娱乐

打题打累了,来玩会游戏叭

BaiMi_Not_Stupid约 130 分钟
原文
文章信息与版本

文章信息

发布
2026年9月13日
收录
2026年9月19日
更新
2026年11月9日
篇幅
约 130 分钟
获赞
1

版本

  1. v1当前

霓虹飞车 · 现已公测

游戏介绍

玩法:

image.png

开发者:BaiMi_Not_Stupid

版本号:0.9.5(暂未发布)


网址:霓虹飞车【公测版】


游戏内截屏

image.png


彩蛋与特别鸣谢

为大家测试,开发者给大家准备了自动驾驶(debug)和一些搞笑的彩蛋

自动驾驶

控制台输入 zidong.autojiashi() 即可开启或关闭自动驾驶,车辆会自动评估三条车道的安全距离并切换车道。

  • 参数化调用:支持 zidong.autojiashi(换道冷却, 保守程度) 语法。不填参数时使用默认值(冷却 0.12 秒,保守程度 8)。冷却越大换道越慢,保守程度越大越倾向于待在安全车道。
  • 隐藏成就触发:首次使用自动驾驶后,隐藏成就“车企理赔”会在成就页面出现(初始显示“?”)。达成条件为自动驾驶途中撞车,结算界面会以金色徽章展示。
  • 视觉反馈:开启后车辆外圈有绿色脉冲光环,左上角显示“AUTO”徽章。参数更新时控制台会打印当前冷却和保守程度。
  • 辅助命令:zidong.status() 可在控制台查看自动驾驶状态、当前参数和成就进度。

自动驾驶仅在游戏运行时生效,沙盒模式下撞车不会触发“车企理赔”成就。

image.png

其他彩蛋

此彩蛋暂不公开,各位可以自行探索此彩蛋暂不公开,各位可以自行探索

特别鸣谢

特别鸣谢 1【账号 1】 | 特别鸣谢 1【账号 2】 | 特别鸣谢 2

特别鸣谢 2 与隐藏彩蛋有关

网页源码

【代码】(由于篇幅较长,此处使用折叠框)
HTML
<!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">
<title>霓虹飞车 · Neon Racer</title>
<style>
  * { margin: 0; padding: 0; box-sizing: border-box; }
  html, body {
    width: 100%; height: 100%; overflow: hidden;
    background: #05010f;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
    -webkit-user-select: none; user-select: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: none;
  }
  #game { display: block; position: absolute; inset: 0; }

  .overlay {
    position: absolute; inset: 0;
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    gap: 15px; padding: 24px; text-align: center;
    background: radial-gradient(ellipse at center, rgba(30,0,70,0.55) 0%, rgba(5,1,15,0.94) 70%);
    z-index: 10;
    transition: opacity 0.35s ease, visibility 0.35s;
  }
  .overlay.hidden { opacity: 0; visibility: hidden; pointer-events: none; }

  .title {
    font-size: clamp(34px, 9vw, 64px);
    font-weight: 900;
    letter-spacing: 0.12em;
    color: #fff;
    text-shadow:
      0 0 8px #00e5ff, 0 0 24px #00e5ff,
      0 0 48px #0088ff, 0 0 90px #ff00c8;
    animation: flicker 3.5s infinite;
  }
  @keyframes flicker {
    0%, 92%, 100% { opacity: 1; }
    93% { opacity: 0.55; }
    95% { opacity: 1; }
    97% { opacity: 0.7; }
  }

  .sub {
    font-size: clamp(12px, 3.2vw, 15px);
    color: #9fb8d8;
    line-height: 1.95;
    letter-spacing: 0.04em;
  }
  .sub b { color: #00e5ff; font-weight: 600; }

  .mode-row {
    display: flex; gap: 12px; flex-wrap: wrap;
    justify-content: center; align-items: center;
    margin-top: 4px;
  }

  .btn {
    padding: 14px 30px;
    font-size: clamp(13px, 3.6vw, 16px);
    font-weight: 700;
    letter-spacing: 0.12em;
    border: none; border-radius: 999px;
    cursor: pointer;
    transition: transform 0.15s, box-shadow 0.15s;
    white-space: nowrap;
  }
  .btn:active { transform: scale(0.96); }

  .btn-easy {
    color: #04121a;
    background: linear-gradient(135deg, #00e5ff, #37ffb0);
    box-shadow: 0 0 20px rgba(0,229,255,0.55), 0 0 50px rgba(0,229,255,0.22);
  }
  .btn-easy:hover {
    transform: translateY(-2px) scale(1.03);
    box-shadow: 0 0 30px rgba(0,229,255,0.9), 0 0 70px rgba(0,229,255,0.4);
  }

  .btn-hard {
    color: #1a0410;
    background: linear-gradient(135deg, #ff2d95, #ff9a3c);
    box-shadow: 0 0 20px rgba(255,45,149,0.55), 0 0 50px rgba(255,45,149,0.22);
  }
  .btn-hard:hover {
    transform: translateY(-2px) scale(1.03);
    box-shadow: 0 0 30px rgba(255,45,149,0.9), 0 0 70px rgba(255,45,149,0.4);
  }

  .btn-sandbox {
    color: #10041f;
    background: linear-gradient(135deg, #c56bff, #6b7bff);
    box-shadow: 0 0 20px rgba(197,107,255,0.55), 0 0 50px rgba(197,107,255,0.22);
  }
  .btn-sandbox:hover {
    transform: translateY(-2px) scale(1.03);
    box-shadow: 0 0 30px rgba(197,107,255,0.95), 0 0 70px rgba(197,107,255,0.45);
  }

  .btn-ach {
    color: #fff5e0;
    background: linear-gradient(135deg, #ffb300, #ff6b00);
    box-shadow: 0 0 20px rgba(255,179,0,0.55), 0 0 50px rgba(255,107,0,0.22);
  }
  .btn-ach:hover {
    transform: translateY(-2px) scale(1.03);
    box-shadow: 0 0 30px rgba(255,179,0,0.9), 0 0 70px rgba(255,107,0,0.4);
  }

  .color-row {
    display: flex; align-items: center; gap: 12px;
    flex-wrap: wrap; justify-content: center;
    margin-top: 2px;
  }
  .color-label {
    font-size: clamp(12px, 3.1vw, 14px);
    color: #9fb8d8; letter-spacing: 0.08em;
  }
  .swatches { display: flex; gap: 9px; flex-wrap: wrap; justify-content: center; }
  .swatch {
    width: 26px; height: 26px;
    border-radius: 50%;
    border: 2px solid rgba(255,255,255,0.15);
    cursor: pointer; padding: 0;
    transition: transform 0.15s, border-color 0.15s;
  }
  .swatch:hover { transform: scale(1.15); }
  .swatch.active { border-color: #fff; transform: scale(1.18); }

  .result { font-size: clamp(16px, 4.5vw, 22px); color: #fff; }
  .result .num { color: #00e5ff; font-weight: 800; font-size: 1.5em; }
  .best { font-size: clamp(12px, 3.2vw, 15px); color: #7f93ad; letter-spacing: 0.06em; }

  .ach-list {
    display: flex; flex-wrap: wrap; gap: 6px;
    justify-content: center; max-width: 380px; margin: 2px auto 0;
  }
  .ach-pill {
    display: inline-block;
    padding: 4px 11px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.04em;
    color: #e0d0ff;
    background: rgba(197, 107, 255, 0.16);
    border: 1px solid rgba(197, 107, 255, 0.42);
    white-space: nowrap;
  }
  .ach-pill.gold {
    color: #ffe0a8;
    background: rgba(255, 179, 0, 0.16);
    border-color: rgba(255, 179, 0, 0.5);
  }
  .ach-title {
    font-size: 11px; letter-spacing: 0.18em;
    color: #a98fd0; margin-top: 6px;
  }

  /* ============================ 成就页面 ============================ */
  .ach-page {
    overflow-y: auto;
    justify-content: flex-start;
    padding: 36px 18px 40px;
    gap: 12px;
    z-index: 20;
    -webkit-overflow-scrolling: touch;
  }
  .ach-page-title {
    font-size: clamp(26px, 7vw, 42px);
    font-weight: 900;
    letter-spacing: 0.16em;
    color: #fff;
    text-shadow: 0 0 10px #ffb300, 0 0 30px #ff6b00, 0 0 60px #c56bff;
    flex-shrink: 0;
  }
  .ach-page-progress {
    font-size: clamp(12px, 3.4vw, 15px);
    color: #c9b3e6;
    letter-spacing: 0.12em;
    flex-shrink: 0;
  }
  .ach-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 11px;
    width: 100%;
    max-width: 620px;
    margin: 6px auto 0;
  }
  .ach-card {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 14px;
    border-radius: 15px;
    background: rgba(20, 8, 40, 0.72);
    border: 1px solid rgba(197, 107, 255, 0.22);
    transition: border-color 0.2s, box-shadow 0.2s, transform 0.2s;
    text-align: left;
  }
  .ach-card.unlocked {
    border-color: rgba(197, 107, 255, 0.7);
    background: rgba(35, 14, 60, 0.82);
    box-shadow: 0 0 18px rgba(197, 107, 255, 0.2), inset 0 0 22px rgba(197, 107, 255, 0.06);
  }
  .ach-card.locked { opacity: 0.5; }
  .ach-card.hidden-ach {
    opacity: 0.34;
    border-style: dashed;
    border-color: rgba(255, 179, 0, 0.4);
  }
  .ach-icon-box {
    flex: 0 0 44px;
    height: 44px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    font-weight: 800;
    background: rgba(197, 107, 255, 0.12);
    border: 1px solid rgba(197, 107, 255, 0.35);
    color: #c8a8ff;
  }
  .ach-card.unlocked .ach-icon-box {
    background: linear-gradient(135deg, #c56bff, #6b7bff);
    color: #fff;
    box-shadow: 0 0 14px rgba(197, 107, 255, 0.7);
  }
  .ach-card.hidden-ach .ach-icon-box {
    background: rgba(255, 179, 0, 0.1);
    border-color: rgba(255, 179, 0, 0.4);
    color: #ffb300;
  }
  .ach-card.hidden-ach.unlocked .ach-icon-box {
    background: linear-gradient(135deg, #ffb300, #ff6b00);
    border-color: rgba(255, 179, 0, 0.8);
    color: #fff;
    box-shadow: 0 0 16px rgba(255, 179, 0, 0.7);
  }
  .ach-info { flex: 1; min-width: 0; }
  .ach-name {
    font-size: 14px;
    font-weight: 700;
    color: #d8c8f0;
    letter-spacing: 0.04em;
  }
  .ach-card.unlocked .ach-name {
    color: #fff;
    text-shadow: 0 0 10px rgba(197, 107, 255, 0.55);
  }
  .ach-desc {
    font-size: 12px;
    color: #9382b0;
    margin-top: 3px;
    letter-spacing: 0.02em;
    line-height: 1.35;
  }
  .btn-back {
    margin-top: 18px;
    color: #04121a;
    background: linear-gradient(135deg, #00e5ff, #37ffb0);
    box-shadow: 0 0 20px rgba(0,229,255,0.55), 0 0 50px rgba(0,229,255,0.2);
    flex-shrink: 0;
  }
  .btn-back:hover {
    transform: translateY(-2px) scale(1.03);
    box-shadow: 0 0 30px rgba(0,229,255,0.9), 0 0 70px rgba(0,229,255,0.4);
  }

  /* ============================ 沙盒控制台 ============================ */
  .sandbox-panel {
    position: absolute;
    right: 14px; bottom: 14px;
    width: min(240px, 64vw);
    z-index: 6;
    background: rgba(12, 5, 28, 0.84);
    border: 1px solid rgba(197, 107, 255, 0.42);
    border-radius: 16px;
    padding: 13px 15px;
    display: flex; flex-direction: column; gap: 10px;
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    box-shadow: 0 0 26px rgba(197, 107, 255, 0.24);
    transition: opacity 0.3s, visibility 0.3s;
  }
  .sandbox-panel.hidden { opacity: 0; visibility: hidden; pointer-events: none; }
  .sb-title {
    font-size: 11px; font-weight: 800; letter-spacing: 0.2em;
    color: #c56bff; text-align: center;
    text-shadow: 0 0 10px rgba(197,107,255,0.8);
  }
  .sb-row { display: flex; align-items: center; gap: 9px; }
  .sb-label { font-size: 12px; color: #9fb8d8; letter-spacing: 0.06em; flex: 0 0 28px; }
  .sb-val {
    font-size: 12px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    color: #e8d8ff; flex: 0 0 32px; text-align: right;
  }
  .sb-exit {
    margin-top: 2px; padding: 8px 12px;
    font-size: 12px; font-weight: 700; letter-spacing: 0.14em;
    color: #d8c8ff;
    background: rgba(197, 107, 255, 0.14);
    border: 1px solid rgba(197, 107, 255, 0.4);
    border-radius: 999px; cursor: pointer;
    transition: background 0.15s;
  }
  .sb-exit:hover { background: rgba(197, 107, 255, 0.28); }

  input[type="range"] {
    -webkit-appearance: none; appearance: none;
    flex: 1; min-width: 0; height: 4px; border-radius: 2px;
    background: rgba(255,255,255,0.14); outline: none; cursor: pointer;
  }
  input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none; appearance: none;
    width: 15px; height: 15px; border-radius: 50%;
    background: #c56bff;
    box-shadow: 0 0 10px rgba(197,107,255,0.9);
    cursor: pointer;
  }
  input[type="range"]::-moz-range-thumb {
    width: 15px; height: 15px; border: none; border-radius: 50%;
    background: #c56bff;
    box-shadow: 0 0 10px rgba(197,107,255,0.9);
    cursor: pointer;
  }
</style>
</head>
<body>
<canvas id="game"></canvas>

<!-- 沙盒控制台 -->
<div class="sandbox-panel hidden" id="sandboxPanel">
  <div class="sb-title">SANDBOX</div>
  <div class="sb-row">
    <span class="sb-label">速度</span>
    <input type="range" id="speedSlider" min="8" max="160" value="45" step="1">
    <span class="sb-val" id="speedVal">45</span>
  </div>
  <div class="sb-row">
    <span class="sb-label">车流</span>
    <input type="range" id="densitySlider" min="0.3" max="3" value="1.2" step="0.1">
    <span class="sb-val" id="densityVal">1.2</span>
  </div>
  <button class="sb-exit" id="btnExit">返回菜单</button>
</div>

<!-- 成就页面 -->
<div class="overlay ach-page hidden" id="achPage">
  <div class="ach-page-title">成就</div>
  <div class="ach-page-progress" id="achProgress">已解锁 0 / 18</div>
  <div class="ach-grid" id="achGrid"></div>
  <button class="btn btn-back" id="btnAchBack">返回菜单</button>
</div>

<!-- 开始/结束遮罩 -->
<div class="overlay" id="overlay">
  <div class="title" id="ovTitle">霓虹飞车</div>
  <div class="sub" id="ovText">
    <b>← →</b> 或 <b>A / D</b> 控制方向 · 手机滑动 / 拖拽<br>
    <span style="color:#37ffb0">简单</span> 自动吸附 · <span style="color:#ffb066">困难</span> 自由转向 · <span style="color:#c56bff">沙盒</span> 无敌可调<br>
    背景随游玩时间变换 · 沙盒模式不计成就
  </div>
  <div class="mode-row">
    <button class="btn btn-easy" id="btnEasy">简单模式</button>
    <button class="btn btn-hard" id="btnHard">困难模式</button>
    <button class="btn btn-sandbox" id="btnSandbox">沙盒模式</button>
  </div>
  <div class="mode-row">
    <button class="btn btn-ach" id="btnAchievements">🏆 成就</button>
  </div>
  <div class="color-row">
    <span class="color-label">车身颜色</span>
    <div class="swatches" id="swatches"></div>
  </div>
  <div class="best" id="ovBest"></div>
</div>

<script>
(function () {
  'use strict';

  /* ============================== DOM ============================== */
  const canvas = document.getElementById('game');
  const ctx = canvas.getContext('2d');
  const overlay = document.getElementById('overlay');
  const ovTitle = document.getElementById('ovTitle');
  const ovText = document.getElementById('ovText');
  const ovBest = document.getElementById('ovBest');
  const btnEasy = document.getElementById('btnEasy');
  const btnHard = document.getElementById('btnHard');
  const btnSandbox = document.getElementById('btnSandbox');
  const btnAchievements = document.getElementById('btnAchievements');
  const swatchesEl = document.getElementById('swatches');
  const sandboxPanel = document.getElementById('sandboxPanel');
  const speedSlider = document.getElementById('speedSlider');
  const speedVal = document.getElementById('speedVal');
  const densitySlider = document.getElementById('densitySlider');
  const densityVal = document.getElementById('densityVal');
  const btnExit = document.getElementById('btnExit');

  const achPage = document.getElementById('achPage');
  const achGrid = document.getElementById('achGrid');
  const achProgress = document.getElementById('achProgress');
  const btnAchBack = document.getElementById('btnAchBack');

  const OV_TEXT_HTML = ovText.innerHTML;
  const TITLE_SHADOW = '0 0 8px #00e5ff, 0 0 24px #00e5ff, 0 0 48px #0088ff, 0 0 90px #ff00c8';
  const OVER_SHADOW = '0 0 10px #ff2d95, 0 0 30px #ff2d95, 0 0 60px #ff0055';

  /* ============================== 常量 ============================== */
  let W = 0, H = 0, f = 0, horizonY = 0;

  const Z_PLAYER = 2.0;
  const Z_FAR = 70;
  const LANES = [-0.62, 0, 0.62];
  const CAR_COLORS = ['#ff2d95', '#ffb300', '#7c4dff', '#00e676', '#ff5722', '#29b6f6', '#e040fb', '#ffea00'];

  const SWIPE_STEP = 34;
  const LANE_COOLDOWN = 0.15;

  const KEY_SPEED = 2.4;
  const DRAG_SPEED = 3.6;
  const DRAG_PER_PX = 0.004;

  // 自动驾驶默认参数(可在控制台通过 zidong.autojiashi(冷却, 保守度) 覆盖)
  const AUTO_LANE_COOLDOWN_DEFAULT = 0.12;  // 换道冷却(秒)
  const AUTO_LANE_PENALTY_DEFAULT = 8;      // 保守程度(越大越谨慎)
  const AUTO_LANE_COOLDOWN_MIN = 0.02, AUTO_LANE_COOLDOWN_MAX = 1.0;
  const AUTO_LANE_PENALTY_MIN = 0,    AUTO_LANE_PENALTY_MAX = 30;

  const PLAYER_COLORS = [
    '#00e5ff', '#37ffb0', '#ff2d95', '#ffb300',
    '#7c4dff', '#ff5722', '#e040fb', '#ffffff'
  ];

  /* ============================== 成就定义 ============================== */
  const ACHIEVEMENTS = [
    /* ---------- 可见成就(11 个) ---------- */
    { id: 'firstLane',  icon: '⇄', name: '初次变道',  desc: '完成第一次变道',           check: s => s.laneChanges >= 1 },
    { id: 'score500',   icon: '★', name: '小试牛刀',  desc: '单局得分达到 500',         check: s => s.score >= 500 },
    { id: 'score1500',  icon: '✦', name: '渐入佳境',  desc: '单局得分达到 1500',        check: s => s.score >= 1500 },
    { id: 'score3000',  icon: '❖', name: '风驰电掣',  desc: '单局得分达到 3000',        check: s => s.score >= 3000 },
    { id: 'score6000',  icon: '♛', name: '霓虹传奇',  desc: '单局得分达到 6000',        check: s => s.score >= 6000 },
    { id: 'survive30',  icon: '⏱', name: '半分钟',    desc: '单局存活 30 秒',           check: s => s.time >= 30 },
    { id: 'survive60',  icon: '⌛', name: '一分钟',    desc: '单局存活 60 秒',           check: s => s.time >= 60 },
    { id: 'dodge20',    icon: '≫', name: '穿梭车流',  desc: '单局躲过 20 辆车',         check: s => s.dodged >= 20 },
    { id: 'dodge50',    icon: '⇉', name: '车流大师',  desc: '单局躲过 50 辆车',         check: s => s.dodged >= 50 },
    { id: 'maxSpeed',   icon: '⚡', name: '极速狂飙',  desc: '达到最高速度',             check: s => s.reachedMaxSpeed },
    { id: 'hardCore',   icon: '☠', name: '硬核玩家',  desc: '困难模式得分达到 1000',    check: s => s.mode === 'hard' && s.score >= 1000 },

    /* ---------- 隐藏成就(7 个) ---------- */
    { id: 'marathon',   icon: '☰', name: '长跑健将',    desc: '单局存活 180 秒',            check: s => s.time >= 180, hidden: true },
    { id: 'edgeWalker', icon: '◧', name: '边缘行者',    desc: '单局横向贴边累计 12 秒',     check: s => s.edgeTime >= 12, hidden: true },
    { id: 'fullSpeed',  icon: '➤', name: '全速前进',    desc: '困难模式速度 80+ 保持 20 秒', check: s => s.maxSpeedHold >= 20, hidden: true },
    { id: 'dodge100',   icon: '∞', name: '百无禁忌',    desc: '单局躲过 100 辆车',          check: s => s.dodged >= 100, hidden: true },
    { id: 'whiteGhost', icon: '◇', name: '白色幽灵',    desc: '白色车身单局得分 2000',      check: s => s.startColor === '#ffffff' && s.score >= 2000, hidden: true },
    { id: 'danceKing',  icon: '♪', name: '风一样的男子', desc: '单局变道 30 次',            check: s => s.laneChanges >= 30, hidden: true },
    // 车企理赔:首次使用自动驾驶后才在成就页面出现
    { id: 'autoCrash',  icon: '✚', name: '车企理赔',    desc: '在自动驾驶途中撞车',         check: s => s.autoCrash === true, hidden: true, requiresAutoPilot: true }
  ];

  function getVisibleAchievements() {
    return ACHIEVEMENTS.filter(function (a) {
      return !a.requiresAutoPilot || state.hasUsedAutoPilot;
    });
  }

  /* ============================== 动态背景主题 ============================== */
  const THEME_HOLD = 22;

  const THEMES = [
    {
      skyTop: [5, 0, 20],   skyMid: [21, 7, 53],  skyLow: [46, 11, 82], skyBot: [92, 18, 99],
      groundTop: [26, 11, 48], groundBot: [5, 1, 9],
      roadTop: [23, 20, 40], roadBot: [12, 12, 24],
      shoulder: [20, 16, 45],
      horizonMid: [255, 45, 149], horizonEdge: [0, 229, 255],
      glowA: [0, 229, 255], glowB: [255, 45, 149],
      edgeL: [125, 250, 255], edgeR: [255, 142, 203],
      skyline: [8, 2, 22]
    },
    {
      skyTop: [1, 5, 16],   skyMid: [3, 16, 48],  skyLow: [6, 40, 90],  skyBot: [15, 75, 125],
      groundTop: [5, 18, 40], groundBot: [1, 3, 8],
      roadTop: [16, 22, 34], roadBot: [8, 12, 20],
      shoulder: [12, 24, 42],
      horizonMid: [60, 180, 255], horizonEdge: [0, 229, 255],
      glowA: [0, 190, 255], glowB: [90, 140, 255],
      edgeL: [140, 230, 255], edgeR: [110, 180, 255],
      skyline: [2, 8, 20]
    },
    {
      skyTop: [16, 1, 6],   skyMid: [55, 6, 20],  skyLow: [110, 14, 30], skyBot: [180, 40, 55],
      groundTop: [40, 8, 12], groundBot: [6, 1, 2],
      roadTop: [34, 16, 18], roadBot: [18, 8, 10],
      shoulder: [42, 14, 18],
      horizonMid: [255, 90, 70], horizonEdge: [255, 40, 40],
      glowA: [255, 80, 80], glowB: [255, 160, 60],
      edgeL: [255, 170, 150], edgeR: [255, 110, 95],
      skyline: [14, 2, 4]
    },
    {
      skyTop: [0, 12, 10],  skyMid: [2, 38, 30],  skyLow: [4, 80, 62],  skyBot: [10, 135, 100],
      groundTop: [4, 26, 20], groundBot: [0, 4, 3],
      roadTop: [16, 28, 26], roadBot: [8, 16, 14],
      shoulder: [10, 30, 24],
      horizonMid: [80, 255, 200], horizonEdge: [0, 255, 160],
      glowA: [0, 255, 170], glowB: [140, 255, 210],
      edgeL: [170, 255, 225], edgeR: [110, 255, 195],
      skyline: [1, 8, 6]
    },
    {
      skyTop: [12, 4, 28],  skyMid: [40, 12, 75], skyLow: [85, 32, 125], skyBot: [140, 70, 165],
      groundTop: [34, 14, 48], groundBot: [5, 2, 10],
      roadTop: [28, 20, 40], roadBot: [14, 10, 22],
      shoulder: [30, 16, 48],
      horizonMid: [200, 100, 255], horizonEdge: [255, 150, 230],
      glowA: [185, 100, 255], glowB: [255, 130, 220],
      edgeL: [225, 175, 255], edgeR: [255, 175, 240],
      skyline: [10, 4, 22]
    }
  ];

  function lerp(a, b, t) { return a + (b - a) * t; }

  function mixTheme(a, b, t) {
    const out = {};
    for (const k in a) {
      const va = a[k], vb = b[k];
      if (Array.isArray(va) && Array.isArray(vb)) {
        out[k] = [lerp(va[0], vb[0], t), lerp(va[1], vb[1], t), lerp(va[2], vb[2], t)];
      } else {
        out[k] = vb;
      }
    }
    return out;
  }

  function computeTheme(t) {
    const total = t / THEME_HOLD;
    const idx = Math.floor(total);
    const frac = total - idx;
    const a = THEMES[idx % THEMES.length];
    const b = THEMES[(idx + 1) % THEMES.length];
    let blend = 0;
    if (frac > 0.68) {
      blend = (frac - 0.68) / 0.32;
      blend = blend * blend * (3 - 2 * blend);
    }
    if (blend <= 0) return a;
    if (blend >= 1) return b;
    return mixTheme(a, b, blend);
  }

  function rgbStr(c) { return 'rgb(' + (c[0] | 0) + ',' + (c[1] | 0) + ',' + (c[2] | 0) + ')'; }
  function rgbaStr(c, a) { return 'rgba(' + (c[0] | 0) + ',' + (c[1] | 0) + ',' + (c[2] | 0) + ',' + a + ')'; }

  /* ============================== 状态 ============================== */
  const state = {
    mode: 'easy',
    running: false,
    over: false,
    time: 0,
    distance: 0,
    speed: 30,

    playerLane: 1,
    playerX: 0,
    playerVX: 0,
    laneCooldown: 0,
    tilt: 0,

    playerColor: '#00e5ff',
    startColor: '#00e5ff',
    sandboxSpeed: 45,
    sandboxDensity: 1.2,

    cars: [],
    spawnTimer: 1.2,
    score: 0,
    best: 0,
    shake: 0,
    flash: 0,

    // 局内成就追踪
    laneChanges: 0,
    dodged: 0,
    reachedMaxSpeed: false,
    edgeTime: 0,
    maxSpeedHold: 0,
    autoCrash: false,
    unlockedThisRun: new Set(),
    unlockedAll: new Set(),
    toasts: [],

    // 调试 / 自动驾驶
    autoPilot: false,
    hasUsedAutoPilot: false,        // 首次启动后 → 车企理赔成就进入成就页
    autoLaneCooldown: AUTO_LANE_COOLDOWN_DEFAULT,
    autoLanePenalty: AUTO_LANE_PENALTY_DEFAULT
  };

  let stars = [];
  let elapsed = 0;

  try {
    state.best = parseInt(localStorage.getItem('neonRacerBest') || '0', 10) || 0;
    const savedColor = localStorage.getItem('neonRacerColor');
    if (savedColor && /^#[0-9a-fA-F]{6}$/.test(savedColor)) state.playerColor = savedColor;
    const savedAch = localStorage.getItem('neonRacerAchievements');
    if (savedAch) {
      savedAch.split(',').filter(Boolean).forEach(id => state.unlockedAll.add(id));
    }
    state.hasUsedAutoPilot = localStorage.getItem('neonRacerUsedAuto') === '1';
  } catch (e) {}

  function saveUnlocked() {
    try {
      localStorage.setItem('neonRacerAchievements', Array.from(state.unlockedAll).join(','));
    } catch (e) {}
  }

  function saveUsedAuto() {
    try { localStorage.setItem('neonRacerUsedAuto', '1'); } catch (e) {}
  }

  /* ============================== 颜色选择 ============================== */
  function buildSwatches() {
    swatchesEl.innerHTML = '';
    PLAYER_COLORS.forEach((col) => {
      const b = document.createElement('button');
      b.className = 'swatch' + (col.toLowerCase() === state.playerColor.toLowerCase() ? ' active' : '');
      b.style.background = col;
      b.style.boxShadow = '0 0 10px ' + col;
      b.setAttribute('aria-label', col);
      b.addEventListener('click', () => {
        state.playerColor = col;
        try { localStorage.setItem('neonRacerColor', col); } catch (e) {}
        buildSwatches();
      });
      swatchesEl.appendChild(b);
    });
  }
  buildSwatches();

  /* ============================== 成就页面 ============================== */
  function renderAchievements() {
    achGrid.innerHTML = '';
    let unlockedCount = 0;
    const list = getVisibleAchievements();
    const total = list.length;

    list.forEach((a) => {
      const isUnlocked = state.unlockedAll.has(a.id);
      if (isUnlocked) unlockedCount++;

      const card = document.createElement('div');
      card.className = 'ach-card';
      if (isUnlocked) {
        card.classList.add('unlocked');
      } else if (a.hidden) {
        card.classList.add('hidden-ach');
      } else {
        card.classList.add('locked');
      }

      const iconBox = document.createElement('div');
      iconBox.className = 'ach-icon-box';
      iconBox.textContent = (a.hidden && !isUnlocked) ? '?' : a.icon;

      const info = document.createElement('div');
      info.className = 'ach-info';

      const nameEl = document.createElement('div');
      nameEl.className = 'ach-name';
      nameEl.textContent = (a.hidden && !isUnlocked) ? '???' : a.name;

      const descEl = document.createElement('div');
      descEl.className = 'ach-desc';
      descEl.textContent = (a.hidden && !isUnlocked) ? '隐藏成就,达成后自动揭晓' : a.desc;

      info.appendChild(nameEl);
      info.appendChild(descEl);
      card.appendChild(iconBox);
      card.appendChild(info);
      achGrid.appendChild(card);
    });

    achProgress.textContent = '已解锁 ' + unlockedCount + ' / ' + total;
  }

  btnAchievements.addEventListener('click', () => {
    overlay.classList.add('hidden');
    sandboxPanel.classList.add('hidden');
    renderAchievements();
    achPage.classList.remove('hidden');
    achPage.scrollTop = 0;
  });

  btnAchBack.addEventListener('click', () => {
    achPage.classList.add('hidden');
    resetOverlay();
    overlay.classList.remove('hidden');
  });

  /* ============================== 输入 ============================== */
  const keys = { left: false, right: false };
  const pointers = new Map();

  let swipeAnchorX = null, swipeStartX = null;
  let dragActive = false, dragId = null, dragAnchorX = 0, dragBaseX = 0, dragTargetX = 0;

  function resetInputState() {
    keys.left = keys.right = false;
    pointers.clear();
    swipeAnchorX = swipeStartX = null;
    dragActive = false;
    dragId = null;
  }

  function isLaneMode() { return state.mode === 'easy'; }

  function tryChangeLane(dir) {
    if (!state.running) return;
    const next = state.playerLane + dir;
    if (next < 0 || next >= LANES.length) return;
    state.playerLane = next;
    state.laneChanges++;
  }

  canvas.addEventListener('pointerdown', (e) => {
    try { canvas.setPointerCapture(e.pointerId); } catch (err) {}
    pointers.set(e.pointerId, e.clientX);

    if (pointers.size === 1) {
      if (isLaneMode()) {
        swipeAnchorX = e.clientX;
        swipeStartX = e.clientX;
      } else if (state.running) {
        dragActive = true;
        dragId = e.pointerId;
        dragAnchorX = e.clientX;
        dragBaseX = state.playerX;
        dragTargetX = state.playerX;
      }
    } else {
      swipeAnchorX = swipeStartX = null;
      dragActive = false;
      dragId = null;
    }
  });

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

    if (isLaneMode()) {
      if (pointers.size !== 1) { swipeAnchorX = null; return; }
      if (swipeAnchorX === null) { swipeAnchorX = e.clientX; return; }

      let dx = e.clientX - swipeAnchorX;
      let guard = 0;
      while (Math.abs(dx) >= SWIPE_STEP && guard++ < 4) {
        tryChangeLane(dx > 0 ? 1 : -1);
        swipeAnchorX += dx > 0 ? SWIPE_STEP : -SWIPE_STEP;
        dx = e.clientX - swipeAnchorX;
      }
    } else if (dragActive && e.pointerId === dragId) {
      dragTargetX = Math.max(-0.78, Math.min(0.78,
        dragBaseX + (e.clientX - dragAnchorX) * DRAG_PER_PX));
    }
  });

  function endPointer(e) {
    const wasSingle = pointers.size === 1 && pointers.has(e.pointerId);
    pointers.delete(e.pointerId);

    if (isLaneMode()) {
      if (wasSingle) {
        if (swipeStartX !== null && Math.abs(e.clientX - swipeStartX) < 10) {
          tryChangeLane(e.clientX < W * 0.5 ? -1 : 1);
        }
        swipeAnchorX = null;
        swipeStartX = null;
      }
    } else if (dragActive && e.pointerId === dragId) {
      dragActive = false;
      dragId = null;
    }
    if (pointers.size === 0) {
      swipeAnchorX = swipeStartX = null;
      dragActive = false;
      dragId = null;
    }
  }
  canvas.addEventListener('pointerup', endPointer);
  canvas.addEventListener('pointercancel', endPointer);
  canvas.addEventListener('pointerleave', endPointer);

  window.addEventListener('keydown', (e) => {
    const k = e.key;
    if (k === 'ArrowLeft' || k === 'a' || k === 'A') { keys.left = true; e.preventDefault(); }
    if (k === 'ArrowRight' || k === 'd' || k === 'D') { keys.right = true; e.preventDefault(); }
    if ((k === ' ' || k === 'Enter') && !state.running) { e.preventDefault(); startGame(state.mode); }
  });
  window.addEventListener('keyup', (e) => {
    const k = e.key;
    if (k === 'ArrowLeft' || k === 'a' || k === 'A') keys.left = false;
    if (k === 'ArrowRight' || k === 'd' || k === 'D') keys.right = false;
  });
  window.addEventListener('blur', resetInputState);

  btnEasy.addEventListener('click', () => startGame('easy'));
  btnHard.addEventListener('click', () => startGame('hard'));
  btnSandbox.addEventListener('click', () => startGame('sandbox'));

  speedSlider.addEventListener('input', () => {
    state.sandboxSpeed = parseFloat(speedSlider.value);
    speedVal.textContent = String(Math.round(state.sandboxSpeed));
  });
  densitySlider.addEventListener('input', () => {
    state.sandboxDensity = parseFloat(densitySlider.value);
    densityVal.textContent = state.sandboxDensity.toFixed(1);
  });

  btnExit.addEventListener('click', () => {
    state.running = false;
    state.over = false;
    sandboxPanel.classList.add('hidden');
    resetOverlay();
    overlay.classList.remove('hidden');
  });

  /* ============================== 尺寸 ============================== */
  function resize() {
    const dpr = Math.min(window.devicePixelRatio || 1, 2);
    W = window.innerWidth;
    H = window.innerHeight;
    canvas.width = Math.floor(W * dpr);
    canvas.height = Math.floor(H * dpr);
    canvas.style.width = W + 'px';
    canvas.style.height = H + 'px';
    ctx.setTransform(dpr, 0, 0, dpr, 0, 0);

    horizonY = H * 0.33;
    f = Math.min(H * 0.88, W * 0.85);

    stars = [];
    const count = Math.round(Math.min(120, W * H / 12000));
    for (let i = 0; i < count; i++) {
      stars.push({
        x: Math.random() * W,
        y: Math.random() * horizonY * 0.92,
        r: Math.random() * 1.4 + 0.3,
        a: Math.random() * 0.6 + 0.35,
        tw: Math.random() * Math.PI * 2
      });
    }
  }
  window.addEventListener('resize', resize);

  /* ============================== 工具 ============================== */
  function projX(x, z) { return W * 0.5 + (f * x) / z; }
  function projY(z) { return horizonY + f / z; }
  function clamp(v, a, b) { return v < a ? a : (v > b ? b : v); }

  function roundRect(c, x, y, w, h, r) {
    r = Math.min(r, Math.abs(w) / 2, Math.abs(h) / 2);
    c.beginPath();
    c.moveTo(x + r, y);
    c.lineTo(x + w - r, y);
    c.quadraticCurveTo(x + w, y, x + w, y + r);
    c.lineTo(x + w, y + h - r);
    c.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
    c.lineTo(x + r, y + h);
    c.quadraticCurveTo(x, y + h, x, y + h - r);
    c.lineTo(x, y + r);
    c.quadraticCurveTo(x, y, x + r, y);
    c.closePath();
  }

  function shade(hex, amt) {
    const n = parseInt(hex.slice(1), 16);
    let r = (n >> 16) & 255, g = (n >> 8) & 255, b = n & 255;
    if (amt >= 0) {
      r += (255 - r) * amt; g += (255 - g) * amt; b += (255 - b) * amt;
    } else {
      r *= (1 + amt); g *= (1 + amt); b *= (1 + amt);
    }
    return 'rgb(' + (r | 0) + ',' + (g | 0) + ',' + (b | 0) + ')';
  }

  function withAlpha(hex, a) {
    const n = parseInt(hex.slice(1), 16);
    return 'rgba(' + ((n >> 16) & 255) + ',' + ((n >> 8) & 255) + ',' + (n & 255) + ',' + a + ')';
  }

  /* ============================== 自动驾驶核心 ============================== */
  function computeLaneSafety() {
    const safety = [Infinity, Infinity, Infinity];
    for (let i = 0; i < state.cars.length; i++) {
      const c = state.cars[i];
      if (c.z < Z_PLAYER - 0.5) continue;
      let li = 0, bestD = Infinity;
      for (let j = 0; j < 3; j++) {
        const d = Math.abs(c.x - LANES[j]);
        if (d < bestD) { bestD = d; li = j; }
      }
      if (bestD > 0.35) continue;
      const dist = c.z - Z_PLAYER;
      if (dist < safety[li]) safety[li] = dist;
    }
    return safety;
  }

  function autoPickLane() {
    if (state.laneCooldown > 0) return;

    const safety = computeLaneSafety();
    const current = state.playerLane;

    let target = current;
    let bestScore = safety[current];
    for (let i = 0; i < 3; i++) {
      if (i === current) continue;
      const penalty = Math.abs(i - current) * state.autoLanePenalty;
      const score = safety[i] - penalty;
      if (score > bestScore) {
        bestScore = score;
        target = i;
      }
    }

    if (target !== current) {
      state.playerLane = target;
      state.laneChanges++;
      state.laneCooldown = state.autoLaneCooldown;
    }
  }

  function autoSteerHard(dt) {
    const safety = computeLaneSafety();

    let curLane = 0, curD = Infinity;
    for (let j = 0; j < 3; j++) {
      const d = Math.abs(LANES[j] - state.playerX);
      if (d < curD) { curD = d; curLane = j; }
    }

    let targetLane = curLane;
    let bestScore = safety[curLane];
    for (let i = 0; i < 3; i++) {
      if (i === curLane) continue;
      const penalty = Math.abs(i - curLane) * state.autoLanePenalty;
      const score = safety[i] - penalty;
      if (score > bestScore) {
        bestScore = score;
        targetLane = i;
      }
    }

    const targetX = LANES[targetLane];
    const diff = targetX - state.playerX;
    const maxStep = 3.2 * dt;
    const step = clamp(diff, -maxStep, maxStep);
    state.playerX += step;
    state.playerVX = step / Math.max(dt, 1e-4);
    state.playerX = clamp(state.playerX, -0.78, 0.78);
  }

  /* ============================== 全局调试接口 ============================== */
  window.zidong = {
    /**
     * 切换自动驾驶
     *   zidong.autojiashi()                     → 仅切换开关,使用当前参数
     *   zidong.autojiashi(0.06)                 → 设置换道冷却后切换
     *   zidong.autojiashi(0.06, 15)             → 同时设置换道冷却与保守程度
     *
     * @param {number} [laneCooldown]  换道冷却(秒),范围 0.02 ~ 1.0,默认 0.12
     * @param {number} [conservative]  保守程度(0~30),越大越保守,默认 8
     */
    autojiashi: function (laneCooldown, conservative) {
      let paramChanged = false;

      if (typeof laneCooldown === 'number' && isFinite(laneCooldown)) {
        state.autoLaneCooldown = clamp(laneCooldown, AUTO_LANE_COOLDOWN_MIN, AUTO_LANE_COOLDOWN_MAX);
        paramChanged = true;
      }
      if (typeof conservative === 'number' && isFinite(conservative)) {
        state.autoLanePenalty = clamp(conservative, AUTO_LANE_PENALTY_MIN, AUTO_LANE_PENALTY_MAX);
        paramChanged = true;
      }

      state.autoPilot = !state.autoPilot;

      // 第一次开启 → 解锁"车企理赔"成就入口
      if (state.autoPilot && !state.hasUsedAutoPilot) {
        state.hasUsedAutoPilot = true;
        saveUsedAuto();
        console.log(
          '%c🔓 新隐藏成就已出现:车企理赔(达成条件:在自动驾驶途中撞车)',
          'color:#ffb300;font-weight:bold;font-size:12px'
        );
      }

      const on = state.autoPilot;
      const tag = on ? '🟢 ON ' : '🔴 OFF';

      console.log(
        '%c[自动驾驶] ' + tag,
        'color:' + (on ? '#37ffb0' : '#ff7ac4') + ';font-weight:bold;font-size:13px'
      );
      console.log(
        '%c换道冷却: ' + state.autoLaneCooldown.toFixed(2) + ' 秒 · 保守程度: ' + state.autoLanePenalty.toFixed(1) + (paramChanged ? ' (已更新)' : ''),
        'color:#c56bff;font-size:11px'
      );
      console.log(
        '%c再调用一次 zidong.autojiashi() 可切换状态',
        'color:#9fb8d8;font-size:11px'
      );

      return on;
    },

    /** 查看当前状态 */
    status: function () {
      const s = {
        自动驾驶: state.autoPilot ? 'ON' : 'OFF',
        换道冷却: state.autoLaneCooldown.toFixed(2) + ' 秒',
        保守程度: state.autoLanePenalty.toFixed(1),
        模式: state.mode,
        运行中: state.running,
        得分: state.score,
        车速: Math.round(state.speed * 3.3) + ' km/h',
        已解锁成就: state.unlockedAll.size + ' / ' + ACHIEVEMENTS.length
      };
      console.table(s);
      return s;
    }
  };

  console.log(
    '%c霓虹飞车 · 调试提示\n' +
    '%c输入 %czidong.autojiashi()%c 开启/关闭自动驾驶\n' +
    '%c参数:%czidong.autojiashi(换道冷却, 保守程度)%c 例:%czidong.autojiashi(0.05, 15)%c\n' +
    '%c默认值:换道冷却 0.12 秒,保守程度 8(越大越谨慎)',
    'color:#c56bff;font-weight:bold;font-size:14px',
    'color:#9fb8d8',
    'color:#37ffb0;font-weight:bold;font-family:monospace',
    'color:#9fb8d8',
    'color:#9fb8d8',
    'color:#ffb300;font-weight:bold;font-family:monospace',
    'color:#9fb8d8',
    'color:#37ffb0;font-weight:bold;font-family:monospace',
    'color:#9fb8d8;font-size:11px'
  );

  /* ============================== 成就逻辑 ============================== */
  function pushToast(ach) {
    state.toasts.push({ ach: ach, age: 0, total: 3.8 });
    if (state.toasts.length > 3) state.toasts.shift();
  }

  function updateToasts(dt) {
    for (let i = state.toasts.length - 1; i >= 0; i--) {
      const t = state.toasts[i];
      t.age += dt;
      if (t.age >= t.total) state.toasts.splice(i, 1);
    }
  }

  function checkAchievements() {
    if (state.mode === 'sandbox') return;
    if (!state.running) return;

    for (let i = 0; i < ACHIEVEMENTS.length; i++) {
      const a = ACHIEVEMENTS[i];
      if (state.unlockedThisRun.has(a.id)) continue;
      if (a.check(state)) {
        state.unlockedThisRun.add(a.id);
        if (!state.unlockedAll.has(a.id)) {
          state.unlockedAll.add(a.id);
          saveUnlocked();
        }
        pushToast(a);
      }
    }
  }

  /* ============================== 游戏流程 ============================== */
  function resetOverlay() {
    ovTitle.textContent = '霓虹飞车';
    ovTitle.style.textShadow = TITLE_SHADOW;
    ovText.innerHTML = OV_TEXT_HTML;
    ovBest.textContent = state.best > 0 ? '最高分:' + state.best : '';
  }

  function startGame(mode) {
    state.mode = (mode === 'hard' || mode === 'sandbox') ? mode : 'easy';
    state.running = true;
    state.over = false;
    state.time = 0;
    state.distance = 0;

    state.playerLane = 1;
    state.playerX = LANES[1];
    state.playerVX = 0;
    state.laneCooldown = 0;
    state.tilt = 0;

    state.speed = state.mode === 'sandbox' ? state.sandboxSpeed : 30;
    state.startColor = state.playerColor;

    state.cars = [];
    state.spawnTimer = 1.0;
    state.score = 0;
    state.shake = 0;
    state.flash = 0;

    state.laneChanges = 0;
    state.dodged = 0;
    state.reachedMaxSpeed = false;
    state.edgeTime = 0;
    state.maxSpeedHold = 0;
    state.autoCrash = false;
    state.unlockedThisRun = new Set();
    state.toasts = [];

    resetInputState();

    if (state.mode === 'sandbox') {
      sandboxPanel.classList.remove('hidden');
    } else {
      sandboxPanel.classList.add('hidden');
    }

    overlay.classList.add('hidden');
  }

  function gameOver() {
    // 【关键】自动驾驶途中撞车 → 触发隐藏成就"车企理赔"
    if (state.autoPilot && state.mode !== 'sandbox') {
      state.autoCrash = true;
      checkAchievements();   // 此时 running 仍为 true,可正常判定
    }

    state.running = false;
    state.over = true;
    state.shake = 22;
    state.flash = 1;
    state.toasts.length = 0;

    if (state.score > state.best) {
      state.best = state.score;
      try { localStorage.setItem('neonRacerBest', String(state.best)); } catch (e) {}
    }

    let achHtml = '';
    if (state.unlockedThisRun.size > 0) {
      achHtml = '<div class="ach-title">本局成就</div><div class="ach-list">';
      ACHIEVEMENTS.forEach(a => {
        if (state.unlockedThisRun.has(a.id)) {
          const gold = a.hidden ? ' gold' : '';
          achHtml += '<span class="ach-pill' + gold + '">' + a.icon + ' ' + a.name + '</span>';
        }
      });
      achHtml += '</div>';
    }

    ovTitle.textContent = '撞车了!';
    ovTitle.style.textShadow = OVER_SHADOW;
    ovText.innerHTML = '<div class="result">本次得分 <span class="num">' + state.score + '</span></div>' + achHtml;
    ovBest.textContent = '最高分:' + state.best;
    setTimeout(() => overlay.classList.remove('hidden'), 450);
  }

  function spawnCar() {
    const laneIdx = Math.floor(Math.random() * LANES.length);
    const x = LANES[laneIdx];
    for (let i = 0; i < state.cars.length; i++) {
      const c = state.cars[i];
      if (c.z > 34 && Math.abs(c.x - x) < 0.5) return;
    }
    state.cars.push({
      x: x,
      z: 58,
      speed: state.speed * (0.42 + Math.random() * 0.33),
      color: CAR_COLORS[(Math.random() * CAR_COLORS.length) | 0]
    });
  }

  function spawnBase() {
    if (state.mode === 'sandbox') return 1.55 / Math.max(0.3, state.sandboxDensity);
    return Math.max(0.62, 1.7 - state.time * 0.017);
  }

  function update(dt) {
    elapsed += dt;
    updateToasts(dt);

    if (state.shake > 0) state.shake = Math.max(0, state.shake - dt * 60);
    if (state.flash > 0) state.flash = Math.max(0, state.flash - dt * 2.5);

    if (state.over) {
      state.distance += state.speed * dt * 0.25;
      state.speed = Math.max(0, state.speed - dt * 60);
      return;
    }

    if (!state.running) {
      state.time += dt * 0.55;
      state.distance += 22 * dt;
      return;
    }

    state.time += dt;

    /* --- 速度 --- */
    if (state.mode === 'sandbox') {
      state.speed += (state.sandboxSpeed - state.speed) * Math.min(1, dt * 1.6);
    } else {
      const targetSpeed = 30 + Math.min(55, state.time * 0.85);
      state.speed += (targetSpeed - state.speed) * Math.min(1, dt * 0.7);
      if (state.speed >= 84) state.reachedMaxSpeed = true;
      if (state.mode === 'hard' && state.speed >= 80) {
        state.maxSpeedHold += dt;
      }
    }
    state.distance += state.speed * dt;
    state.score = Math.floor(state.distance * 1.1);

    /* --- 横向控制 --- */
    if (isLaneMode()) {
      state.laneCooldown -= dt;

      if (state.autoPilot) {
        autoPickLane();
      } else {
        let dirInput = 0;
        if (keys.left) dirInput -= 1;
        if (keys.right) dirInput += 1;

        if (dirInput !== 0 && state.laneCooldown <= 0) {
          const before = state.playerLane;
          tryChangeLane(dirInput);
          state.laneCooldown = (state.playerLane !== before) ? LANE_COOLDOWN : 0.08;
        }
      }

      const targetX = LANES[state.playerLane];
      const kSpring = 130;
      const cDamp = 2 * Math.sqrt(kSpring);
      state.playerVX += (kSpring * (targetX - state.playerX) - cDamp * state.playerVX) * dt;
      state.playerVX = clamp(state.playerVX, -6, 6);
      state.playerX += state.playerVX * dt;

    } else {
      if (state.autoPilot) {
        autoSteerHard(dt);
      } else {
        let vx = 0;
        if (dragActive) {
          const diff = dragTargetX - state.playerX;
          const maxStep = DRAG_SPEED * dt;
          const step = clamp(diff, -maxStep, maxStep);
          state.playerX += step;
          vx = step / Math.max(dt, 1e-4);
        } else {
          let dir = 0;
          if (keys.left) dir -= 1;
          if (keys.right) dir += 1;
          if (dir !== 0) {
            state.playerX += dir * KEY_SPEED * dt;
            vx = dir * KEY_SPEED;
          }
        }
        state.playerX = clamp(state.playerX, -0.78, 0.78);
        state.playerVX = vx;
      }
    }

    /* --- 贴边计时 --- */
    if (Math.abs(state.playerX) > 0.6) {
      state.edgeTime += dt;
    }

    /* --- 车身倾斜 --- */
    const tiltTarget = clamp(state.playerVX * 0.09, -0.2, 0.2);
    state.tilt += (tiltTarget - state.tilt) * Math.min(1, dt * 12);

    /* --- 生成对手 --- */
    state.spawnTimer -= dt;
    if (state.spawnTimer <= 0) {
      spawnCar();
      const base = spawnBase();
      state.spawnTimer = base * (0.72 + Math.random() * 0.7);
    }

    /* --- 更新对手 --- */
    for (let i = state.cars.length - 1; i >= 0; i--) {
      const c = state.cars[i];
      const rel = Math.max(3, state.speed - c.speed);
      const zPrev = c.z;
      c.z -= rel * dt;

      if (c.z < 0.9) {
        state.cars.splice(i, 1);
        continue;
      }

      const passedPlayer = (zPrev > Z_PLAYER) !== (c.z > Z_PLAYER);
      const nearPlayer = Math.abs(c.z - Z_PLAYER) < 0.62;

      if ((passedPlayer || nearPlayer) && Math.abs(c.x - state.playerX) < 0.44) {
        if (state.mode === 'sandbox') {
          state.cars.splice(i, 1);
          state.shake = Math.max(state.shake, 9);
          state.flash = Math.max(state.flash, 0.3);
          continue;
        } else {
          gameOver();
          return;
        }
      }

      if (zPrev > Z_PLAYER && c.z <= Z_PLAYER) {
        state.dodged++;
      }
    }

    checkAchievements();
  }

  /* ============================== 渲染 ============================== */
  function render() {
    const theme = computeTheme(state.time);

    ctx.shadowBlur = 0;
    ctx.globalAlpha = 1;

    ctx.save();
    if (state.shake > 0.5) {
      ctx.translate(
        (Math.random() - 0.5) * state.shake,
        (Math.random() - 0.5) * state.shake
      );
    }

    drawSky(theme);
    drawGround(theme);
    drawRoad(theme);
    if (isLaneMode()) drawLaneGuide();
    drawCars();

    ctx.restore();

    if (state.flash > 0) {
      ctx.fillStyle = 'rgba(255,60,120,' + (state.flash * 0.5) + ')';
      ctx.fillRect(0, 0, W, H);
    }

    drawHUD();
    drawToasts();
    drawAutoBadge();
  }

  function drawSky(theme) {
    const g = ctx.createLinearGradient(0, 0, 0, horizonY);
    g.addColorStop(0, rgbStr(theme.skyTop));
    g.addColorStop(0.42, rgbStr(theme.skyMid));
    g.addColorStop(0.78, rgbStr(theme.skyLow));
    g.addColorStop(1, rgbStr(theme.skyBot));
    ctx.fillStyle = g;
    ctx.fillRect(0, 0, W, horizonY + 1);

    for (let i = 0; i < stars.length; i++) {
      const s = stars[i];
      const a = s.a * (0.65 + 0.35 * Math.sin(elapsed * 2.1 + s.tw));
      ctx.globalAlpha = a;
      ctx.fillStyle = '#e6f2ff';
      ctx.beginPath();
      ctx.arc(s.x, s.y, s.r, 0, Math.PI * 2);
      ctx.fill();
    }
    ctx.globalAlpha = 1;

    const hg = ctx.createLinearGradient(0, horizonY - H * 0.15, 0, horizonY + 2);
    hg.addColorStop(0, rgbaStr(theme.horizonMid, 0));
    hg.addColorStop(0.65, rgbaStr(theme.horizonMid, 0.18));
    hg.addColorStop(1, rgbaStr(theme.horizonEdge, 0.45));
    ctx.fillStyle = hg;
    ctx.fillRect(0, horizonY - H * 0.15, W, H * 0.15 + 2);

    ctx.fillStyle = rgbaStr(theme.skyline, 0.86);
    for (let i = 0; i < 26; i++) {
      const bx = (i / 26) * W + ((i * 37) % 11) - 5;
      const bw = W / 26 * (0.5 + ((i * 13) % 10) / 14);
      const bh = (6 + ((i * 29) % 17)) * (H / 700);
      ctx.fillRect(bx, horizonY - bh, bw, bh + 2);
    }
  }

  function drawGround(theme) {
    const g = ctx.createLinearGradient(0, horizonY, 0, H);
    g.addColorStop(0, rgbStr(theme.groundTop));
    g.addColorStop(1, rgbStr(theme.groundBot));
    ctx.fillStyle = g;
    ctx.fillRect(0, horizonY, W, H - horizonY);
  }

  function drawRoad(theme) {
    const zB = f / (H - horizonY);
    if (!isFinite(zB) || zB <= 0) return;
    const zT = Z_FAR;

    const yB = H;
    const yT = projY(zT);
    const xBL = projX(-1, zB), xBR = projX(1, zB);
    const xTL = projX(-1, zT), xTR = projX(1, zT);

    const rg = ctx.createLinearGradient(0, yT, 0, yB);
    rg.addColorStop(0, rgbStr(theme.roadTop));
    rg.addColorStop(1, rgbStr(theme.roadBot));
    ctx.beginPath();
    ctx.moveTo(xBL, yB);
    ctx.lineTo(xTL, yT);
    ctx.lineTo(xTR, yT);
    ctx.lineTo(xBR, yB);
    ctx.closePath();
    ctx.fillStyle = rg;
    ctx.fill();

    const shoulder = 0.16;
    ctx.fillStyle = rgbaStr(theme.shoulder, 0.92);
    ctx.beginPath();
    ctx.moveTo(projX(-1 - shoulder, zB), yB);
    ctx.lineTo(projX(-1 - shoulder, zT), yT);
    ctx.lineTo(xTL, yT);
    ctx.lineTo(xBL, yB);
    ctx.closePath();
    ctx.fill();

    ctx.beginPath();
    ctx.moveTo(xBR, yB);
    ctx.lineTo(xTR, yT);
    ctx.lineTo(projX(1 + shoulder, zT), yT);
    ctx.lineTo(projX(1 + shoulder, zB), yB);
    ctx.closePath();
    ctx.fill();

    ctx.lineCap = 'round';
    ctx.shadowBlur = 16;
    ctx.lineWidth = 2.5;

    ctx.shadowColor = rgbStr(theme.glowA);
    ctx.strokeStyle = rgbStr(theme.edgeL);
    ctx.beginPath();
    ctx.moveTo(xBL, yB);
    ctx.lineTo(xTL, yT);
    ctx.stroke();

    ctx.shadowColor = rgbStr(theme.glowB);
    ctx.strokeStyle = rgbStr(theme.edgeR);
    ctx.beginPath();
    ctx.moveTo(xBR, yB);
    ctx.lineTo(xTR, yT);
    ctx.stroke();
    ctx.shadowBlur = 0;

    const D = state.distance;
    const dashSpacing = 4.0;
    const dashLen = 2.0;
    const dashW = 0.032;

    const nStart = Math.ceil((D + zB) / dashSpacing);
    const nEnd = Math.ceil((D + zT) / dashSpacing);

    ctx.fillStyle = rgbaStr(theme.edgeL, 0.4);
    for (let n = nStart; n <= nEnd; n++) {
      const z1 = n * dashSpacing - D;
      const z2 = z1 + dashLen;
      if (z2 <= zB || z1 >= zT) continue;
      const za = Math.max(z1, zB);
      const zb = Math.min(z2, zT);
      if (zb <= za) continue;

      for (let li = 0; li < 2; li++) {
        const dx = li === 0 ? -0.31 : 0.31;
        const ya = projY(za), yb = projY(zb);
        const a1 = projX(dx - dashW, za), a2 = projX(dx + dashW, za);
        const b1 = projX(dx - dashW, zb), b2 = projX(dx + dashW, zb);
        ctx.beginPath();
        ctx.moveTo(a1, ya);
        ctx.lineTo(b1, yb);
        ctx.lineTo(b2, yb);
        ctx.lineTo(a2, ya);
        ctx.closePath();
        ctx.fill();
      }
    }

    const postSpacing = 9;
    const pStart = Math.ceil((D + zB) / postSpacing);
    const pEnd = Math.ceil((D + zT) / postSpacing);
    for (let n = pStart; n <= pEnd; n++) {
      const z = n * postSpacing - D;
      if (z < zB || z > zT) continue;
      const fade = clamp((zT - z) / (zT * 0.75), 0, 1);
      const hgt = 0.55 * (f / z);
      if (hgt < 0.6) continue;

      for (let si = 0; si < 2; si++) {
        const side = si === 0 ? -1.22 : 1.22;
        const px = projX(side, z);
        const py = projY(z);
        const col = si === 0 ? theme.glowA : theme.glowB;

        ctx.strokeStyle = rgbaStr(col, 0.35 * fade);
        ctx.lineWidth = Math.max(0.8, 0.03 * f / z);
        ctx.beginPath();
        ctx.moveTo(px, py);
        ctx.lineTo(px, py - hgt);
        ctx.stroke();

        ctx.fillStyle = rgbaStr(col, 0.85 * fade);
        ctx.shadowBlur = 12 * fade;
        ctx.shadowColor = rgbStr(col);
        const dr = Math.max(1, 0.045 * f / z);
        ctx.beginPath();
        ctx.arc(px, py - hgt, dr, 0, Math.PI * 2);
        ctx.fill();
        ctx.shadowBlur = 0;
      }
    }
  }

  function drawLaneGuide() {
    if (!state.running && !state.over) return;

    const col = state.playerColor;
    const x = LANES[state.playerLane];
    const z1 = Z_PLAYER - 0.55;
    const z2 = Z_PLAYER + 24;
    const y1 = projY(z1), y2 = projY(z2);

    const g = ctx.createLinearGradient(0, y2, 0, y1);
    g.addColorStop(0, withAlpha(col, 0));
    g.addColorStop(0.55, withAlpha(col, 0.07));
    g.addColorStop(1, withAlpha(col, 0.3));

    const half = 0.11;
    ctx.beginPath();
    ctx.moveTo(projX(x - half, z1), y1);
    ctx.lineTo(projX(x - half, z2), y2);
    ctx.lineTo(projX(x + half, z2), y2);
    ctx.lineTo(projX(x + half, z1), y1);
    ctx.closePath();
    ctx.fillStyle = g;
    ctx.fill();

    ctx.strokeStyle = withAlpha(col, 0.24);
    ctx.lineWidth = 1.2;
    ctx.beginPath();
    ctx.moveTo(projX(x, z1), y1);
    ctx.lineTo(projX(x, z2), y2);
    ctx.stroke();
  }

  function drawCarBody(w, h, color) {
    ctx.beginPath();
    ctx.ellipse(0, -h * 0.04, w * 0.62, h * 0.11, 0, 0, Math.PI * 2);
    ctx.fillStyle = 'rgba(0,0,0,0.55)';
    ctx.fill();

    ctx.fillStyle = '#08080e';
    const ww = w * 0.17, wh = h * 0.3;
    ctx.fillRect(-w * 0.57, -h * 0.38, ww, wh);
    ctx.fillRect(w * 0.57 - ww, -h * 0.38, ww, wh);

    const grad = ctx.createLinearGradient(0, -h, 0, 0);
    grad.addColorStop(0, shade(color, 0.28));
    grad.addColorStop(0.42, color);
    grad.addColorStop(1, shade(color, -0.55));
    ctx.fillStyle = grad;
    roundRect(ctx, -w * 0.5, -h, w, h, w * 0.2);
    ctx.fill();

    ctx.strokeStyle = 'rgba(255,255,255,0.16)';
    ctx.lineWidth = Math.max(0.5, w * 0.018);
    ctx.stroke();

    ctx.fillStyle = 'rgba(8,16,34,0.88)';
    roundRect(ctx, -w * 0.35, -h * 0.93, w * 0.7, h * 0.32, w * 0.1);
    ctx.fill();

    ctx.fillStyle = 'rgba(255,255,255,0.09)';
    roundRect(ctx, -w * 0.3, -h * 0.99, w * 0.6, h * 0.06, w * 0.05);
    ctx.fill();

    ctx.shadowColor = '#ff2244';
    ctx.shadowBlur = w * 0.4;
    ctx.fillStyle = '#ff3b5c';
    roundRect(ctx, -w * 0.44, -h * 0.24, w * 0.3, h * 0.1, w * 0.045);
    ctx.fill();
    roundRect(ctx, w * 0.14, -h * 0.24, w * 0.3, h * 0.1, w * 0.045);
    ctx.fill();
    ctx.shadowBlur = 0;

    ctx.fillStyle = 'rgba(20,20,30,0.9)';
    roundRect(ctx, -w * 0.48, -h * 0.13, w * 0.96, h * 0.1, w * 0.04);
    ctx.fill();
  }

  function drawCars() {
    const items = [];
    for (let i = 0; i < state.cars.length; i++) {
      items.push({ z: state.cars[i].z, car: state.cars[i], player: false });
    }
    items.push({ z: Z_PLAYER, player: true });
    items.sort((a, b) => b.z - a.z);

    for (let i = 0; i < items.length; i++) {
      const it = items[i];
      if (it.player) drawPlayerCar();
      else drawEnemyCar(it.car);
    }
  }

  function drawEnemyCar(c) {
    if (c.z <= 1.0) return;
    const sc = f / c.z;
    const sx = W * 0.5 + c.x * sc;
    const sy = horizonY + f / c.z;
    const w = 0.42 * sc;
    const h = w * 1.4;

    if (w < 1.2) return;
    if (sy - h > H + 50) return;
    if (sx + w < -60 || sx - w > W + 60) return;

    const alpha = clamp((58 - c.z) / 16, 0, 1);
    if (alpha <= 0.02) return;

    ctx.save();
    ctx.globalAlpha = alpha;
    ctx.translate(sx, sy);
    drawCarBody(w, h, c.color);
    ctx.restore();
    ctx.globalAlpha = 1;
  }

  function drawPlayerCar() {
    const sc = f / Z_PLAYER;
    const sx = W * 0.5 + state.playerX * sc;
    const sy = horizonY + f / Z_PLAYER;
    const w = 0.42 * sc;
    const h = w * 1.4;
    const col = state.playerColor;

    ctx.save();
    ctx.translate(sx, sy);
    ctx.rotate(state.tilt * 0.55);
    ctx.scale(1 - Math.abs(state.tilt) * 0.12, 1);

    ctx.shadowColor = col;
    ctx.shadowBlur = w * 0.5;
    ctx.fillStyle = withAlpha(col, 0.2);
    ctx.beginPath();
    ctx.ellipse(0, -h * 0.05, w * 0.75, h * 0.16, 0, 0, Math.PI * 2);
    ctx.fill();
    ctx.shadowBlur = 0;

    if (isLaneMode()) {
      const pulse = 0.5 + 0.5 * Math.sin(elapsed * 6);
      ctx.strokeStyle = withAlpha(col, 0.18 + 0.22 * pulse);
      ctx.lineWidth = Math.max(1, w * 0.028);
      ctx.beginPath();
      ctx.ellipse(0, -h * 0.05,
        w * 0.62 * (1 + pulse * 0.12),
        h * 0.13 * (1 + pulse * 0.12),
        0, 0, Math.PI * 2);
      ctx.stroke();
    }

    if (state.mode === 'sandbox') {
      const t = elapsed * 2.2;
      ctx.strokeStyle = 'rgba(197,107,255,' + (0.25 + 0.2 * Math.sin(t)) + ')';
      ctx.lineWidth = Math.max(1, w * 0.02);
      ctx.beginPath();
      ctx.ellipse(0, -h * 0.05, w * 0.85, h * 0.19, 0, 0, Math.PI * 2);
      ctx.stroke();
    }

    if (state.autoPilot && state.running) {
      const t = elapsed * 4;
      ctx.strokeStyle = 'rgba(55,255,176,' + (0.4 + 0.35 * Math.sin(t)) + ')';
      ctx.lineWidth = Math.max(1.2, w * 0.03);
      ctx.beginPath();
      ctx.ellipse(0, -h * 0.05, w * 0.95, h * 0.21, 0, 0, Math.PI * 2);
      ctx.stroke();
    }

    drawCarBody(w, h, col);

    ctx.shadowColor = col;
    ctx.shadowBlur = w * 0.45;
    ctx.fillStyle = 'rgba(255,255,255,0.88)';
    roundRect(ctx, -w * 0.16, -h * 0.99, w * 0.32, h * 0.05, w * 0.03);
    ctx.fill();
    ctx.shadowBlur = 0;

    ctx.restore();
  }

  function drawToasts() {
    if (state.toasts.length === 0) return;

    ctx.save();
    ctx.textBaseline = 'middle';

    const baseY = 104;

    for (let i = 0; i < state.toasts.length; i++) {
      const t = state.toasts[i];
      const aIn = Math.min(1, t.age / 0.32);
      const aOut = Math.min(1, (t.total - t.age) / 0.45);
      const alpha = Math.max(0, Math.min(aIn, aOut));
      if (alpha <= 0.01) continue;

      const ease = 1 - Math.pow(1 - aIn, 3);
      const slideY = (1 - ease) * -26;
      const ach = t.ach;

      ctx.font = '800 14px ui-monospace, SFMono-Regular, Menlo, monospace';
      const nameW = ctx.measureText(ach.name).width;
      ctx.font = '600 12px ui-monospace, SFMono-Regular, Menlo, monospace';
      const descW = ctx.measureText(ach.desc).width;

      const iconW = 26;
      const padX = 16;
      const innerW = iconW + 8 + nameW + 10 + descW;
      const boxW = Math.min(W - 40, innerW + padX * 2);
      const boxH = 42;
      const boxX = (W - boxW) * 0.5;
      const boxY = baseY + i * (boxH + 10) + slideY;

      ctx.globalAlpha = alpha;

      const bg = ctx.createLinearGradient(boxX, 0, boxX + boxW, 0);
      bg.addColorStop(0, 'rgba(28, 8, 52, 0.94)');
      bg.addColorStop(0.5, 'rgba(40, 12, 74, 0.94)');
      bg.addColorStop(1, 'rgba(24, 6, 46, 0.94)');
      ctx.fillStyle = bg;
      roundRect(ctx, boxX, boxY, boxW, boxH, 21);
      ctx.fill();

      const isHidden = !!ach.hidden;
      ctx.strokeStyle = isHidden ? 'rgba(255, 179, 0, 0.75)' : 'rgba(197, 107, 255, 0.7)';
      ctx.lineWidth = 1.4;
      ctx.shadowColor = isHidden ? 'rgba(255, 179, 0, 0.85)' : 'rgba(197, 107, 255, 0.85)';
      ctx.shadowBlur = 14;
      roundRect(ctx, boxX, boxY, boxW, boxH, 21);
      ctx.stroke();
      ctx.shadowBlur = 0;

      const cx = boxX + padX + iconW * 0.5;
      const cy = boxY + boxH * 0.5;
      ctx.beginPath();
      ctx.arc(cx, cy, iconW * 0.5, 0, Math.PI * 2);
      const ig = ctx.createLinearGradient(cx - iconW * 0.5, cy - iconW * 0.5, cx + iconW * 0.5, cy + iconW * 0.5);
      if (isHidden) {
        ig.addColorStop(0, '#ffb300');
        ig.addColorStop(1, '#ff6b00');
        ctx.shadowColor = 'rgba(255, 179, 0, 0.9)';
      } else {
        ig.addColorStop(0, '#c56bff');
        ig.addColorStop(1, '#6b7bff');
        ctx.shadowColor = 'rgba(197, 107, 255, 0.9)';
      }
      ctx.fillStyle = ig;
      ctx.shadowBlur = 12;
      ctx.fill();
      ctx.shadowBlur = 0;

      ctx.font = '800 14px ui-monospace, SFMono-Regular, Menlo, monospace';
      ctx.fillStyle = '#ffffff';
      ctx.textAlign = 'center';
      ctx.fillText(ach.icon, cx, cy + 1);

      ctx.textAlign = 'left';
      ctx.font = '800 14px ui-monospace, SFMono-Regular, Menlo, monospace';
      ctx.fillStyle = '#ffffff';
      ctx.fillText(ach.name, boxX + padX + iconW + 8, cy);

      ctx.font = '600 12px ui-monospace, SFMono-Regular, Menlo, monospace';
      ctx.fillStyle = isHidden ? 'rgba(255, 210, 130, 0.88)' : 'rgba(200, 175, 255, 0.85)';
      ctx.fillText(ach.desc, boxX + padX + iconW + 8 + nameW + 10, cy);
    }

    ctx.globalAlpha = 1;
    ctx.restore();
  }

  function drawAutoBadge() {
    if (!state.autoPilot) return;
    if (!state.running && !state.over) return;

    ctx.save();
    ctx.textBaseline = 'middle';
    ctx.textAlign = 'left';

    const bx = 22;
    const by = 122;
    const bw = 74;
    const bh = 22;

    const pulse = 0.65 + 0.35 * Math.sin(elapsed * 6);

    ctx.fillStyle = 'rgba(55, 255, 176, ' + (0.15 + 0.1 * pulse) + ')';
    roundRect(ctx, bx, by, bw, bh, 11);
    ctx.fill();

    ctx.strokeStyle = 'rgba(55, 255, 176, ' + (0.55 + 0.35 * pulse) + ')';
    ctx.lineWidth = 1.2;
    ctx.shadowColor = '#37ffb0';
    ctx.shadowBlur = 10;
    roundRect(ctx, bx, by, bw, bh, 11);
    ctx.stroke();
    ctx.shadowBlur = 0;

    ctx.font = '800 11px ui-monospace, SFMono-Regular, Menlo, monospace';
    ctx.fillStyle = '#37ffb0';
    ctx.shadowColor = '#37ffb0';
    ctx.shadowBlur = 8;
    ctx.fillText('◉ AUTO', bx + 12, by + bh * 0.5 + 0.5);
    ctx.shadowBlur = 0;

    ctx.restore();
  }

  function drawHUD() {
    if (!state.running && !state.over) return;

    const easy = state.mode === 'easy';
    const sandbox = state.mode === 'sandbox';

    ctx.save();
    ctx.textBaseline = 'middle';

    ctx.font = '700 14px ui-monospace, SFMono-Regular, Menlo, monospace';
    ctx.fillStyle = 'rgba(0,229,255,0.7)';
    ctx.textAlign = 'left';
    ctx.fillText('SCORE', 22, 30);

    ctx.font = '800 30px ui-monospace, SFMono-Regular, Menlo, monospace';
    ctx.fillStyle = '#ffffff';
    ctx.shadowColor = '#00e5ff';
    ctx.shadowBlur = 14;
    ctx.fillText(String(state.score).padStart(5, '0'), 22, 60);
    ctx.shadowBlur = 0;

    const kmh = Math.round(state.speed * 3.3);
    ctx.textAlign = 'right';
    ctx.font = '700 14px ui-monospace, SFMono-Regular, Menlo, monospace';
    ctx.fillStyle = 'rgba(255,45,149,0.75)';
    ctx.fillText('SPEED', W - 22, 30);

    ctx.font = '800 30px ui-monospace, SFMono-Regular, Menlo, monospace';
    ctx.fillStyle = '#ffffff';
    ctx.shadowColor = '#ff2d95';
    ctx.shadowBlur = 14;
    ctx.fillText(String(kmh), W - 22, 60);
    ctx.shadowBlur = 0;

    ctx.font = '700 13px ui-monospace, SFMono-Regular, Menlo, monospace';
    ctx.fillStyle = 'rgba(255,255,255,0.5)';
    ctx.fillText('km/h', W - 22, 82);

    const barW = Math.min(180, W * 0.3);
    const barX = W - 22 - barW;
    const barY = 98;
    const ratio = clamp((state.speed - 8) / 100, 0, 1);
    ctx.fillStyle = 'rgba(255,255,255,0.12)';
    roundRect(ctx, barX, barY, barW, 6, 3);
    ctx.fill();
    const bg = ctx.createLinearGradient(barX, 0, barX + barW, 0);
    bg.addColorStop(0, '#00e5ff');
    bg.addColorStop(0.6, '#7c4dff');
    bg.addColorStop(1, '#ff2d95');
    ctx.fillStyle = bg;
    roundRect(ctx, barX, barY, Math.max(6, barW * ratio), 6, 3);
    ctx.fill();

    ctx.textAlign = 'center';
    ctx.font = '800 13px ui-monospace, SFMono-Regular, Menlo, monospace';
    if (easy) {
      ctx.fillStyle = 'rgba(55,255,176,0.9)';
      ctx.shadowColor = '#37ffb0';
    } else if (sandbox) {
      ctx.fillStyle = 'rgba(197,107,255,0.95)';
      ctx.shadowColor = '#c56bff';
    } else {
      ctx.fillStyle = 'rgba(255,140,70,0.95)';
      ctx.shadowColor = '#ff8c46';
    }
    ctx.shadowBlur = 10;
    ctx.fillText(easy ? '简单 · 吸附' : (sandbox ? '沙盒 · 无敌' : '困难 · 自由'), W * 0.5, 28);
    ctx.shadowBlur = 0;

    if (!sandbox) {
      const visTotal = getVisibleAchievements().length;
      ctx.font = '700 11px ui-monospace, SFMono-Regular, Menlo, monospace';
      ctx.fillStyle = 'rgba(255,179,0,0.8)';
      ctx.shadowColor = '#ffb300';
      ctx.shadowBlur = 8;
      ctx.fillText('成就 ' + state.unlockedThisRun.size + ' / ' + visTotal, W * 0.5, 48);
      ctx.shadowBlur = 0;
    }

    if (easy) {
      const dotY = H - 34;
      const dotGap = 26;
      const cx = W * 0.5;
      for (let i = 0; i < LANES.length; i++) {
        const dx = cx + (i - 1) * dotGap;
        const active = i === state.playerLane;
        ctx.beginPath();
        ctx.arc(dx, dotY, active ? 5 : 3, 0, Math.PI * 2);
        if (active) {
          ctx.fillStyle = state.playerColor;
          ctx.shadowColor = state.playerColor;
          ctx.shadowBlur = 12;
        } else {
          ctx.fillStyle = 'rgba(255,255,255,0.22)';
          ctx.shadowBlur = 0;
        }
        ctx.fill();
        ctx.shadowBlur = 0;
      }
    } else {
      const markerColor = sandbox ? '#c56bff' : '#ff9a3c';
      const trackW = Math.min(W * 0.5, 300);
      const trackX = W * 0.5 - trackW / 2;
      const trackY = H - 34;

      ctx.shadowBlur = 0;
      ctx.fillStyle = 'rgba(255,255,255,0.12)';
      roundRect(ctx, trackX, trackY - 2, trackW, 4, 2);
      ctx.fill();

      for (let i = 0; i < LANES.length; i++) {
        const px = trackX + ((LANES[i] + 0.78) / 1.56) * trackW;
        ctx.fillStyle = 'rgba(255,255,255,0.2)';
        ctx.fillRect(px - 0.5, trackY - 7, 1, 14);
      }

      const carPx = trackX + ((state.playerX + 0.78) / 1.56) * trackW;
      ctx.fillStyle = markerColor;
      ctx.shadowColor = markerColor;
      ctx.shadowBlur = 12;
      ctx.beginPath();
      ctx.arc(carPx, trackY, 5, 0, Math.PI * 2);
      ctx.fill();
      ctx.shadowBlur = 0;
    }

    ctx.restore();
  }

  /* ============================== 主循环 ============================== */
  let lastTime = performance.now();

  function loop(now) {
    let dt = (now - lastTime) / 1000;
    lastTime = now;
    if (dt > 0.05) dt = 0.05;
    if (dt < 0) dt = 0;

    update(dt);
    render();

    requestAnimationFrame(loop);
  }

  /* ============================== 初始化 ============================== */
  resize();
  resetOverlay();
  speedSlider.value = String(state.sandboxSpeed);
  speedVal.textContent = String(Math.round(state.sandboxSpeed));
  densitySlider.value = String(state.sandboxDensity);
  densityVal.textContent = state.sandboxDensity.toFixed(1);
  requestAnimationFrame(loop);

})();
</script>
</body>
</html>
手敲代码太苦了,用 DeepSeek(及其他AIGC技术)生成
反馈邮箱
原文