🎮ArcadeLab

低碳闯关挑战

by PrismDolphin72
293 lines11.0 KB
▶ Play
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>低碳闯关挑战</title>
<style>
  *{box-sizing:border-box}
  body{margin:0;padding:14px;font-family:"Microsoft YaHei",system-ui,sans-serif;
       background:#eef7f2;display:flex;justify-content:center}
  #wrap{width:100%;max-width:640px}
  h1{font-size:19px;text-align:center;color:#1f6f4a;margin:4px 0 8px}
  #hud{display:flex;justify-content:space-between;align-items:center;
       font-size:14px;color:#2b5c46;margin-bottom:4px;flex-wrap:wrap;gap:4px}
  #lives{font-size:17px;letter-spacing:2px}
  #bar{height:8px;background:#d6e9df;border-radius:6px;overflow:hidden;margin-bottom:10px}
  #barIn{height:100%;width:100%;background:#3fb27f}
  #stage{position:relative;height:340px;border:2px solid #b6dcc8;border-radius:14px;
         overflow:hidden;background:linear-gradient(#f4fcf7,#d9efe3);
         display:flex;align-items:center;justify-content:center}
  #card{width:82%;max-width:380px;padding:22px 16px;border-radius:16px;background:#fff;
        box-shadow:0 6px 18px rgba(0,0,0,.12);text-align:center;display:none;
        animation:pop .25s ease-out}
  @keyframes pop{from{transform:scale(.85);opacity:0}to{transform:scale(1);opacity:1}}
  #card .emoji{font-size:44px;line-height:1}
  #card .name{font-size:18px;color:#234;margin-top:8px;font-weight:bold}
  #card .tip{font-size:12px;color:#888;margin-top:6px;min-height:16px}
  .btnrow{display:flex;gap:12px;margin-top:14px}
  .btnrow button{flex:1;padding:12px;font-size:16px;border:none;border-radius:12px;
                 color:#fff;cursor:pointer;font-weight:bold}
  #lowBtn{background:#3fb27f}#lowBtn:hover{background:#349a6c}
  #highBtn{background:#e05a4f}#highBtn:hover{background:#c94940}
  .btnrow button:disabled{opacity:.45;cursor:default}
  #overlay{position:absolute;inset:0;background:rgba(255,255,255,.96);border-radius:12px;
           display:flex;flex-direction:column;align-items:center;justify-content:center;
           text-align:center;padding:18px}
  #overlay h2{color:#1f6f4a;margin:0 0 8px;font-size:19px}
  #overlay p{color:#41685a;font-size:14px;line-height:1.7;margin:3px 0}
  #overlay button{margin-top:14px;padding:10px 26px;font-size:16px;border:none;
                  border-radius:999px;background:#3fb27f;color:#fff;cursor:pointer}
  #overlay button:hover{background:#349a6c}
  .hide{display:none!important}
</style>
</head>
<body>
<div id="wrap">
  <h1>🌍 低碳闯关挑战</h1>
  <div id="hud">
    <div id="lives">❤️❤️❤️</div>
    <div>第 <b id="level">1</b> 关</div>
    <div>得分:<b id="score">0</b></div>
    <div>连击:<b id="combo">0</b></div>
  </div>
  <div id="bar"><div id="barIn"></div></div>
  <div id="stage">
    <div id="card">
      <div class="emoji">🌱</div>
      <div class="name">准备中...</div>
      <div class="tip"></div>
    </div>
    <div id="overlay">
      <h2>低碳闯关挑战</h2>
      <p>共 <b>3 关</b>,每关 <b>5 题</b>,你有 <b>3 条生命</b>。</p >
      <p>判断行为是 <b style="color:#3fb27f">低碳</b> 还是 <b style="color:#e05a4f">高碳</b>,答错或超时扣 1 条命。</p >
      <p>关卡越高,时间越短,题目越迷惑!</p >
      <button id="startBtn">开始挑战</button>
    </div>
  </div>
  <div class="btnrow">
    <button id="lowBtn" disabled>🌱 低碳</button>
    <button id="highBtn" disabled>🏭 高碳</button>
  </div>
</div>

<script>
(function(){
  const stage    = document.getElementById('stage');
  const card     = document.getElementById('card');
  const overlay  = document.getElementById('overlay');
  const scoreEl  = document.getElementById('score');
  const comboEl  = document.getElementById('combo');
  const levelEl  = document.getElementById('level');
  const livesEl  = document.getElementById('lives');
  const barIn    = document.getElementById('barIn');
  const lowBtn   = document.getElementById('lowBtn');
  const highBtn  = document.getElementById('highBtn');
  const startBtn = document.getElementById('startBtn');

  // lv:从第几关开始出现该题
  const ITEMS = [
    {e:'🚲', n:'骑自行车上学',      low:true,  lv:1},
    {e:'💡', n:'随手关灯',          low:true,  lv:1},
    {e:'🛍️', n:'自带购物袋',        low:true,  lv:1},
    {e:'🌳', n:'参与种树活动',      low:true,  lv:1},
    {e:'☀️', n:'使用太阳能发电',    low:true,  lv:1},
    {e:'💡', n:'长明灯不关',        low:false, lv:1},
    {e:'🥢', n:'使用一次性筷子',    low:false, lv:1},
    {e:'🚗', n:'短途也开车出行',    low:false, lv:1},
    {e:'📺', n:'电视待机不拔插头',  low:false, lv:1},
    {e:'💧', n:'水龙头一直开着',    low:false, lv:1},

    {e:'🚿', n:'用洗菜水浇花',      low:true,  lv:2},
    {e:'📄', n:'纸张双面打印',      low:true,  lv:2},
    {e:'🚶', n:'步行去附近超市',    low:true,  lv:2},
    {e:'🥤', n:'自带水杯',          low:true,  lv:2},
    {e:'👕', n:'旧衣物回收',        low:true,  lv:2},
    {e:'🚌', n:'坐公交出行',        low:true,  lv:2},
    {e:'🛍️', n:'大量使用塑料袋',    low:false, lv:2},
    {e:'🍚', n:'经常浪费食物',      low:false, lv:2},
    {e:'💡', n:'用节能灯替换白炽灯',low:true,  lv:2},

    {e:'🌡️', n:'夏天空调开26度',    low:true,  lv:3},
    {e:'❄️', n:'夏天空调开16度',    low:false, lv:3},
    {e:'📦', n:'商品过度包装',      low:false, lv:3},
    {e:'🔥', n:'露天焚烧垃圾',      low:false, lv:3},
    {e:'🍖', n:'经常吃露天烧烤',    low:false, lv:3}
  ];

  const LEVELS = [
    {time:3.5, count:5, desc:'热身'},
    {time:3.0, count:5, desc:'加速'},
    {time:2.5, count:5, desc:'极限'}
  ];

  let level=1, lives=3, score=0, combo=0, running=false;
  let current=null, qIndex=0, answered=false, usedSet=new Set();
  let rafId=null, qStart=0, qDur=0;

  function updateHUD(){
    scoreEl.textContent = score;
    comboEl.textContent = combo;
    levelEl.textContent = level;
    livesEl.textContent = '❤️'.repeat(lives) + '🖤'.repeat(3-lives);
  }

  function setBar(frac){
    barIn.style.width = Math.max(0, frac*100) + '%';
  }

  function stopTimer(){
    if(rafId) cancelAnimationFrame(rafId);
    rafId = null;
  }

  function startTimer(){
    stopTimer();
    qStart = performance.now();
    const tick = (now)=>{
      if(!running || answered) return;
      const el = now - qStart;
      const frac = Math.max(0, 1 - el/qDur);
      setBar(frac);
      if(el >= qDur){ judge(null); return; }
      rafId = requestAnimationFrame(tick);
    };
    rafId = requestAnimationFrame(tick);
  }

  function pickItem(){
    const pool = ITEMS.filter(it => it.lv <= level);
    const avail = pool.filter(it => !usedSet.has(it.n));
    if(avail.length === 0){ usedSet.clear(); return pickItem(); }
    const it = avail[Math.floor(Math.random()*avail.length)];
    usedSet.add(it.n);
    return it;
  }

  function showQuestion(){
    if(!running) return;
    answered = false;
    lowBtn.disabled = false;
    highBtn.disabled = false;

    current = pickItem();
    card.querySelector('.emoji').textContent = current.e;
    card.querySelector('.name').textContent  = current.n;
    card.querySelector('.tip').textContent   = '';
    card.style.display = 'block';
    card.style.animation = 'none';
    void card.offsetWidth;
    card.style.animation = 'pop .25s ease-out';

    qDur = LEVELS[level-1].time * 1000;
    setBar(1);
    startTimer();
  }

  function judge(answer){
    if(!running || answered) return;
    answered = true;
    stopTimer();
    lowBtn.disabled = true;
    highBtn.disabled = true;

    if(answer === null){
      lives--; combo = 0;
      card.querySelector('.tip').textContent =
        '⏰ 超时!这是「' + (current.low?'低碳':'高碳') + '」行为,-1❤️';
    } else if(answer === current.low){
      combo++;
      const gain = combo >= 3 ? 15 : 10;
      score += gain;
      card.querySelector('.tip').textContent =
        '✅ 正确!+' + gain + '分' + (combo>=3?'(连击奖励)':'');
    } else {
      lives--; combo = 0;
      card.querySelector('.tip').textContent =
        '❌ 错误!这是「' + (current.low?'低碳':'高碳') + '」行为,-1❤️';
    }
    updateHUD();

    if(lives <= 0){ setTimeout(gameOver, 650); return; }

    qIndex++;
    if(qIndex >= LEVELS[level-1].count){
      setTimeout(levelComplete, 700);
    } else {
      setTimeout(showQuestion, 700);
    }
  }

  function levelComplete(){
    stopTimer();
    running = false;
    card.style.display = 'none';

    if(level >= LEVELS.length){ victory(); return; }

    overlay.innerHTML =
      '<h2>🎉 第 ' + level + ' 关完成!</h2>' +
      '<p>当前得分:<b style="color:#2e9e6b">' + score + '</b></p >' +
      '<p>剩余生命:' + '❤️'.repeat(lives) + '</p >' +
      '<p>下一关:<b>' + LEVELS[level].desc + '</b>,每题时间缩短到 ' + LEVELS[level].time + ' 秒!</p >' +
      '<button id="nextBtn">进入第 ' + (level+1) + ' 关</button>';
    overlay.classList.remove('hide');
    document.getElementById('nextBtn').onclick = ()=>{ level++; startLevel(); };
  }

  function victory(){
    running = false;
    card.style.display = 'none';
    overlay.innerHTML =
      '<h2>🏆 恭喜通关!</h2>' +
      '<p>最终得分:<b style="font-size:26px;color:#2e9e6b">' + score + '</b> 分</p >' +
      '<p>剩余生命:' + '❤️'.repeat(lives) + '</p >' +
      '<p>你已经是低碳生活小专家啦!</p >' +
      '<p style="font-size:12px;color:#888;margin-top:10px">小知识:低碳生活从随手关灯、拧紧水龙头、绿色出行开始。</p >' +
      '<button id="againBtn">再玩一次</button>';
    overlay.classList.remove('hide');
    document.getElementById('againBtn').onclick = startGame;
  }

  function gameOver(){
    running = false;
    stopTimer();
    card.style.display = 'none';
    overlay.innerHTML =
      '<h2>💔 生命耗尽</h2>' +
      '<p>最终得分:<b style="font-size:26px;color:#e05a4f">' + score + '</b> 分</p >' +
      '<p>坚持到了第 <b>' + level + '</b> 关</p >' +
      '<p>别灰心,再来一次吧!</p >' +
      '<button id="againBtn">重新挑战</button>';
    overlay.classList.remove('hide');
    document.getElementById('againBtn').onclick = startGame;
  }

  function startLevel(){
    qIndex = 0;
    usedSet.clear();
    combo = 0;
    running = true;
    updateHUD();
    overlay.classList.add('hide');
    showQuestion();
  }

  function startGame(){
    level = 1;
    lives = 3;
    score = 0;
    combo = 0;
    startLevel();
  }

  lowBtn.onclick  = ()=>{ if(running && !answered) judge(true); };
  highBtn.onclick = ()=>{ if(running && !answered) judge(false); };
  startBtn.onclick = startGame;

  updateHUD();
})();
</script>
</body>
</html>

Game Source: 低碳闯关挑战

Creator: PrismDolphin72

Libraries: none

Complexity: complex (293 lines, 11.0 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: game-prismdolphin72-mwf3lcyd" to link back to the original. Then publish at arcadelab.ai/publish.