down100 / snake.html
ymo2017's picture
evolve: 花朵摇摆反馈/青苔滋养增殖/流星山头余光(2026-09-22)
35f243b verified
Raw History Blame Contribute Delete
58.2 kB
<!doctype html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1"> <title>贪吃蛇</title> <link rel="icon" href="favicon.svg" type="image/svg+xml"> <link rel="apple-touch-icon" href="apple-touch-icon.png"> <style> *{box-sizing:border-box;-webkit-tap-highlight-color:transparent} html,body{margin:0;height:100%;background:#101a0c;overflow:hidden} body{display:flex;align-items:center;justify-content:center; background:radial-gradient(1200px 700px at 50% 30%,#2a3d1e 0%,#101a0c 75%)} body.touch{flex-direction:column;justify-content:flex-start;padding-top:8px;overflow-y:auto} canvas{max-height:92vh;max-width:96vw; border:2px solid #6b4a26;box-shadow:0 0 40px rgba(20,40,10,.5),0 0 140px rgba(0,0,0,.6)} #stage{position:relative;line-height:0} #retryBtn{position:absolute;left:50%;bottom:10%;transform:translateX(-50%);display:none;z-index:5; font:700 18px "PingFang SC","Microsoft YaHei",monospace;letter-spacing:.3em;text-indent:.3em; color:#0d2608;background:linear-gradient(180deg,#a8e063,#56ab2f);border:0;border-radius:999px; padding:14px 36px;cursor:pointer;box-shadow:0 4px 18px rgba(40,90,20,.5);touch-action:manipulation} #retryBtn.show{display:block} #retryBtn:active{transform:translateX(-50%) scale(.96)} #pauseBtn{display:none;position:absolute;right:10px;top:10px;z-index:5; width:44px;height:44px;border-radius:12px;border:1px solid #6b4a26; background:rgba(255,255,255,.10);color:#f0e8d0;font-size:18px;touch-action:manipulation} body.touch #pauseBtn{display:flex;align-items:center;justify-content:center} #pauseBtn svg{width:22px;height:22px;display:block} </style> </head> <body> <div id="stage"> <canvas id="game" width="420" height="520"></canvas> <button id="retryBtn" type="button">再来一局</button> <button id="pauseBtn" type="button" aria-label="暂停"><svg viewBox="0 0 24 24" aria-hidden="true"><defs><linearGradient id="snk-pg" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="#d6f2b4"/><stop offset="1" stop-color="#4c9a5f"/></linearGradient></defs><path d="M11 3c3 2 4 6 3.4 9.6C13.4 18 9 21 5 21c.6-4 2.4-7 5-8.6" fill="#7cc47f" opacity=".55"/><rect x="5.2" y="4" width="5" height="16" rx="2.5" fill="url(#snk-pg)"/><rect x="13.8" y="4" width="5" height="16" rx="2.5" fill="url(#snk-pg)"/><circle cx="18.6" cy="5.6" r="2.1" fill="#ffd9e8"/><circle cx="18.6" cy="5.6" r=".85" fill="#ff8fb3"/></svg></button> </div> <script> 'use strict'; /* ================= 配置 ================= */ const COLS=21, ROWS=21, CELL=20; const OX=0, OY=100; // 棋盘左上角:canvas 420x520,顶部 100px 留给 HUD const canvas=document.getElementById('game'); const ctx=canvas.getContext('2d'); const retryBtn=document.getElementById('retryBtn'); const pauseBtn=document.getElementById('pauseBtn'); const IS_TOUCH=('ontouchstart' in window)||(navigator.maxTouchPoints>0); if(IS_TOUCH) document.body.classList.add('touch'); const G={ state:'menu', paused:false, snake:[], dir:{x:1,y:0}, queued:null, food:null, score:0, best:0, stepMs:150, acc:0, last:0, flash:0, shake:0, deadAt:0, overT:1, // 死亡结算面板缩放入场计时(纯表现层) nibbles:[], // 吃果处草皮压痕(纯表现层) sprouts:[], // 食物生长动画记录(纯表现层) deadSpot:null, // 死亡压扁草皮痕迹(纯表现层) sheds:[], // 里程碑蛇蜕记录(纯表现层) shedShown:{}, // 已触发的里程碑(纯表现层) bentGrass:[] // 蛇行倾倒草痕(纯表现层:蛇身经过格子留 1.5 秒草痕消退) }; try{ G.best=parseInt(localStorage.getItem('snake_best')||'0',10)||0; }catch(e){} /* 草原美术素材:田野背景 / 午后草甸全景(升级) / 蛇头精灵 / 眼睛分叉舌蛇头(升级) / 鳞片纹理 / 细化鳞片(升级) / 写实苹果 / 红苹果升级 / 金苹果(加载失败逐项回退原矢量绘制) */ const SN_FIELD=new Image(); let SN_FIELD_OK=false; SN_FIELD.onload=()=>{ SN_FIELD_OK=true; }; SN_FIELD.onerror=()=>{ SN_FIELD_OK=false; }; SN_FIELD.src='art/sn-field.webp'; /* 草甸全景背景升级(午后草原全景:午后暖光棋盘草原、泥土斑块、角落水洼;加载失败回退 sn-field,再失败回退矢量网格) */ const SN_MEADOW2=new Image(); let SN_MEADOW2_OK=false; SN_MEADOW2.onload=()=>{ SN_MEADOW2_OK=true; }; SN_MEADOW2.onerror=()=>{ SN_MEADOW2_OK=false; }; SN_MEADOW2.src='art/sn-meadow2.webp'; const SN_HEAD=new Image(); let SN_HEAD_OK=false; SN_HEAD.onload=()=>{ SN_HEAD_OK=true; }; SN_HEAD.onerror=()=>{ SN_HEAD_OK=false; }; SN_HEAD.src='art/sn-head.webp'; /* 蛇头插画升级:大眼+分叉红舌(朝移动方向;加载失败回退旧 sn-head,再失败回退矢量眼) */ const SN_HEAD2=new Image(); let SN_HEAD2_OK=false; SN_HEAD2.onload=()=>{ SN_HEAD2_OK=true; }; SN_HEAD2.onerror=()=>{ SN_HEAD2_OK=false; }; SN_HEAD2.src='art/sn-head2.webp'; const SN_SCALE=new Image(); let SN_SCALE_OK=false; SN_SCALE.onload=()=>{ SN_SCALE_OK=true; }; SN_SCALE.onerror=()=>{ SN_SCALE_OK=false; }; SN_SCALE.src='art/sn-scale.webp'; let SN_SCALE_PAT=null; /* 蛇身鳞片纹理细化版(细密叠瓦状绿鳞,蛇身分段贴;加载失败回退旧 sn-scale) */ const SN_SCALE2=new Image(); let SN_SCALE2_OK=false; SN_SCALE2.onload=()=>{ SN_SCALE2_OK=true; }; SN_SCALE2.onerror=()=>{ SN_SCALE2_OK=false; }; SN_SCALE2.src='art/sn-scale2.webp'; let SN_SCALE2_PAT=null; /* 蛇头细鳞片纹理(纯表现层:只叠在蛇头上,细密弧片,与蛇尾磨损衔接;加载失败跳过,不改玩法/碰撞) */ const SN_SCALE_FINE=new Image(); let SN_SCALE_FINE_OK=false; SN_SCALE_FINE.onload=()=>{ SN_SCALE_FINE_OK=true; }; SN_SCALE_FINE.onerror=()=>{ SN_SCALE_FINE_OK=false; }; SN_SCALE_FINE.src='art/sn-scale-fine.webp'; let SN_SCALE_FINE_PAT=null; /* 高速排汗湿光(纯表现层:stepMs<120 蛇头/前节叠加湿润高光;加载失败跳过,不改速度/碰撞) */ const SN_SWEAT=new Image(); let SN_SWEAT_OK=false; SN_SWEAT.onload=()=>{ SN_SWEAT_OK=true; }; SN_SWEAT.onerror=()=>{ SN_SWEAT_OK=false; }; SN_SWEAT.src='art/sn-sweat.webp'; const SN_APPLE=new Image(); let SN_APPLE_OK=false; SN_APPLE.onload=()=>{ SN_APPLE_OK=true; }; SN_APPLE.onerror=()=>{ SN_APPLE_OK=false; }; SN_APPLE.src='art/sn-apple.webp'; /* 红苹果插画升级版(加载失败回退旧 sn-apple,再失败回退矢量苹果) */ const SN_APPLE2=new Image(); let SN_APPLE2_OK=false; SN_APPLE2.onload=()=>{ SN_APPLE2_OK=true; }; SN_APPLE2.onerror=()=>{ SN_APPLE2_OK=false; }; SN_APPLE2.src='art/sn-apple2.webp'; /* 金苹果插画(纯表现层视觉变体:18% 概率刷出金苹果,得分/玩法完全不变;加载失败回退红苹果) */ const SN_APPLE3=new Image(); let SN_APPLE3_OK=false; SN_APPLE3.onload=()=>{ SN_APPLE3_OK=true; }; SN_APPLE3.onerror=()=>{ SN_APPLE3_OK=false; }; SN_APPLE3.src='art/sn-apple3.webp'; /* 蛇尾磨损鳞片贴图(纯表现层:尾部节叠加磨损环纹,加载失败则只保留渐进加深,不影响玩法) */ const SN_TAILWORN=new Image(); let SN_TAILWORN_OK=false; SN_TAILWORN.onload=()=>{ SN_TAILWORN_OK=true; }; SN_TAILWORN.onerror=()=>{ SN_TAILWORN_OK=false; }; SN_TAILWORN.src='art/sn-tail-worn.webp'; /* 里程碑蛇蜕贴图(纯表现层:蛇长到 15/25/35 节时留下的蛇蜕,加载失败跳过,不改玩法/碰撞) */ const SN_SHED=new Image(); let SN_SHED_OK=false; SN_SHED.onload=()=>{ SN_SHED_OK=true; }; SN_SHED.onerror=()=>{ SN_SHED_OK=false; }; SN_SHED.src='art/sn-shed.webp'; /* 蛇蜕沾附草叶贴图(纯表现层:蛇蜕上沾几片草叶、随蛇蜕 5 秒消退,加载失败跳过,不改玩法/碰撞) */ const SN_GRASS=new Image(); let SN_GRASS_OK=false; SN_GRASS.onload=()=>{ SN_GRASS_OK=true; }; SN_GRASS.onerror=()=>{ SN_GRASS_OK=false; }; SN_GRASS.src='art/sn-grassblade.webp'; /* 草甸装饰贴图(纯表现层:加载失败则跳过装饰绘制,玩法/背景不受影响) */ const SN_DECOR=new Image(); let SN_DECOR_OK=false; SN_DECOR.onload=()=>{ SN_DECOR_OK=true; }; SN_DECOR.onerror=()=>{ SN_DECOR_OK=false; }; SN_DECOR.src='art/sn-decor.webp'; /* 草地细草叶噪点贴图(纯表现层:叠在 sn-field 上打破大面积色块平整感,加载失败跳过) */ const SN_NOISE=new Image(); let SN_NOISE_OK=false; SN_NOISE.onload=()=>{ SN_NOISE_OK=true; }; SN_NOISE.onerror=()=>{ SN_NOISE_OK=false; }; SN_NOISE.src='art/sn-grass-noise.webp'; /* 蛇行倾倒草叶贴图(纯表现层:蛇身经过的格子叠加 1.5 秒淡出的倾倒草叶,加载失败自动跳过绘制,不改玩法/碰撞) */ const SN_BENT=new Image(); let SN_BENT_OK=false; SN_BENT.onload=()=>{ SN_BENT_OK=true; }; SN_BENT.onerror=()=>{ SN_BENT_OK=false; }; SN_BENT.src='art/sn-grass-bent.webp'; /* 金苹果金色草皮光斑(纯表现层:金苹果刷新处草皮淡金色光斑、随存活时间呼吸明灭,红果无光斑;加载失败自动跳过,不改玩法/碰撞) */ const SN_GOLDPATCH=new Image(); let SN_GOLDPATCH_OK=false; SN_GOLDPATCH.onload=()=>{ SN_GOLDPATCH_OK=true; }; SN_GOLDPATCH.onerror=()=>{ SN_GOLDPATCH_OK=false; }; SN_GOLDPATCH.src='art/sn-goldpatch.webp'; /* 金苹果成熟度红晕(纯表现层:存活越久果面红晕越重(20秒封顶),成熟度图像化;加载失败跳过,不改玩法/碰撞) */ const SN_BLUSH=new Image(); let SN_BLUSH_OK=false; SN_BLUSH.onload=()=>{ SN_BLUSH_OK=true; }; SN_BLUSH.onerror=()=>{ SN_BLUSH_OK=false; }; SN_BLUSH.src='art/sn-blush.webp'; /* 金苹果果蒂叶贴图(纯表现层:金苹果顶部叠加果蒂叶贴图、随苹果脉冲同呼吸,加载失败跳过、红果不画;不改玩法/碰撞) */ const SN_LEAF=new Image(); let SN_LEAF_OK=false; SN_LEAF.onload=()=>{ SN_LEAF_OK=true; }; SN_LEAF.onerror=()=>{ SN_LEAF_OK=false; }; SN_LEAF.src='art/sn-leaf.webp'; /* 棋盘边界灌木篱贴图(纯表现层:灌木篱纹理带沿棋盘四条边界线铺贴;加载失败跳过,原 strokeRect 边界线保留;不改玩法/碰撞) */ const SN_HEDGE=new Image(); let SN_HEDGE_OK=false; SN_HEDGE.onload=()=>{ SN_HEDGE_OK=true; }; SN_HEDGE.onerror=()=>{ SN_HEDGE_OK=false; }; SN_HEDGE.src='art/sn-hedge.webp'; /* 灌木篱花朵开放差异化(纯表现层:灌木篱上叠加白/黄小花贴图、随时间呼吸开合,打破篱带匀质感;加载失败跳过,不改玩法/碰撞) */ const SN_HFL=new Image(); let SN_HFL_OK=false; SN_HFL.onload=()=>{ SN_HFL_OK=true; }; SN_HFL.onerror=()=>{ SN_HFL_OK=false; }; SN_HFL.src='art/sn-hedge-flowers.webp'; /* 花朵摇摆触发时间戳(模块级:tileKey -> 最近一次触发时间;蛇头在旁逗留时每 0.5s 补一次脉冲,离开后 0.5s 衰退) */ const FLOWER_SWAY_T={}; /* 蒲公英绒伞随风漂移场景层(纯表现层:4 朵绒伞背景上层随时间横向漂移循环、纯时间驱动无状态;加载失败跳过;不改玩法/碰撞) */ const SN_FLUFF=new Image(); let SN_FLUFF_OK=false; SN_FLUFF.onload=()=>{ SN_FLUFF_OK=true; }; SN_FLUFF.onerror=()=>{ SN_FLUFF_OK=false; }; SN_FLUFF.src='art/sn-fluff.webp'; const SN_DECOR_REGIONS=[ // 512x512 图集:3列x2行,每区150x150(白花/石块/草丛/黄花/碎石/深草) {x:10,y:10,w:150,h:150},{x:176,y:10,w:150,h:150},{x:342,y:10,w:150,h:150}, {x:10,y:176,w:150,h:150},{x:176,y:176,w:150,h:150},{x:342,y:176,w:150,h:150} ]; /* 花卉变体图集(纯表现层:透明底手绘蒲公英/花丛/紫花,色调融入草原黄绿;加载失败回退主图集) */ const SN_DECOR2=new Image(); let SN_DECOR2_OK=false; SN_DECOR2.onload=()=>{ SN_DECOR2_OK=true; }; SN_DECOR2.onerror=()=>{ SN_DECOR2_OK=false; }; SN_DECOR2.src='art/sn-decor2.webp'; const SN_DECOR2_REGIONS=[ // 512x176 图集:一排 3 区(蒲公英/花丛/紫花) {x:10,y:10,w:150,h:150},{x:176,y:10,w:150,h:150},{x:342,y:10,w:150,h:150} ]; /* 花丛水洼边饰图集(纯表现层:透明底手绘白花丛/芦苇水洼边/紫花丛,色调融入午后草原;加载失败回退主图集) */ const SN_DECOR3=new Image(); let SN_DECOR3_OK=false; SN_DECOR3.onload=()=>{ SN_DECOR3_OK=true; }; SN_DECOR3.onerror=()=>{ SN_DECOR3_OK=false; }; SN_DECOR3.src='art/sn-decor3.webp'; const SN_DECOR3_REGIONS=[ // 1200x400 图集:一排 3 区(白花丛/芦苇水洼/紫花丛) {x:0,y:0,w:400,h:400},{x:400,y:0,w:400,h:400},{x:800,y:0,w:400,h:400} ]; /* 分区密度差异化权重:k=0..5 主图集,k=6..8 花卉图集,k=9..11 水洼边饰图集;亮区花聚、暗区石多 */ const DECOR_ZONE_W=[ /* 亮区(上 36%) */ [1.6,0.5,1.2,1.6,0.5,1.0, 3.2,3.0,2.8, 2.4,2.0,1.8], /* 中区 */ [1.2,1.0,2.0,1.2,1.0,1.6, 1.4,1.2,1.0, 1.0,1.4,1.0], /* 暗区(下 26%) */ [0.7,2.2,1.8,0.7,2.4,1.6, 0.7,0.6,0.5, 0.5,0.9,0.5] ]; function decorZonePick(zone){ const w=DECOR_ZONE_W[zone]; let sum=0; for(let i=0;i<w.length;i++) sum+=w[i]; let r=Math.random()*sum; for(let i=0;i<w.length;i++){ r-=w[i]; if(r<=0) return i; } return 2; } function decorLayout(){ const spots=[]; const zones=[{y0:0.0,y1:0.36,n0:4,n1:6},{y0:0.36,y1:0.74,n0:2,n1:4},{y0:0.74,y1:1.0,n0:1,n1:3}]; const ySpan=ROWS*CELL-96; const sx0=Math.floor(COLS/2), sy0=Math.floor(ROWS/2); zones.forEach((z,zi)=>{ const want=z.n0+((Math.random()*(z.n1-z.n0+1))|0); // 每区分区数量,打破匀质感 let guard=0,added=0; while(added<want&&guard++<300){ const x=OX+26+Math.random()*(COLS*CELL-52); const y=OY+56+(z.y0+Math.random()*(z.y1-z.y0))*ySpan; // 分区播种 const cx=Math.floor((x-OX)/CELL), cy=Math.floor((y-OY)/CELL); if(Math.abs(cx-sx0)<=3&&Math.abs(cy-sy0)<=3) continue; // 避开蛇出生区 let clash=false; for(const s of spots){ const dx=s.x-x,dy=s.y-y; if(dx*dx+dy*dy<64*64){ clash=true; break; } } if(clash) continue; spots.push({k:decorZonePick(zi),x:x,y:y, s:30+Math.random()*22, flip:Math.random()<0.5}); added++; } }); return spots; } /* 吃到食物的爆炸粒子 + 冲击波环(不改玩法,只加视觉反馈) */ const parts=[], rings=[]; function burst(cx,cy){ const cols=['#ff5c78','#ff9ec4','#ffd93b','#ff4d6d','#ffffff']; for(let i=0;i<16;i++){ const a=Math.random()*Math.PI*2, sp=60+Math.random()*160; parts.push({x:cx,y:cy,vx:Math.cos(a)*sp,vy:Math.sin(a)*sp-40, life:0.45+Math.random()*0.25,t:0,r:1.5+Math.random()*2.5, c:cols[(Math.random()*cols.length)|0]}); } rings.push({x:cx,y:cy,r:4,maxR:CELL*1.6,t:0,life:0.35}); } /* 成就:首次通关 / 破纪录(localStorage arcade_achv) */ /* 成就 toast:破纪录弹"新纪录!",首次通关弹 🎖️ 徽章动画(队列依次展示,不改玩法) */ (function(){if(document.getElementById('achvToastStyle'))return;var s=document.createElement('style');s.id='achvToastStyle';s.textContent='.achv-toast{position:fixed;top:12%;left:50%;transform:translate(-50%,-16px) scale(.92);padding:10px 24px;border-radius:14px;font-size:20px;font-weight:800;letter-spacing:1px;z-index:99999;opacity:0;transition:opacity .28s ease,transform .28s ease;pointer-events:none;background:#f2e7c8;border:3px solid #6b4a26;box-shadow:0 4px 16px rgba(30,50,15,.5);color:#3d5a1e;white-space:nowrap;font-family:inherit}.achv-toast.show{opacity:1;transform:translate(-50%,0) scale(1)}';document.head.appendChild(s)})(); var _achvQ=[],_achvBusy=false; function showAchvToast(kind){_achvQ.push(kind);pumpAchvToast()} function pumpAchvToast(){if(_achvBusy||!_achvQ.length)return;_achvBusy=true;try{var kind=_achvQ.shift(),t=document.getElementById('achvToast');if(!t){t=document.createElement('div');t.id='achvToast';t.className='achv-toast';document.body.appendChild(t)}t.textContent=(kind==='clear')?'🎖️ 首次通关!':'🎉 新纪录!';void t.offsetWidth;t.classList.add('show');setTimeout(function(){t.classList.remove('show');setTimeout(function(){_achvBusy=false;pumpAchvToast()},320)},2100)}catch(_){_achvBusy=false}} function arcadeAchv(kind,id){var isNew=false;try{var K='arcade_achv',d={};try{d=JSON.parse(localStorage.getItem(K)||'{}')||{}}catch(_){}var g=d['snake'];if(!g)g=d['snake']={records:0,clears:[]};if(kind==='record'){g.records=(g.records||0)+1;isNew=true}else if(id&&g.clears.indexOf(id)<0){g.clears.push(id);isNew=true}localStorage.setItem(K,JSON.stringify(d))}catch(_){}if(isNew)showAchvToast(kind)} /* ================= 音频(Web Audio 合成,首次交互初始化) ================= */ let AC=null; function audio(){ if(!AC){ try{ AC=new (window.AudioContext||window.webkitAudioContext)(); }catch(e){ return null; } } if(AC && AC.state==='suspended') AC.resume(); return AC; } function tone(freq,dur,opt){ const ac=audio(); if(!ac) return; opt=opt||{}; const t=ac.currentTime+(opt.delay||0); const o=ac.createOscillator(), g=ac.createGain(); o.type=opt.type||'square'; o.frequency.setValueAtTime(freq,t); if(opt.slide) o.frequency.exponentialRampToValueAtTime(opt.slide,t+dur); g.gain.setValueAtTime(0.0001,t); g.gain.exponentialRampToValueAtTime(opt.vol||0.08,t+0.008); g.gain.exponentialRampToValueAtTime(0.0001,t+dur); o.connect(g); g.connect(ac.destination); o.start(t); o.stop(t+dur+0.02); } const SFX={ eat(){ tone(520,0.07,{vol:0.10,slide:880}); tone(880,0.06,{vol:0.08,delay:0.05,slide:1320}); }, die(){ tone(300,0.35,{vol:0.12,type:'sawtooth',slide:60}); }, turn(){ tone(260,0.04,{vol:0.05}); } }; /* ================= 游戏逻辑 ================= */ function reset(){ const cx=Math.floor(COLS/2), cy=Math.floor(ROWS/2); G.snake=[{x:cx,y:cy},{x:cx-1,y:cy},{x:cx-2,y:cy}]; G.headTrail=[]; // 蛇头拖尾历史(纯表现层) G.dir={x:1,y:0}; G.queued=null; G.score=0; G.stepMs=150; G.acc=0; G.paused=false; G.flash=0; G.shake=0; G.overT=1; G.headGlowT=0; G.nibbles=[]; G.sprouts=[]; G.deadSpot=null; G.sheds=[]; G.shedShown={}; G.bentGrass=[]; G.decor=decorLayout(); // 每局重新随机摆装饰(纯表现层,静态,不改玩法/碰撞) retryBtn.classList.remove('show'); spawnFood(); } function start(){ reset(); G.state='playing'; } function spawnFood(){ const taken=new Set(G.snake.map(s=>s.x+','+s.y)); const free=[]; for(let y=0;y<ROWS;y++) for(let x=0;x<COLS;x++) if(!taken.has(x+','+y)) free.push({x,y}); if(!free.length){ G.food=null; return; } G.food=free[(Math.random()*free.length)|0]; G.food.gold=Math.random()<0.18; // 金苹果:纯表现层视觉变体,得分/速度/碰撞完全不变 G.food.bornT=performance.now(); // 食物出生时间(纯表现层:金苹果成熟度红晕计时) /* 新食物涟漪提示(纯表现层:刷新处扩散小光圈 0.6 秒后消退,不改玩法/碰撞) */ const cx0=OX+G.food.x*CELL+CELL/2, cy0=OY+G.food.y*CELL+CELL/2; rings.push({x:cx0,y:cy0,r:4,maxR:CELL*1.35,t:0,life:0.6,c:G.food.gold?'255,215,106':'255,255,220'}); G.sprouts.push({x:cx0,y:cy0,t:0,life:300}); // 食物生长动画:"从草丛里冒出来"(纯表现层,0.3 秒长满后消退,不改刷怪逻辑) } function setDir(x,y){ if(G.state!=='playing'||G.paused) return; const cur=G.queued||G.dir; if(x===-cur.x&&y===-cur.y) return; // 禁止 180° 掉头 if(x===cur.x&&y===cur.y) return; G.queued={x,y}; SFX.turn(); } function step(){ if(G.queued){ G.dir=G.queued; G.queued=null; } const head={x:G.snake[0].x+G.dir.x, y:G.snake[0].y+G.dir.y}; if(head.x<0||head.y<0||head.x>=COLS||head.y>=ROWS|| G.snake.some(s=>s.x===head.x&&s.y===head.y)){ G.state='over'; G.deadAt=performance.now(); G.overT=0; // 结算面板缩放入场动画从 0 开始 retryBtn.classList.add('show'); if(G.score>G.best){ G.best=G.score; try{ localStorage.setItem('snake_best',String(G.best)); }catch(e){} arcadeAchv('record'); } G.flash=1; G.shake=1; burst(OX+head.x*CELL+CELL/2, OY+head.y*CELL+CELL/2); const dclx=OX+Math.min(Math.max(head.x,0),COLS-1)*CELL+CELL/2; // 撞墙死亡时头部坐标越界,钳制到棋盘内 const dcly=OY+Math.min(Math.max(head.y,0),ROWS-1)*CELL+CELL/2; G.deadSpot={x:dclx,y:dcly,t:0,life:4000}; // 死亡压扁草皮痕迹(纯表现层,4 秒消退,图像化死亡反馈,不改逻辑) SFX.die(); return; } G.snake.unshift(head); G.headTrail.push({x:OX+head.x*CELL+CELL/2,y:OY+head.y*CELL+CELL/2}); // 蛇头拖尾记录 if(G.headTrail.length>10) G.headTrail.shift(); G.bentGrass.push({x:head.x,y:head.y,t:0,life:1500}); // 蛇身经过格留倾倒草痕(纯表现层,1.5 秒消退;dt 为毫秒) if(G.bentGrass.length>200) G.bentGrass.shift(); if(G.food&&head.x===G.food.x&&head.y===G.food.y){ G.score+=10; G.stepMs=Math.max(70,150-G.score/10*4); // 吃得越多越快 G.headGlowT=performance.now(); // 吃红果加速时蛇头短暂发亮(纯表现层,不改速度/碰撞) burst(OX+G.food.x*CELL+CELL/2, OY+G.food.y*CELL+CELL/2); G.nibbles.push({x:OX+G.food.x*CELL+CELL/2,y:OY+G.food.y*CELL+CELL/2,t:0,life:2500}); // 被啃的草皮压痕(纯表现层,2.5s 消退;dt 为毫秒) /* 里程碑蛇蜕(纯表现层:蛇长到 15/25/35 节时在当前位置留一张蛇蜕贴图,5 秒消退,不改玩法/碰撞) */ for(const _m of [15,25,35]){ if(!G.shedShown[_m]&&G.snake.length>=_m){ G.shedShown[_m]=1; G.sheds.push({x:OX+head.x*CELL+CELL/2,y:OY+head.y*CELL+CELL/2,t:0,life:5000,rot:(Math.random()-0.5)*0.7}); } } G.flash=1; SFX.eat(); spawnFood(); }else{ G.snake.pop(); } } function update(dt){ /* 粒子/冲击波/闪屏/震屏的衰减不依赖 playing 状态(死亡瞬间也要播完) */ if(G.flash>0) G.flash=Math.max(0,G.flash-dt/300); if(G.shake>0) G.shake=Math.max(0,G.shake-dt/400); if(G.state==='over'&&G.overT<1) G.overT=Math.min(1,G.overT+dt/320); // 死亡结算面板缩放入场(纯表现层) for(let i=parts.length-1;i>=0;i--){ const p=parts[i]; p.t+=dt/1000; if(p.t>=p.life){ parts.splice(i,1); continue; } p.x+=p.vx*dt/1000; p.y+=p.vy*dt/1000; p.vy+=300*dt/1000; } for(let i=rings.length-1;i>=0;i--){ const r=rings[i]; r.t+=dt/1000; r.r=r.maxR*(r.t/r.life); if(r.t>=r.life) rings.splice(i,1); } for(let i=G.nibbles.length-1;i>=0;i--){ G.nibbles[i].t+=dt; if(G.nibbles[i].t>=G.nibbles[i].life) G.nibbles.splice(i,1); } // 草皮压痕消退(纯表现层) for(let i=G.sprouts.length-1;i>=0;i--){ G.sprouts[i].t+=dt; if(G.sprouts[i].t>=G.sprouts[i].life) G.sprouts.splice(i,1); } // 食物生长消退(纯表现层) if(G.deadSpot){ G.deadSpot.t+=dt; if(G.deadSpot.t>=G.deadSpot.life) G.deadSpot=null; } // 死亡压痕消退(纯表现层) for(let i=G.sheds.length-1;i>=0;i--){ G.sheds[i].t+=dt; if(G.sheds[i].t>=G.sheds[i].life) G.sheds.splice(i,1); } // 蛇蜕消退(纯表现层) for(let i=G.bentGrass.length-1;i>=0;i--){ G.bentGrass[i].t+=dt; if(G.bentGrass[i].t>=G.bentGrass[i].life) G.bentGrass.splice(i,1); } // 倾倒草痕消退(纯表现层,1.5 秒) if(G.state!=='playing'||G.paused) return; G.acc+=dt; while(G.acc>=G.stepMs){ G.acc-=G.stepMs; step(); if(G.state!=='playing') break; } } /* ================= 渲染 ================= */ function rr(x,y,w,h,r){ ctx.beginPath(); ctx.moveTo(x+r,y); ctx.arcTo(x+w,y,x+w,y+h,r); ctx.arcTo(x+w,y+h,x,y+h,r); ctx.arcTo(x,y+h,x,y,r); ctx.arcTo(x,y,x+w,y,r); ctx.closePath(); } function draw(){ ctx.clearRect(0,0,canvas.width,canvas.height); ctx.save(); if(G.shake>0) ctx.translate((Math.random()-0.5)*8*G.shake,(Math.random()-0.5)*8*G.shake); // 背景:午后草甸全景(加载失败回退旧 sn-field,再失败回退深色网格) const _bgImg=(SN_MEADOW2_OK&&SN_MEADOW2.naturalWidth)?SN_MEADOW2 :((SN_FIELD_OK&&SN_FIELD.naturalWidth)?SN_FIELD:null); if(_bgImg){ ctx.drawImage(_bgImg,0,0,canvas.width,canvas.height); window.__snakeBgDrawn=(window.__snakeBgDrawn||0)+1; // 探针:新背景确实绘制 ctx.strokeStyle='#4a2f14'; ctx.lineWidth=3; ctx.strokeRect(OX+1,OY+1,COLS*CELL-2,ROWS*CELL-2); /* 棋盘边界灌木篱场景层(纯表现层:灌木篱贴图沿四条边界线铺贴;加载失败跳过;不改玩法/碰撞) */ if(SN_HEDGE_OK&&SN_HEDGE.naturalWidth){ window.__snakeHedgeDraws=(window.__snakeHedgeDraws||0); const HH=14, tileW=256; const nx=Math.ceil(COLS*CELL/tileW), ny=Math.ceil(ROWS*CELL/tileW); for(let i=0;i<nx;i++){ const w=Math.min(tileW,COLS*CELL-i*tileW); ctx.drawImage(SN_HEDGE,i*tileW,0,w,32,OX+i*tileW,OY-HH/2,w,HH); ctx.drawImage(SN_HEDGE,i*tileW,0,w,32,OX+i*tileW,OY+ROWS*CELL-HH/2,w,HH); window.__snakeHedgeDraws+=2; } for(const ex of [OX-HH/2,OX+COLS*CELL-HH/2]){ ctx.save(); ctx.translate(ex,OY); ctx.rotate(Math.PI/2); for(let i=0;i<ny;i++){ const h=Math.min(tileW,ROWS*CELL-i*tileW); ctx.drawImage(SN_HEDGE,i*tileW,0,h,32,i*tileW,-HH/2,h,HH); window.__snakeHedgeDraws++; } ctx.restore(); } /* 灌木篱花朵开放差异化(纯表现层:灌木篱贴图上叠加白/黄小花、随时间呼吸开合;加载失败跳过,不改玩法/碰撞) */ /* 花朵随蛇经过轻微摇摆反馈(纯表现层:蛇头进入某块灌木篱花朵区 2 格内时该块花朵摇摆 0.5s(旋转±0.08 衰减),环境互动感;纯时间/距离驱动;不改玩法/碰撞) */ if(SN_HFL_OK&&SN_HFL.naturalWidth&&G.snake&&G.snake.length){ const _hx=G.snake[0].x,_hy=G.snake[0].y,_swNow=performance.now(); const _cd=(x0,x1,y0,y1)=>Math.max(0,x0-_hx,_hx-x1,y0-_hy,_hy-y1); // 蛇头到格矩形的切比雪夫距离 const _trg=(key)=>{ if(FLOWER_SWAY_T[key]===undefined||_swNow-FLOWER_SWAY_T[key]>500) FLOWER_SWAY_T[key]=_swNow; }; // 每 0.5s 最多补一脉冲 for(let i=0;i<nx;i++){ const w=Math.min(tileW,COLS*CELL-i*tileW),x0=i*tileW/CELL,x1=(i*tileW+w)/CELL; if(_cd(x0,x1,-1,1)<=2)_trg('T'+i); if(_cd(x0,x1,ROWS-1,ROWS+1)<=2)_trg('B'+i); } for(let i=0;i<ny;i++){ const h=Math.min(tileW,ROWS*CELL-i*tileW),y0=i*tileW/CELL,y1=(i*tileW+h)/CELL; if(_cd(-1,1,y0,y1)<=2)_trg('L'+i); if(_cd(COLS-1,COLS+1,y0,y1)<=2)_trg('R'+i); } } if(SN_HFL_OK&&SN_HFL.naturalWidth){ window.__snakeHedgeFlowerDraws=(window.__snakeHedgeFlowerDraws||0); window.__snakeFlowerSwayN=0; // 探针:本帧摇摆中的花朵块数 const _ft=performance.now()/1000,_swT=performance.now(); const _swayRot=(key)=>{const t0=FLOWER_SWAY_T[key];if(t0===undefined)return 0;const a=(_swT-t0)/500;if(a>=1)return 0;window.__snakeFlowerSwayN++;return 0.08*Math.sin(a*Math.PI*3)*(1-a);}; for(let i=0;i<nx;i++){ const w=Math.min(tileW,COLS*CELL-i*tileW); const _ph=_ft/1.4+i*1.7; const _s=1+0.16*Math.sin(_ph); // 开合呼吸:纵向缩放 const _dh=HH*_s, _dy=HH-_dh; for(const _sd of ['T','B']){ const _yy=_sd==='T'?OY-HH/2+_dy/2:OY+ROWS*CELL-HH/2+_dy/2; const _rot=_swayRot(_sd+i); if(_rot){ // 摇摆中:绕块中心旋转(纯表现层) ctx.save(); ctx.globalAlpha=0.55+0.45*Math.abs(Math.sin(_ph*0.8)); ctx.translate(OX+i*tileW+w/2,_yy+_dh/2); ctx.rotate(_rot); ctx.drawImage(SN_HFL,i*tileW,0,w,32,-w/2,-_dh/2,w,_dh); ctx.restore(); }else{ ctx.save(); ctx.globalAlpha=0.55+0.45*Math.abs(Math.sin(_ph*0.8)); ctx.drawImage(SN_HFL,i*tileW,0,w,32,OX+i*tileW,_yy,w,_dh); ctx.restore(); } window.__snakeHedgeFlowerDraws++; } } for(const [si,ex] of [['L',OX-HH/2],['R',OX+COLS*CELL-HH/2]]){ ctx.save(); ctx.translate(ex,OY); ctx.rotate(Math.PI/2); for(let i=0;i<ny;i++){ const h=Math.min(tileW,ROWS*CELL-i*tileW); const _ph=_ft/1.4+i*2.1+3.7; const _s=1+0.16*Math.sin(_ph); const _dh=HH*_s, _dy=HH-_dh; const _rot=_swayRot(si+i); if(_rot){ ctx.save(); ctx.globalAlpha=0.55+0.45*Math.abs(Math.sin(_ph*0.8)); ctx.translate(i*tileW+h/2,-HH/2+_dy/2+_dh/2); ctx.rotate(_rot); ctx.drawImage(SN_HFL,i*tileW,0,h,32,-h/2,-_dh/2,h,_dh); ctx.restore(); }else{ ctx.save(); ctx.globalAlpha=0.55+0.45*Math.abs(Math.sin(_ph*0.8)); ctx.drawImage(SN_HFL,i*tileW,0,h,32,i*tileW,-HH/2+_dy/2,h,_dh); ctx.restore(); } window.__snakeHedgeFlowerDraws++; } ctx.restore(); } } } }else{ ctx.fillStyle='#050b16'; ctx.fillRect(OX,OY,COLS*CELL,ROWS*CELL); ctx.fillStyle='rgba(34,230,255,.045)'; for(let y=0;y<ROWS;y++) for(let x=0;x<COLS;x++) if((x+y)%2===0) ctx.fillRect(OX+x*CELL,OY+y*CELL,CELL,CELL); ctx.strokeStyle='#12324a'; ctx.lineWidth=2; ctx.strokeRect(OX,OY,COLS*CELL,ROWS*CELL); } /* 蒲公英绒伞随风漂移场景层(纯表现层:4 朵绒伞在背景层之上随时间横向漂移循环、上下微浮动、纯时间驱动无状态;加载失败跳过;不改玩法/碰撞) */ if(SN_FLUFF_OK&&SN_FLUFF.naturalWidth){ window.__snakeFluffDraws=(window.__snakeFluffDraws||0); const _t=performance.now()/1000; const _bw=canvas.width,_bh=canvas.height; ctx.save(); for(let _fi=0;_fi<4;_fi++){ const _spd=14+(_fi*37)%26, _span=_bw+180; const _x=(((_t*_spd)+_fi*_span*0.37)%_span)-90; const _y=110+(((_fi*251)%(_bh-260))+Math.sin(_t*0.7+_fi*2.1)*22+((_t*9)%1)); const _sz=44+((_fi*53)%26); ctx.save(); ctx.translate(_x,_y); ctx.rotate(Math.sin(_t*0.5+_fi*1.7)*0.35); ctx.globalAlpha=0.52+0.18*Math.sin(_t*1.1+_fi*2.9); ctx.drawImage(SN_FLUFF,-_sz/2,-_sz/2,_sz,_sz); ctx.restore(); window.__snakeFluffDraws++; } ctx.restore(); } // 草甸细草叶纹理层(纯表现层:半透明噪点贴图平铺在 sn-field 上,打破大面积色块的平整感,加载失败跳过) if(SN_NOISE_OK&&SN_NOISE.naturalWidth){ ctx.save(); ctx.beginPath(); ctx.rect(OX,OY,COLS*CELL,ROWS*CELL); ctx.clip(); ctx.globalAlpha=0.12; for(let yy=OY;yy<OY+ROWS*CELL;yy+=256) for(let xx=OX;xx<OX+COLS*CELL;xx+=256) ctx.drawImage(SN_NOISE,xx,yy); ctx.restore(); } // 草甸装饰(纯表现层:背景层静态绘制,蛇/食物/粒子在其上层,不改玩法/碰撞) if(SN_DECOR_OK&&SN_DECOR.naturalWidth&&G.decor){ for(const dspot of G.decor){ let dimg=SN_DECOR, rg; if(dspot.k>=9){ // 水洼边饰区走第三图集,未加载回退主图集对应区 if(SN_DECOR3_OK&&SN_DECOR3.naturalWidth){ dimg=SN_DECOR3; rg=SN_DECOR3_REGIONS[dspot.k-9]; } else rg=SN_DECOR_REGIONS[dspot.k%6]; } else if(dspot.k>=6){ // 花卉变体区走第二图集,未加载回退主图集对应区 if(SN_DECOR2_OK&&SN_DECOR2.naturalWidth){ dimg=SN_DECOR2; rg=SN_DECOR2_REGIONS[dspot.k-6]; } else rg=SN_DECOR_REGIONS[dspot.k%6]; } else rg=SN_DECOR_REGIONS[dspot.k]; ctx.save(); ctx.translate(dspot.x,dspot.y); if(dspot.flip) ctx.scale(-1,1); ctx.drawImage(dimg,rg.x,rg.y,rg.w,rg.h,-dspot.s/2,-dspot.s/2,dspot.s,dspot.s); ctx.restore(); window.__snakeDecorDraws=(window.__snakeDecorDraws||0)+1; // 探针:装饰确实绘制 if(dspot.k>=6&&dspot.k<9) window.__snakeDecor2Draws=(window.__snakeDecor2Draws||0)+1; // 探针:花卉变体确实绘制 if(dspot.k>=9) window.__snakeDecor3Draws=(window.__snakeDecor3Draws||0)+1; // 探针:水洼边饰确实绘制 } } // 蛇行倾倒草痕(纯表现层:蛇身经过的格子叠加倾倒草叶,1.5 秒淡出; // 在背景/装饰之上、蛇身之下;加载失败自动跳过,不改玩法/碰撞) if(SN_BENT_OK&&SN_BENT.naturalWidth&&G.bentGrass&&G.bentGrass.length&&G.state!=='menu'){ ctx.save(); for(const b of G.bentGrass){ const k=Math.min(1,b.t/b.life), a=Math.max(0,1-k); ctx.globalAlpha=0.75*a; const rot=((b.x*73+b.y*149)%360)/360*Math.PI*0.6-0.3; // 按格子伪随机微旋转,确定性无闪烁 const w=CELL*1.5, h=w/2; ctx.save(); ctx.translate(OX+b.x*CELL+CELL/2,OY+b.y*CELL+CELL/2); ctx.rotate(rot); ctx.drawImage(SN_BENT,-w/2,-h/2,w,h); ctx.restore(); window.__snakeBentDraws=(window.__snakeBentDraws||0)+1; // 探针:倾倒草痕确实绘制 } ctx.restore(); } // 吃果处"被啃的草皮"压痕(纯表现层:吃果点留下深色草皮椭圆压痕,2.5 秒消退,不改玩法/碰撞) if(G.nibbles&&G.nibbles.length&&G.state!=='menu'){ ctx.save(); for(const n of G.nibbles){ const k=n.t/n.life, a=Math.max(0,1-k); const wob=1+0.18*Math.sin(n.x*1.7+n.y*2.3); ctx.globalAlpha=0.38*a; ctx.fillStyle='#2f6630'; ctx.beginPath(); ctx.ellipse(n.x,n.y,CELL*0.58*wob,CELL*0.42,0.4,0,Math.PI*2); ctx.fill(); ctx.globalAlpha=0.5*a; ctx.fillStyle='#245127'; ctx.beginPath(); ctx.ellipse(n.x+2,n.y-1,CELL*0.34,CELL*0.24,-0.3,0,Math.PI*2); ctx.fill(); } ctx.restore(); } // 食物生长动画(纯表现层:红果刷新时"从草丛里冒出来"——复用 sn-decor 草丛区 0.3 秒渐大,加载失败回退矢量草叶) if(G.sprouts&&G.sprouts.length&&G.state!=='menu'){ for(const sp of G.sprouts){ const k=Math.min(1,sp.t/sp.life); const ease=1-Math.pow(1-k,3); const s=CELL*1.9*ease; if(s<=0) continue; ctx.save(); ctx.globalAlpha=0.9*k; ctx.translate(sp.x,sp.y); if(SN_DECOR_OK&&SN_DECOR.naturalWidth){ const rg=SN_DECOR_REGIONS[2]; // 草丛区 ctx.drawImage(SN_DECOR,rg.x,rg.y,rg.w,rg.h,-s/2,-s/2,s,s); }else{ ctx.strokeStyle='#4c9434'; ctx.lineWidth=2; for(let b=0;b<5;b++){ const ang=-Math.PI/2+(b/4-0.5)*1.4; ctx.beginPath(); ctx.moveTo(0,6*ease); ctx.quadraticCurveTo(Math.cos(ang)*8*ease,Math.sin(ang)*12*ease,Math.cos(ang)*14*ease,Math.sin(ang)*18*ease); ctx.stroke(); } } ctx.restore(); } } // 食物(红苹果升级版 / 金苹果变体+光环,透明通道直接绘制;加载失败逐项回退原矢量绘制) if(G.food&&G.state!=='menu'){ const fx=OX+G.food.x*CELL, fy=OY+G.food.y*CELL; const pulse=1+Math.sin(performance.now()/180)*0.12; const gold=!!G.food.gold; // 食物落点草叶压影(纯表现层:食物下方铺一小块椭圆暗影、接地感;金苹果透明度随呼吸微动、红果固定;程序化绘制无新贴图,不改玩法/碰撞) { const shA=gold?Math.max(0.14,0.30+0.12*Math.sin(performance.now()/430)):0.24; ctx.save(); ctx.globalAlpha=shA; ctx.fillStyle='#223c14'; ctx.beginPath(); ctx.ellipse(fx+CELL/2,fy+CELL*0.90,CELL*0.52,CELL*0.20,0,0,Math.PI*2); ctx.fill(); ctx.restore(); window.__snakeFoodShadowDraws=(window.__snakeFoodShadowDraws||0)+1; } // 探针:食物压影确实绘制 const _appleImg=gold ?((SN_APPLE3_OK&&SN_APPLE3.naturalWidth)?SN_APPLE3:(SN_APPLE2_OK&&SN_APPLE2.naturalWidth?SN_APPLE2:null)) :((SN_APPLE2_OK&&SN_APPLE2.naturalWidth)?SN_APPLE2:(SN_APPLE_OK&&SN_APPLE.naturalWidth?SN_APPLE:null)); if(_appleImg){ if(gold&&SN_GOLDPATCH_OK&&SN_GOLDPATCH.naturalWidth){ // 金苹果金色草皮光斑(纯表现层:草皮淡金色光斑随存活时间呼吸明灭,红果无;不改玩法/碰撞) const gpA=Math.max(0.12,0.34+0.16*Math.sin(performance.now()/430)); const gw=CELL*2.3, gh=gw*0.62; ctx.save(); ctx.globalAlpha=gpA; ctx.drawImage(SN_GOLDPATCH,fx+CELL/2-gw/2,fy+CELL/2-gh/2,gw,gh); ctx.restore(); window.__snakeGoldPatchDraws=(window.__snakeGoldPatchDraws||0)+1; // 探针:金色草皮光斑确实绘制 } if(gold){ // 金苹果光环(纯表现层:暖金径向光晕+呼吸脉动,不改玩法/碰撞) ctx.save(); ctx.globalCompositeOperation='lighter'; const hr=CELL*(gold?1.9:1.2)*pulse; const hg=ctx.createRadialGradient(fx+CELL/2,fy+CELL/2,CELL*0.25,fx+CELL/2,fy+CELL/2,hr); hg.addColorStop(0,'rgba(255,215,106,'+(0.55*pulse).toFixed(3)+')'); hg.addColorStop(1,'rgba(255,215,106,0)'); ctx.fillStyle=hg; ctx.beginPath(); ctx.arc(fx+CELL/2,fy+CELL/2,hr,0,Math.PI*2); ctx.fill(); ctx.restore(); window.__snakeGoldHaloDraws=(window.__snakeGoldHaloDraws||0)+1; // 探针:金苹果光环确实绘制 } const s=CELL*1.7*pulse*(gold?1.1:1.0); ctx.drawImage(_appleImg,fx+CELL/2-s/2,fy+CELL/2-s/2,s,s); if(gold&&SN_BLUSH_OK&&SN_BLUSH.naturalWidth){ // 金苹果成熟度红晕(纯表现层:存活越久红晕越重、20秒封顶,与金色草皮光斑/光环叠加不冲突;红果不画;加载失败跳过,不改玩法/碰撞) const mat=Math.min(1,(performance.now()-((G.food&&G.food.bornT)||performance.now()))/20000); ctx.save(); ctx.globalAlpha=(0.15+0.65*mat).toFixed(2); ctx.drawImage(SN_BLUSH,fx+CELL/2-s/2,fy+CELL/2-s/2,s,s); ctx.restore(); window.__snakeBlushDraws=(window.__snakeBlushDraws||0)+1; // 探针:成熟度红晕确实绘制 } if(gold) window.__snakeGoldDraws=(window.__snakeGoldDraws||0)+1; // 探针:金苹果确实绘制 if(gold&&SN_LEAF_OK&&SN_LEAF.naturalWidth){ // 果蒂叶(纯表现层:金苹果顶部叶贴图,随苹果脉冲同呼吸+微摆;加载失败跳过,不改玩法/碰撞) const ls=CELL*1.05; const sway=Math.sin(performance.now()/430)*0.09; ctx.save(); ctx.translate(fx+CELL/2,fy+CELL/2-s*0.52); ctx.rotate(sway); ctx.drawImage(SN_LEAF,-ls/2,-ls/2,ls,ls); ctx.restore(); window.__snakeLeafDraws=(window.__snakeLeafDraws||0)+1; // 探针:果蒂叶确实绘制 } window.__snakeAppleDraws=(window.__snakeAppleDraws||0)+1; // 探针:苹果确实绘制 }else{ ctx.save(); ctx.shadowColor='rgba(60,20,10,.4)'; ctx.shadowBlur=6; ctx.fillStyle=gold?'#f0b429':'#d93b3b'; ctx.beginPath(); ctx.arc(fx+CELL/2,fy+CELL/2,(CELL/2-3)*pulse,0,Math.PI*2); ctx.fill(); ctx.restore(); ctx.fillStyle='#6b4a26'; ctx.fillRect(fx+CELL/2-1,fy+2,2,4); // 果梗 ctx.fillStyle='#4c9434'; // 叶子 ctx.beginPath(); ctx.ellipse(fx+CELL/2+4,fy+4,4,2.2,-0.5,0,Math.PI*2); ctx.fill(); } } // 蛇头拖尾(纯表现层:淡金色残影,越新越亮,不改玩法/碰撞) if(G.headTrail&&G.headTrail.length>1&&G.state!=='menu'){ ctx.save(); ctx.globalCompositeOperation='lighter'; const t=G.headTrail, pad=1.5; for(let i=0;i<t.length-1;i++){ const k=(i+1)/t.length; ctx.globalAlpha=0.03+0.10*k; ctx.fillStyle='#fff3b0'; const s=(CELL-pad*2)*(0.5+0.5*k); rr(t[i].x-s/2,t[i].y-s/2,s,s,5); ctx.fill(); } ctx.restore(); } // 蛇 if(G.state!=='menu'){ const n=G.snake.length; for(let i=n-1;i>=0;i--){ const s=G.snake[i]; const x=OX+s.x*CELL, y=OY+s.y*CELL, pad=1.5; const head=i===0; ctx.save(); if(head){ ctx.shadowColor='rgba(20,40,10,.5)'; ctx.shadowBlur=6; } const grad=ctx.createLinearGradient(x,y,x,y+CELL); if(head){ grad.addColorStop(0,'#a8e063'); grad.addColorStop(1,'#6fbf4a'); } else{ grad.addColorStop(0,'#8fd45e'); grad.addColorStop(1,'#5aa73f'); } ctx.fillStyle=grad; rr(x+pad,y+pad,CELL-pad*2,CELL-pad*2,5); ctx.fill(); ctx.restore(); if(SN_SCALE2_OK&&SN_SCALE2.naturalWidth){ // 蛇身分段鳞片纹理叠加(细化版,加载失败回退旧 sn-scale) if(!SN_SCALE2_PAT) SN_SCALE2_PAT=ctx.createPattern(SN_SCALE2,'repeat'); if(SN_SCALE2_PAT){ ctx.save(); ctx.globalAlpha=.24; ctx.fillStyle=SN_SCALE2_PAT; rr(x+pad,y+pad,CELL-pad*2,CELL-pad*2,5); ctx.fill(); ctx.restore(); // 分段明暗:偶数节微亮、奇数节微暗,节段感更清晰(纯表现层) ctx.save(); ctx.globalAlpha=(i%2===0)?0.05:0.11; ctx.fillStyle=(i%2===0)?'#e8ffb0':'#1e4d12'; rr(x+pad,y+pad,CELL-pad*2,CELL-pad*2,5); ctx.fill(); ctx.restore(); window.__snakeScale2Draws=(window.__snakeScale2Draws||0)+1; // 探针:细化鳞片确实绘制 } }else if(SN_SCALE_OK&&SN_SCALE.naturalWidth){ // 回退旧纹理 if(!SN_SCALE_PAT) SN_SCALE_PAT=ctx.createPattern(SN_SCALE,'repeat'); if(SN_SCALE_PAT){ ctx.save(); ctx.globalAlpha=.16; ctx.fillStyle=SN_SCALE_PAT; rr(x+pad,y+pad,CELL-pad*2,CELL-pad*2,5); ctx.fill(); ctx.restore(); } } if(!head&&n>=4){ // 蛇尾磨损质感(纯表现层:渐进加深+磨损鳞片环纹,越靠近尾尖越暗,不改玩法/碰撞) const tk=(i-1)/Math.max(1,n-2); // 0→1 从第二节渐变到尾尖 const k=Math.max(0,Math.min(1,(tk-0.25)/0.75)); if(k>0){ ctx.save(); rr(x+pad,y+pad,CELL-pad*2,CELL-pad*2,5); ctx.clip(); if(SN_TAILWORN_OK&&SN_TAILWORN.naturalWidth){ ctx.globalAlpha=0.25+0.45*k; ctx.drawImage(SN_TAILWORN,x+pad,y+pad,CELL-pad*2,CELL-pad*2); } ctx.globalAlpha=0.10+0.40*k; // 尾尖泥土深色渐进(草原+泥土画风) ctx.fillStyle='#4a3319'; rr(x+pad,y+pad,CELL-pad*2,CELL-pad*2,5); ctx.fill(); ctx.restore(); } } if(head){ // 蛇头插画:大眼+分叉红舌(朝移动方向),回退旧蛇头/矢量眼 const dx=G.dir.x, dy=G.dir.y; const ex=x+CELL/2, ey=y+CELL/2; if(SN_HEAD2_OK&&SN_HEAD2.naturalWidth){ const hw=CELL*2.0, hh=hw*(SN_HEAD2.naturalHeight/SN_HEAD2.naturalWidth); ctx.save(); ctx.translate(ex,ey); ctx.rotate(Math.atan2(dy,dx)); ctx.drawImage(SN_HEAD2,-hw/2,-hh/2,hw,hh); ctx.restore(); window.__snakeHead2Draws=(window.__snakeHead2Draws||0)+1; // 探针:新蛇头确实绘制 }else if(SN_HEAD_OK&&SN_HEAD.naturalWidth){ const hs=CELL*1.6; ctx.save(); ctx.translate(ex,ey); ctx.rotate(Math.atan2(dy,dx)); ctx.drawImage(SN_HEAD,-hs/2,-hs/2,hs,hs); ctx.restore(); if(SN_SCALE_FINE_OK&&SN_SCALE_FINE.naturalWidth){ // 蛇头细鳞片纹理叠加(仅旧蛇头用,新蛇头已带鳞片) if(!SN_SCALE_FINE_PAT) SN_SCALE_FINE_PAT=ctx.createPattern(SN_SCALE_FINE,'repeat'); if(SN_SCALE_FINE_PAT){ ctx.save(); ctx.globalAlpha=.34; ctx.fillStyle=SN_SCALE_FINE_PAT; rr(x+pad,y+pad,CELL-pad*2,CELL-pad*2,5); ctx.fill(); ctx.restore(); } } }else{ ctx.fillStyle='#1a330e'; ctx.beginPath(); ctx.arc(ex+dx*4-dy*4,ey+dy*4+dx*4,2.6,0,Math.PI*2); ctx.arc(ex+dx*4+dy*4,ey+dy*4-dx*4,2.6,0,Math.PI*2); ctx.fill(); } if(G.headGlowT){ // 吃红果加速时蛇头短暂发亮(纯表现层,不改速度/碰撞) const ga=performance.now()-G.headGlowT; if(ga<600){ const gk=1-ga/600; ctx.save(); ctx.globalCompositeOperation='lighter'; ctx.globalAlpha=0.6*gk; const hg=ctx.createRadialGradient(ex,ey,2,ex,ey,CELL*1.2); hg.addColorStop(0,'#ffe9a8'); hg.addColorStop(1,'rgba(255,215,106,0)'); ctx.fillStyle=hg; ctx.beginPath(); ctx.arc(ex,ey,CELL*1.2,0,Math.PI*2); ctx.fill(); ctx.restore(); } } } if((head||i<3)&&G.stepMs<120){ // 高速排汗湿光(纯表现层:stepMs<120 时蛇头/前节湿润高光,随速度衰减;加载失败跳过,不改速度/碰撞) const sk=Math.max(0,Math.min(1,(120-G.stepMs)/50)); if(SN_SWEAT_OK&&SN_SWEAT.naturalWidth){ ctx.save(); ctx.globalAlpha=0.55*sk; rr(x+pad,y+pad,CELL-pad*2,CELL-pad*2,5); ctx.clip(); ctx.drawImage(SN_SWEAT,x+pad,y+pad,CELL-pad*2,CELL-pad*2); ctx.restore(); window.__snakeSweatDraws=(window.__snakeSweatDraws||0)+1; // 探针:排汗湿光确实绘制 } } } } // 爆炸粒子(普通合成,草地上加色会过曝) + 冲击波环(加色合成) if(parts.length||rings.length){ for(const p of parts){ const k=1-p.t/p.life; ctx.save(); ctx.globalAlpha=Math.max(0,k); ctx.fillStyle=p.c; ctx.beginPath(); ctx.arc(p.x,p.y,p.r*k+0.5,0,Math.PI*2); ctx.fill(); ctx.restore(); } if(rings.length){ ctx.save(); ctx.globalCompositeOperation='lighter'; for(const r of rings){ const k=r.t/r.life; ctx.strokeStyle='rgba('+(r.c||'255,150,180')+','+(0.8*(1-k)).toFixed(3)+')'; ctx.lineWidth=2.5; ctx.beginPath(); ctx.arc(r.x,r.y,Math.max(0.1,r.r),0,Math.PI*2); ctx.stroke(); } ctx.restore(); } } ctx.restore(); // 震屏结束 drawHUD(); if(G.state==='menu') drawMenu(); else if(G.state==='over') drawOver(); else if(G.paused) drawPaused(); // 里程碑蛇蜕(纯表现层:蛇蜕贴图微旋转、5 秒淡出,加载失败自动跳过,不改玩法/碰撞) if(G.sheds&&G.sheds.length&&G.state!=='menu'){ for(const _sd of G.sheds){ if(SN_SHED_OK&&SN_SHED.naturalWidth){ const _sk=Math.min(1,_sd.t/_sd.life), _sa=Math.max(0,1-_sk*_sk); window.__snakeShedDraws=(window.__snakeShedDraws||0)+1; ctx.save(); ctx.translate(_sd.x,_sd.y); ctx.rotate(_sd.rot); ctx.globalAlpha=0.9*_sa; const _sz=CELL*2.4; ctx.drawImage(SN_SHED,-_sz/2,-_sz/2,_sz,_sz); if(SN_GRASS_OK&&SN_GRASS.naturalWidth){ // 沾附草叶:蛇蜕左上/右下各 1 丛(位置由坐标哈希确定,与蛇蜕同旋转、同淡出) const _h=Math.abs(Math.sin(_sd.x*12.9898+_sd.y*78.233)*43758.5453)%1; const _gb=[[-0.28*_sz,-0.30*_sz,_h*0.9-0.2,0.42],[0.30*_sz,0.24*_sz,0.5+_h*1.1,0.38]]; for(const _g of _gb){ ctx.save(); ctx.translate(_g[0],_g[1]); ctx.rotate(_g[2]); const _gs=_sz*_g[3]; ctx.drawImage(SN_GRASS,-_gs/2,-_gs/2,_gs,_gs); ctx.restore(); window.__snakeGrassDraws=(window.__snakeGrassDraws||0)+1; } } ctx.restore(); } } } // 死亡压扁草皮痕迹(纯表现层:死亡点留下压扁的草皮椭圆,4 秒消退,画在结算遮罩之上才看得见,图像化死亡反馈,不改逻辑) if(G.deadSpot&&G.state!=='menu'){ const dk=Math.min(1,G.deadSpot.t/G.deadSpot.life), da=Math.max(0,1-dk*dk); ctx.save(); ctx.globalAlpha=0.5*da; ctx.fillStyle='#33582b'; ctx.beginPath(); ctx.ellipse(G.deadSpot.x,G.deadSpot.y,CELL*1.05,CELL*0.75,0.3,0,Math.PI*2); ctx.fill(); ctx.globalAlpha=0.6*da; ctx.fillStyle='#2a4c24'; ctx.beginPath(); ctx.ellipse(G.deadSpot.x-4,G.deadSpot.y+2,CELL*0.65,CELL*0.42,-0.5,0,Math.PI*2); ctx.fill(); ctx.restore(); } // 吃到/死亡时的全屏闪(盖在结算遮罩之上) if(G.flash>0){ ctx.fillStyle='rgba(255,140,170,'+(G.flash*0.35).toFixed(3)+')'; ctx.fillRect(0,0,canvas.width,canvas.height); } } function drawHUD(){ ctx.textBaseline='alphabetic'; ctx.fillStyle='#fff8e0'; ctx.font='700 26px "PingFang SC","Microsoft YaHei",monospace'; ctx.textAlign='left'; ctx.fillText('SCORE '+G.score,14,44); ctx.textAlign='right'; ctx.fillStyle='#e8d5a8'; ctx.font='400 18px "PingFang SC","Microsoft YaHei",monospace'; ctx.fillText('BEST '+G.best,canvas.width-14,42); ctx.textAlign='left'; ctx.fillStyle='#c9b183'; ctx.font='400 15px "PingFang SC","Microsoft YaHei",monospace'; ctx.fillText(IS_TOUCH?'滑动转向 · 右上 ⏸ 暂停':'方向键 / WASD 转向 · P 暂停 · 回车 开始',14,76); } function dim(){ ctx.fillStyle='rgba(2,8,16,.72)'; ctx.fillRect(0,0,canvas.width,canvas.height); } function drawMenu(){ dim(); ctx.textAlign='center'; ctx.save(); ctx.shadowColor='rgba(60,120,30,.9)'; ctx.shadowBlur=24; ctx.fillStyle='#d8f3a0'; ctx.font='700 44px "PingFang SC","Microsoft YaHei",monospace'; ctx.fillText('贪 吃 蛇',canvas.width/2,240); ctx.restore(); ctx.fillStyle='#f0e8d0'; ctx.font='400 17px "PingFang SC","Microsoft YaHei",monospace'; const lines=IS_TOUCH ? ['在棋盘上滑动手指转向','吃到红果加速变长','撞墙或咬到自己就结束'] : ['方向键 / WASD 转向','吃到红果加速变长','撞墙或咬到自己就结束']; lines.forEach((t,i)=>ctx.fillText(t,canvas.width/2,300+i*30)); ctx.fillStyle='#ffe9a8'; ctx.font='700 19px "PingFang SC","Microsoft YaHei",monospace'; ctx.fillText(IS_TOUCH?'点按屏幕开始':'按 回车 开始',canvas.width/2,420); } function drawOver(){ dim(); /* 死亡结算面板缩放入场:320ms easeOutBack 0.6→1(纯表现层,不改逻辑) */ const t=Math.min(1,Math.max(0,G.overT===undefined?1:G.overT)); const eob=1+2.7*Math.pow(t-1,3)+1.7*Math.pow(t-1,2); const sc=0.6+0.4*eob; ctx.save(); ctx.translate(canvas.width/2,275); ctx.scale(Math.max(0.01,sc),Math.max(0.01,sc)); ctx.translate(-canvas.width/2,-275); /* 草甸木牌风:复用暂停遮罩 drawPaused 同一视觉语言,去霓虹红字发光(纯表现层) */ const ocx=canvas.width/2, ocy=275, OW=340, OH=150, OX=ocx-OW/2, OY=ocy-OH/2; ctx.fillStyle='#3f7d33'; // 草地色外框 rr(OX-8,OY-8,OW+16,OH+16,16); ctx.fill(); ctx.fillStyle='#2b5c22'; rr(OX-3,OY-3,OW+6,OH+6,13); ctx.fill(); const owg=ctx.createLinearGradient(0,OY,0,OY+OH); // 木牌底 owg.addColorStop(0,'#a9764a'); owg.addColorStop(0.5,'#8f5e33'); owg.addColorStop(1,'#75491f'); ctx.fillStyle=owg; rr(OX,OY,OW,OH,12); ctx.fill(); ctx.strokeStyle='rgba(58,36,16,.35)'; ctx.lineWidth=2; // 木板横纹 for(const fy of [OY+OH*0.33,OY+OH*0.66]){ ctx.beginPath(); ctx.moveTo(OX+12,fy); ctx.lineTo(OX+OW-12,fy); ctx.stroke(); } ctx.fillStyle='rgba(58,36,16,.28)'; // 木节 ctx.beginPath(); ctx.ellipse(OX+OW*0.82,OY+OH*0.30,10,7,0.4,0,Math.PI*2); ctx.fill(); ctx.beginPath(); ctx.ellipse(OX+OW*0.20,OY+OH*0.72,8,6,-0.3,0,Math.PI*2); ctx.fill(); for(const [nx,ny] of [[OX+16,OY+16],[OX+OW-16,OY+16],[OX+16,OY+OH-16],[OX+OW-16,OY+OH-16]]){ ctx.fillStyle='#3a2a16'; // 四角铆钉 ctx.beginPath(); ctx.arc(nx,ny,5,0,Math.PI*2); ctx.fill(); ctx.fillStyle='rgba(255,220,150,.5)'; ctx.beginPath(); ctx.arc(nx-1.5,ny-1.5,1.8,0,Math.PI*2); ctx.fill(); } ctx.textAlign='center'; ctx.textBaseline='middle'; ctx.fillStyle='#2e1c0e'; // 深色墨字,无霓虹发光 ctx.font='700 40px "PingFang SC","Microsoft YaHei",monospace'; ctx.fillText('游 戏 结 束',ocx,ocy-38); ctx.fillStyle='#4a3418'; ctx.font='400 20px "PingFang SC","Microsoft YaHei",monospace'; ctx.fillText('得分 '+G.score+' 历史最佳 '+G.best,ocx,ocy+24); ctx.restore(); window.__snakeOverPlaque=(window.__snakeOverPlaque||0)+1; // 探针钩子:木牌死亡面板绘制计数 } function drawPaused(){ dim(); /* 暂停遮罩草甸木牌风格:木牌质感卡片+草地色边框,去霓虹字样式(纯表现层,不改逻辑) */ const cx=canvas.width/2, cy=280, W=300, H=124, X=cx-W/2, Y=cy-H/2; ctx.save(); ctx.textAlign='center'; ctx.textBaseline='middle'; ctx.fillStyle='#3f7d33'; // 草地色外框 rr(X-8,Y-8,W+16,H+16,16); ctx.fill(); ctx.fillStyle='#2b5c22'; rr(X-3,Y-3,W+6,H+6,13); ctx.fill(); const wg=ctx.createLinearGradient(0,Y,0,Y+H); // 木牌底 wg.addColorStop(0,'#a9764a'); wg.addColorStop(0.5,'#8f5e33'); wg.addColorStop(1,'#75491f'); ctx.fillStyle=wg; rr(X,Y,W,H,12); ctx.fill(); ctx.strokeStyle='rgba(58,36,16,.35)'; ctx.lineWidth=2; // 木板横纹 for(const fy of [Y+H*0.33,Y+H*0.66]){ ctx.beginPath(); ctx.moveTo(X+12,fy); ctx.lineTo(X+W-12,fy); ctx.stroke(); } ctx.fillStyle='rgba(58,36,16,.28)'; // 木节 ctx.beginPath(); ctx.ellipse(X+W*0.82,Y+H*0.30,10,7,0.4,0,Math.PI*2); ctx.fill(); ctx.beginPath(); ctx.ellipse(X+W*0.20,Y+H*0.72,8,6,-0.3,0,Math.PI*2); ctx.fill(); for(const [nx,ny] of [[X+16,Y+16],[X+W-16,Y+16],[X+16,Y+H-16],[X+W-16,Y+H-16]]){ ctx.fillStyle='#3a2a16'; // 四角铆钉 ctx.beginPath(); ctx.arc(nx,ny,5,0,Math.PI*2); ctx.fill(); ctx.fillStyle='rgba(255,220,150,.5)'; ctx.beginPath(); ctx.arc(nx-1.5,ny-1.5,1.8,0,Math.PI*2); ctx.fill(); } ctx.fillStyle='#2e1c0e'; // 深色墨字,无霓虹发光 ctx.font='700 34px "PingFang SC","Microsoft YaHei",monospace'; ctx.fillText('已 暂 停',cx,cy-18); ctx.fillStyle='#4a3418'; ctx.font='400 17px "PingFang SC","Microsoft YaHei",monospace'; ctx.fillText(IS_TOUCH?'点 ⏸ 继续':'按 P 继续',cx,cy+28); ctx.restore(); } /* ================= 主循环 ================= */ let rafId=0; function tick(ts){ const dt=Math.min(100,ts-(G.last||ts)); G.last=ts; update(dt); draw(); rafId=requestAnimationFrame(tick); } rafId=requestAnimationFrame(tick); /* ================= 输入 ================= */ window.addEventListener('keydown',e=>{ const k=e.key; if(['ArrowUp','ArrowDown','ArrowLeft','ArrowRight',' '].includes(k)) e.preventDefault(); audio(); if(k==='p'||k==='P'){ togglePause(); return; } if(k==='Enter'){ if(G.state==='menu'||G.state==='over') start(); return; } if(G.state==='menu'){ start(); } if(k==='ArrowUp'||k==='w'||k==='W') setDir(0,-1); else if(k==='ArrowDown'||k==='s'||k==='S') setDir(0,1); else if(k==='ArrowLeft'||k==='a'||k==='A') setDir(-1,0); else if(k==='ArrowRight'||k==='d'||k==='D') setDir(1,0); }); function togglePause(){ if(G.state!=='playing') return; G.paused=!G.paused; } pauseBtn.addEventListener('click',e=>{ e.preventDefault(); audio(); togglePause(); }); retryBtn.addEventListener('click',e=>{ e.preventDefault(); audio(); start(); }); /* 触屏:点按开始 / 滑动转向 */ let touchStart=null; canvas.addEventListener('touchstart',e=>{ audio(); const t=e.changedTouches[0]; touchStart={x:t.clientX,y:t.clientY,moved:false}; },{passive:true}); canvas.addEventListener('touchmove',e=>{ if(!touchStart) return; e.preventDefault(); const t=e.changedTouches[0]; const dx=t.clientX-touchStart.x, dy=t.clientY-touchStart.y; if(Math.abs(dx)<24&&Math.abs(dy)<24) return; touchStart.moved=true; if(Math.abs(dx)>Math.abs(dy)) setDir(dx>0?1:-1,0); else setDir(0,dy>0?1:-1); touchStart={x:t.clientX,y:t.clientY,moved:true}; // 连续滑动可多次转向 },{passive:false}); canvas.addEventListener('touchend',e=>{ if(touchStart&&!touchStart.moved){ if(G.state==='menu'||G.state==='over') start(); } touchStart=null; },{passive:true}); canvas.addEventListener('mousedown',()=>{ audio(); if(G.state==='menu'||G.state==='over') start(); }); window.addEventListener('blur',()=>{ if(G.state==='playing') G.paused=true; }); /* headless 探针钩子 */ window.__snake={G:G, forceDie(){ start(); G.state='over'; G.overT=0; G.deadAt=performance.now(); retryBtn.classList.add('show'); G.deadSpot={x:OX+16*CELL+CELL/2,y:OY+17*CELL+CELL/2,t:0,life:4000}; // 探针可验死亡压痕 if(G.score>G.best){ G.best=G.score; try{ localStorage.setItem('snake_best',String(G.best)); }catch(e){} } G.flash=1; G.shake=1; }, overT(){ return G.overT; }, sprouts(){ return G.sprouts?G.sprouts.length:0; }, deadSpot(){ return G.deadSpot?1:0; }, sheds(){ return G.sheds?G.sheds.length:0; }, shedOk(){ return SN_SHED_OK&&SN_SHED.naturalWidth>0; }, shedDraws(){ return window.__snakeShedDraws||0; }, bentOk(){ return SN_BENT_OK&&SN_BENT.naturalWidth>0; }, bentDraws(){ return window.__snakeBentDraws||0; }, bentMarks(){ return G.bentGrass?G.bentGrass.length:0; }, noiseOk(){ return SN_NOISE_OK&&SN_NOISE.naturalWidth>0; }, decorSpots(){ return G.decor?G.decor.length:0; }, decorDraws(){ return window.__snakeDecorDraws||0; }, decor3Draws(){ return window.__snakeDecor3Draws||0; }, meadowOk(){ return SN_MEADOW2_OK&&SN_MEADOW2.naturalWidth>0; }, scale2Ok(){ return SN_SCALE2_OK&&SN_SCALE2.naturalWidth>0; }, head2Ok(){ return SN_HEAD2_OK&&SN_HEAD2.naturalWidth>0; }, apple2Ok(){ return SN_APPLE2_OK&&SN_APPLE2.naturalWidth>0; }, goldOk(){ return SN_APPLE3_OK&&SN_APPLE3.naturalWidth>0; }, bgDrawn(){ return window.__snakeBgDrawn||0; }, scale2Draws(){ return window.__snakeScale2Draws||0; }, head2Draws(){ return window.__snakeHead2Draws||0; }, appleDraws(){ return window.__snakeAppleDraws||0; }, goldDraws(){ return window.__snakeGoldDraws||0; }, goldHaloDraws(){ return window.__snakeGoldHaloDraws||0; }, goldPatchOk(){ return SN_GOLDPATCH_OK&&SN_GOLDPATCH.naturalWidth>0; }, goldPatchDraws(){ return window.__snakeGoldPatchDraws||0; }, leafOk(){ return SN_LEAF_OK&&SN_LEAF.naturalWidth>0; }, leafDraws(){ return window.__snakeLeafDraws||0; }, foodShadowDraws(){ return window.__snakeFoodShadowDraws||0; }, blushOk(){ return SN_BLUSH_OK&&SN_BLUSH.naturalWidth>0; }, blushDraws(){ return window.__snakeBlushDraws||0; }, blushMat(){ if(!(G.food&&G.food.gold)) return -1; return Math.min(1,(performance.now()-((G.food&&G.food.bornT)||performance.now()))/20000); }, foodGold(){ return !!(G.food&&G.food.gold); }, score(){ return G.score; }, state(){ return G.state; } }; </script> </body> </html>
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>贪吃蛇</title>
<link rel="icon" href="favicon.svg" type="image/svg+xml">
<link rel="apple-touch-icon" href="apple-touch-icon.png">
<style>
*{box-sizing:border-box;-webkit-tap-highlight-color:transparent}
html,body{margin:0;height:100%;background:#101a0c;overflow:hidden}
body{display:flex;align-items:center;justify-content:center;
background:radial-gradient(1200px 700px at 50% 30%,#2a3d1e 0%,#101a0c 75%)}
body.touch{flex-direction:column;justify-content:flex-start;padding-top:8px;overflow-y:auto}
canvas{max-height:92vh;max-width:96vw;
border:2px solid #6b4a26;box-shadow:0 0 40px rgba(20,40,10,.5),0 0 140px rgba(0,0,0,.6)}
#stage{position:relative;line-height:0}
#retryBtn{position:absolute;left:50%;bottom:10%;transform:translateX(-50%);display:none;z-index:5;
font:700 18px "PingFang SC","Microsoft YaHei",monospace;letter-spacing:.3em;text-indent:.3em;
color:#0d2608;background:linear-gradient(180deg,#a8e063,#56ab2f);border:0;border-radius:999px;
padding:14px 36px;cursor:pointer;box-shadow:0 4px 18px rgba(40,90,20,.5);touch-action:manipulation}
#retryBtn.show{display:block}
#retryBtn:active{transform:translateX(-50%) scale(.96)}
#pauseBtn{display:none;position:absolute;right:10px;top:10px;z-index:5;
width:44px;height:44px;border-radius:12px;border:1px solid #6b4a26;
background:rgba(255,255,255,.10);color:#f0e8d0;font-size:18px;touch-action:manipulation}
body.touch #pauseBtn{display:flex;align-items:center;justify-content:center}
#pauseBtn svg{width:22px;height:22px;display:block}
</style>
</head>
<body>
<div id="stage">
<canvas id="game" width="420" height="520"></canvas>
<button id="retryBtn" type="button">再来一局</button>
<button id="pauseBtn" type="button" aria-label="暂停"><svg viewBox="0 0 24 24" aria-hidden="true"><defs><linearGradient id="snk-pg" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="#d6f2b4"/><stop offset="1" stop-color="#4c9a5f"/></linearGradient></defs><path d="M11 3c3 2 4 6 3.4 9.6C13.4 18 9 21 5 21c.6-4 2.4-7 5-8.6" fill="#7cc47f" opacity=".55"/><rect x="5.2" y="4" width="5" height="16" rx="2.5" fill="url(#snk-pg)"/><rect x="13.8" y="4" width="5" height="16" rx="2.5" fill="url(#snk-pg)"/><circle cx="18.6" cy="5.6" r="2.1" fill="#ffd9e8"/><circle cx="18.6" cy="5.6" r=".85" fill="#ff8fb3"/></svg></button>
</div>
<script>
'use strict';
/* ================= 配置 ================= */
const COLS=21, ROWS=21, CELL=20;
const OX=0, OY=100; // 棋盘左上角:canvas 420x520,顶部 100px 留给 HUD
const canvas=document.getElementById('game');
const ctx=canvas.getContext('2d');
const retryBtn=document.getElementById('retryBtn');
const pauseBtn=document.getElementById('pauseBtn');
const IS_TOUCH=('ontouchstart' in window)||(navigator.maxTouchPoints>0);
if(IS_TOUCH) document.body.classList.add('touch');
const G={
state:'menu', paused:false,
snake:[], dir:{x:1,y:0}, queued:null,
food:null, score:0, best:0, stepMs:150, acc:0, last:0,
flash:0, shake:0, deadAt:0, overT:1, // 死亡结算面板缩放入场计时(纯表现层)
nibbles:[], // 吃果处草皮压痕(纯表现层)
sprouts:[], // 食物生长动画记录(纯表现层)
deadSpot:null, // 死亡压扁草皮痕迹(纯表现层)
sheds:[], // 里程碑蛇蜕记录(纯表现层)
shedShown:{}, // 已触发的里程碑(纯表现层)
bentGrass:[] // 蛇行倾倒草痕(纯表现层:蛇身经过格子留 1.5 秒草痕消退)
};
try{ G.best=parseInt(localStorage.getItem('snake_best')||'0',10)||0; }catch(e){}
/* 草原美术素材:田野背景 / 午后草甸全景(升级) / 蛇头精灵 / 眼睛分叉舌蛇头(升级) / 鳞片纹理 / 细化鳞片(升级) / 写实苹果 / 红苹果升级 / 金苹果(加载失败逐项回退原矢量绘制) */
const SN_FIELD=new Image(); let SN_FIELD_OK=false;
SN_FIELD.onload=()=>{ SN_FIELD_OK=true; };
SN_FIELD.onerror=()=>{ SN_FIELD_OK=false; };
SN_FIELD.src='art/sn-field.webp';
/* 草甸全景背景升级(午后草原全景:午后暖光棋盘草原、泥土斑块、角落水洼;加载失败回退 sn-field,再失败回退矢量网格) */
const SN_MEADOW2=new Image(); let SN_MEADOW2_OK=false;
SN_MEADOW2.onload=()=>{ SN_MEADOW2_OK=true; };
SN_MEADOW2.onerror=()=>{ SN_MEADOW2_OK=false; };
SN_MEADOW2.src='art/sn-meadow2.webp';
const SN_HEAD=new Image(); let SN_HEAD_OK=false;
SN_HEAD.onload=()=>{ SN_HEAD_OK=true; };
SN_HEAD.onerror=()=>{ SN_HEAD_OK=false; };
SN_HEAD.src='art/sn-head.webp';
/* 蛇头插画升级:大眼+分叉红舌(朝移动方向;加载失败回退旧 sn-head,再失败回退矢量眼) */
const SN_HEAD2=new Image(); let SN_HEAD2_OK=false;
SN_HEAD2.onload=()=>{ SN_HEAD2_OK=true; };
SN_HEAD2.onerror=()=>{ SN_HEAD2_OK=false; };
SN_HEAD2.src='art/sn-head2.webp';
const SN_SCALE=new Image(); let SN_SCALE_OK=false;
SN_SCALE.onload=()=>{ SN_SCALE_OK=true; };
SN_SCALE.onerror=()=>{ SN_SCALE_OK=false; };
SN_SCALE.src='art/sn-scale.webp';
let SN_SCALE_PAT=null;
/* 蛇身鳞片纹理细化版(细密叠瓦状绿鳞,蛇身分段贴;加载失败回退旧 sn-scale) */
const SN_SCALE2=new Image(); let SN_SCALE2_OK=false;
SN_SCALE2.onload=()=>{ SN_SCALE2_OK=true; };
SN_SCALE2.onerror=()=>{ SN_SCALE2_OK=false; };
SN_SCALE2.src='art/sn-scale2.webp';
let SN_SCALE2_PAT=null;
/* 蛇头细鳞片纹理(纯表现层:只叠在蛇头上,细密弧片,与蛇尾磨损衔接;加载失败跳过,不改玩法/碰撞) */
const SN_SCALE_FINE=new Image(); let SN_SCALE_FINE_OK=false;
SN_SCALE_FINE.onload=()=>{ SN_SCALE_FINE_OK=true; };
SN_SCALE_FINE.onerror=()=>{ SN_SCALE_FINE_OK=false; };
SN_SCALE_FINE.src='art/sn-scale-fine.webp';
let SN_SCALE_FINE_PAT=null;
/* 高速排汗湿光(纯表现层:stepMs<120 蛇头/前节叠加湿润高光;加载失败跳过,不改速度/碰撞) */
const SN_SWEAT=new Image(); let SN_SWEAT_OK=false;
SN_SWEAT.onload=()=>{ SN_SWEAT_OK=true; };