snake 草原美术改版:田野背景+蛇头精灵+鳞片纹理+写实苹果
Browse files- snake.html +100 -66
snake.html
CHANGED
|
@@ -8,22 +8,22 @@
|
|
| 8 |
<link rel="apple-touch-icon" href="apple-touch-icon.png">
|
| 9 |
<style>
|
| 10 |
*{box-sizing:border-box;-webkit-tap-highlight-color:transparent}
|
| 11 |
-
html,body{margin:0;height:100%;background:#
|
| 12 |
body{display:flex;align-items:center;justify-content:center;
|
| 13 |
-
background:radial-gradient(1200px 700px at 50% 30%,#
|
| 14 |
body.touch{flex-direction:column;justify-content:flex-start;padding-top:8px;overflow-y:auto}
|
| 15 |
canvas{max-height:92vh;max-width:96vw;
|
| 16 |
-
border:
|
| 17 |
#stage{position:relative;line-height:0}
|
| 18 |
#retryBtn{position:absolute;left:50%;bottom:10%;transform:translateX(-50%);display:none;z-index:5;
|
| 19 |
font:700 18px "PingFang SC","Microsoft YaHei",monospace;letter-spacing:.3em;text-indent:.3em;
|
| 20 |
-
color:#
|
| 21 |
-
padding:14px 36px;cursor:pointer;box-shadow:0
|
| 22 |
#retryBtn.show{display:block}
|
| 23 |
#retryBtn:active{transform:translateX(-50%) scale(.96)}
|
| 24 |
#pauseBtn{display:none;position:absolute;right:10px;top:10px;z-index:5;
|
| 25 |
-
width:44px;height:44px;border-radius:12px;border:1px solid #
|
| 26 |
-
background:rgba(
|
| 27 |
body.touch #pauseBtn{display:block}
|
| 28 |
</style>
|
| 29 |
</head>
|
|
@@ -52,12 +52,24 @@ const G={
|
|
| 52 |
flash:0, shake:0, deadAt:0, overT:1 // 死亡结算面板缩放入场计时(纯表现层)
|
| 53 |
};
|
| 54 |
try{ G.best=parseInt(localStorage.getItem('snake_best')||'0',10)||0; }catch(e){}
|
| 55 |
-
/*
|
| 56 |
-
const
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
/* 吃到食物的爆炸粒子 + 冲击波环(不改玩法,只加视觉反馈) */
|
| 62 |
const parts=[], rings=[];
|
| 63 |
function burst(cx,cy){
|
|
@@ -72,7 +84,7 @@ function burst(cx,cy){
|
|
| 72 |
}
|
| 73 |
/* 成就:首次通关 / 破纪录(localStorage arcade_achv) */
|
| 74 |
/* 成就 toast:破纪录弹"新纪录!",首次通关弹 🎖️ 徽章动画(队列依次展示,不改玩法) */
|
| 75 |
-
(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:
|
| 76 |
var _achvQ=[],_achvBusy=false;
|
| 77 |
function showAchvToast(kind){_achvQ.push(kind);pumpAchvToast()}
|
| 78 |
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}}
|
|
@@ -193,47 +205,52 @@ function draw(){
|
|
| 193 |
ctx.clearRect(0,0,canvas.width,canvas.height);
|
| 194 |
ctx.save();
|
| 195 |
if(G.shake>0) ctx.translate((Math.random()-0.5)*8*G.shake,(Math.random()-0.5)*8*G.shake);
|
| 196 |
-
// 背景网格
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
-
// 食物(精灵
|
| 206 |
if(G.food&&G.state!=='menu'){
|
| 207 |
const fx=OX+G.food.x*CELL, fy=OY+G.food.y*CELL;
|
| 208 |
const pulse=1+Math.sin(performance.now()/180)*0.12;
|
| 209 |
-
if(
|
| 210 |
const s=CELL*1.7*pulse;
|
| 211 |
-
ctx.
|
| 212 |
-
ctx.globalCompositeOperation='lighter'; // 黑底用加色合成抵消
|
| 213 |
-
ctx.drawImage(SN_FOOD,fx+CELL/2-s/2,fy+CELL/2-s/2,s,s);
|
| 214 |
-
ctx.restore();
|
| 215 |
}else{
|
| 216 |
ctx.save();
|
| 217 |
-
ctx.shadowColor='
|
| 218 |
-
ctx.fillStyle='#
|
| 219 |
ctx.beginPath();
|
| 220 |
ctx.arc(fx+CELL/2,fy+CELL/2,(CELL/2-3)*pulse,0,Math.PI*2);
|
| 221 |
ctx.fill();
|
| 222 |
ctx.restore();
|
| 223 |
-
ctx.fillStyle='#
|
| 224 |
-
ctx.fillRect(fx+CELL/2-1
|
|
|
|
|
|
|
| 225 |
}
|
| 226 |
}
|
| 227 |
|
| 228 |
-
// 蛇头
|
| 229 |
if(G.headTrail&&G.headTrail.length>1&&G.state!=='menu'){
|
| 230 |
ctx.save();
|
| 231 |
ctx.globalCompositeOperation='lighter';
|
| 232 |
const t=G.headTrail, pad=1.5;
|
| 233 |
for(let i=0;i<t.length-1;i++){
|
| 234 |
const k=(i+1)/t.length;
|
| 235 |
-
ctx.globalAlpha=0.
|
| 236 |
-
ctx.fillStyle='#
|
| 237 |
const s=(CELL-pad*2)*(0.5+0.5*k);
|
| 238 |
rr(t[i].x-s/2,t[i].y-s/2,s,s,5);
|
| 239 |
ctx.fill();
|
|
@@ -249,17 +266,35 @@ function draw(){
|
|
| 249 |
const x=OX+s.x*CELL, y=OY+s.y*CELL, pad=1.5;
|
| 250 |
const head=i===0;
|
| 251 |
ctx.save();
|
| 252 |
-
if(head){ ctx.shadowColor='
|
| 253 |
const grad=ctx.createLinearGradient(x,y,x,y+CELL);
|
| 254 |
-
if(head){ grad.addColorStop(0,'#
|
| 255 |
-
else{ grad.addColorStop(0,'#
|
| 256 |
ctx.fillStyle=grad;
|
| 257 |
rr(x+pad,y+pad,CELL-pad*2,CELL-pad*2,5);
|
| 258 |
ctx.fill();
|
| 259 |
ctx.restore();
|
| 260 |
-
if(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
const dx=G.dir.x, dy=G.dir.y;
|
| 262 |
const ex=x+CELL/2, ey=y+CELL/2;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
if(G.headGlowT){ // 吃红果加速时蛇头短暂发亮(纯表现层,不改速度/碰撞)
|
| 264 |
const ga=performance.now()-G.headGlowT;
|
| 265 |
if(ga<600){
|
|
@@ -273,32 +308,31 @@ function draw(){
|
|
| 273 |
ctx.restore();
|
| 274 |
}
|
| 275 |
}
|
| 276 |
-
ctx.fillStyle='#02120a';
|
| 277 |
-
ctx.beginPath();
|
| 278 |
-
ctx.arc(ex+dx*4-dy*4,ey+dy*4+dx*4,2.6,0,Math.PI*2);
|
| 279 |
-
ctx.arc(ex+dx*4+dy*4,ey+dy*4-dx*4,2.6,0,Math.PI*2);
|
| 280 |
-
ctx.fill();
|
| 281 |
}
|
| 282 |
}
|
| 283 |
}
|
| 284 |
|
| 285 |
-
// 爆炸粒子 + 冲击波环(加色合成)
|
| 286 |
if(parts.length||rings.length){
|
| 287 |
-
ctx.save();
|
| 288 |
-
ctx.globalCompositeOperation='lighter';
|
| 289 |
-
for(const r of rings){
|
| 290 |
-
const k=r.t/r.life;
|
| 291 |
-
ctx.strokeStyle='rgba('+(r.c||'255,150,180')+','+(0.8*(1-k)).toFixed(3)+')';
|
| 292 |
-
ctx.lineWidth=2.5;
|
| 293 |
-
ctx.beginPath(); ctx.arc(r.x,r.y,Math.max(0.1,r.r),0,Math.PI*2); ctx.stroke();
|
| 294 |
-
}
|
| 295 |
for(const p of parts){
|
| 296 |
const k=1-p.t/p.life;
|
|
|
|
| 297 |
ctx.globalAlpha=Math.max(0,k);
|
| 298 |
ctx.fillStyle=p.c;
|
| 299 |
ctx.beginPath(); ctx.arc(p.x,p.y,p.r*k+0.5,0,Math.PI*2); ctx.fill();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
}
|
| 301 |
-
ctx.restore();
|
| 302 |
}
|
| 303 |
ctx.restore(); // 震屏结束
|
| 304 |
|
|
@@ -316,16 +350,16 @@ function draw(){
|
|
| 316 |
}
|
| 317 |
function drawHUD(){
|
| 318 |
ctx.textBaseline='alphabetic';
|
| 319 |
-
ctx.fillStyle='#
|
| 320 |
ctx.font='700 26px "PingFang SC","Microsoft YaHei",monospace';
|
| 321 |
ctx.textAlign='left';
|
| 322 |
ctx.fillText('SCORE '+G.score,14,44);
|
| 323 |
ctx.textAlign='right';
|
| 324 |
-
ctx.fillStyle='#
|
| 325 |
ctx.font='400 18px "PingFang SC","Microsoft YaHei",monospace';
|
| 326 |
ctx.fillText('BEST '+G.best,canvas.width-14,42);
|
| 327 |
ctx.textAlign='left';
|
| 328 |
-
ctx.fillStyle='#
|
| 329 |
ctx.font='400 15px "PingFang SC","Microsoft YaHei",monospace';
|
| 330 |
ctx.fillText(IS_TOUCH?'滑动转向 · 右上 ⏸ 暂停':'方向键 / WASD 转向 · P 暂停 · 回车 开始',14,76);
|
| 331 |
}
|
|
@@ -337,18 +371,18 @@ function drawMenu(){
|
|
| 337 |
dim();
|
| 338 |
ctx.textAlign='center';
|
| 339 |
ctx.save();
|
| 340 |
-
ctx.shadowColor='
|
| 341 |
-
ctx.fillStyle='#
|
| 342 |
ctx.font='700 44px "PingFang SC","Microsoft YaHei",monospace';
|
| 343 |
ctx.fillText('贪 吃 蛇',canvas.width/2,240);
|
| 344 |
ctx.restore();
|
| 345 |
-
ctx.fillStyle='#
|
| 346 |
ctx.font='400 17px "PingFang SC","Microsoft YaHei",monospace';
|
| 347 |
const lines=IS_TOUCH
|
| 348 |
? ['在棋盘上滑动手指转向','吃到红果加速变长','撞墙或咬到自己就结束']
|
| 349 |
: ['方向键 / WASD 转向','吃到红果加速变长','撞墙或咬到自己就结束'];
|
| 350 |
lines.forEach((t,i)=>ctx.fillText(t,canvas.width/2,300+i*30));
|
| 351 |
-
ctx.fillStyle='#
|
| 352 |
ctx.font='700 19px "PingFang SC","Microsoft YaHei",monospace';
|
| 353 |
ctx.fillText(IS_TOUCH?'点按屏幕开始':'按 回车 开始',canvas.width/2,420);
|
| 354 |
}
|
|
@@ -363,11 +397,11 @@ function drawOver(){
|
|
| 363 |
ctx.scale(Math.max(0.01,sc),Math.max(0.01,sc));
|
| 364 |
ctx.translate(-canvas.width/2,-275);
|
| 365 |
ctx.textAlign='center';
|
| 366 |
-
ctx.shadowColor='#
|
| 367 |
-
ctx.fillStyle='#
|
| 368 |
ctx.font='700 40px "PingFang SC","Microsoft YaHei",monospace';
|
| 369 |
ctx.fillText('游 戏 结 束',canvas.width/2,250);
|
| 370 |
-
ctx.fillStyle='#
|
| 371 |
ctx.font='400 20px "PingFang SC","Microsoft YaHei",monospace';
|
| 372 |
ctx.fillText('得分 '+G.score+' 历史最佳 '+G.best,canvas.width/2,300);
|
| 373 |
ctx.restore();
|
|
@@ -375,10 +409,10 @@ function drawOver(){
|
|
| 375 |
function drawPaused(){
|
| 376 |
dim();
|
| 377 |
ctx.textAlign='center';
|
| 378 |
-
ctx.fillStyle='#
|
| 379 |
ctx.font='700 34px "PingFang SC","Microsoft YaHei",monospace';
|
| 380 |
ctx.fillText('已 暂 停',canvas.width/2,280);
|
| 381 |
-
ctx.fillStyle='#
|
| 382 |
ctx.font='400 17px "PingFang SC","Microsoft YaHei",monospace';
|
| 383 |
ctx.fillText(IS_TOUCH?'点 ⏸ 继续':'按 P 继续',canvas.width/2,326);
|
| 384 |
}
|
|
|
|
| 8 |
<link rel="apple-touch-icon" href="apple-touch-icon.png">
|
| 9 |
<style>
|
| 10 |
*{box-sizing:border-box;-webkit-tap-highlight-color:transparent}
|
| 11 |
+
html,body{margin:0;height:100%;background:#101a0c;overflow:hidden}
|
| 12 |
body{display:flex;align-items:center;justify-content:center;
|
| 13 |
+
background:radial-gradient(1200px 700px at 50% 30%,#2a3d1e 0%,#101a0c 75%)}
|
| 14 |
body.touch{flex-direction:column;justify-content:flex-start;padding-top:8px;overflow-y:auto}
|
| 15 |
canvas{max-height:92vh;max-width:96vw;
|
| 16 |
+
border:2px solid #6b4a26;box-shadow:0 0 40px rgba(20,40,10,.5),0 0 140px rgba(0,0,0,.6)}
|
| 17 |
#stage{position:relative;line-height:0}
|
| 18 |
#retryBtn{position:absolute;left:50%;bottom:10%;transform:translateX(-50%);display:none;z-index:5;
|
| 19 |
font:700 18px "PingFang SC","Microsoft YaHei",monospace;letter-spacing:.3em;text-indent:.3em;
|
| 20 |
+
color:#0d2608;background:linear-gradient(180deg,#a8e063,#56ab2f);border:0;border-radius:999px;
|
| 21 |
+
padding:14px 36px;cursor:pointer;box-shadow:0 4px 18px rgba(40,90,20,.5);touch-action:manipulation}
|
| 22 |
#retryBtn.show{display:block}
|
| 23 |
#retryBtn:active{transform:translateX(-50%) scale(.96)}
|
| 24 |
#pauseBtn{display:none;position:absolute;right:10px;top:10px;z-index:5;
|
| 25 |
+
width:44px;height:44px;border-radius:12px;border:1px solid #6b4a26;
|
| 26 |
+
background:rgba(255,255,255,.10);color:#f0e8d0;font-size:18px;touch-action:manipulation}
|
| 27 |
body.touch #pauseBtn{display:block}
|
| 28 |
</style>
|
| 29 |
</head>
|
|
|
|
| 52 |
flash:0, shake:0, deadAt:0, overT:1 // 死亡结算面板缩放入场计时(纯表现层)
|
| 53 |
};
|
| 54 |
try{ G.best=parseInt(localStorage.getItem('snake_best')||'0',10)||0; }catch(e){}
|
| 55 |
+
/* 草原美术素材:田野背景 / 蛇头精灵 / 鳞片纹理 / 写实苹果(加载失败逐项回退原矢量绘制) */
|
| 56 |
+
const SN_FIELD=new Image(); let SN_FIELD_OK=false;
|
| 57 |
+
SN_FIELD.onload=()=>{ SN_FIELD_OK=true; };
|
| 58 |
+
SN_FIELD.onerror=()=>{ SN_FIELD_OK=false; };
|
| 59 |
+
SN_FIELD.src='art/sn-field.webp';
|
| 60 |
+
const SN_HEAD=new Image(); let SN_HEAD_OK=false;
|
| 61 |
+
SN_HEAD.onload=()=>{ SN_HEAD_OK=true; };
|
| 62 |
+
SN_HEAD.onerror=()=>{ SN_HEAD_OK=false; };
|
| 63 |
+
SN_HEAD.src='art/sn-head.webp';
|
| 64 |
+
const SN_SCALE=new Image(); let SN_SCALE_OK=false;
|
| 65 |
+
SN_SCALE.onload=()=>{ SN_SCALE_OK=true; };
|
| 66 |
+
SN_SCALE.onerror=()=>{ SN_SCALE_OK=false; };
|
| 67 |
+
SN_SCALE.src='art/sn-scale.webp';
|
| 68 |
+
let SN_SCALE_PAT=null;
|
| 69 |
+
const SN_APPLE=new Image(); let SN_APPLE_OK=false;
|
| 70 |
+
SN_APPLE.onload=()=>{ SN_APPLE_OK=true; };
|
| 71 |
+
SN_APPLE.onerror=()=>{ SN_APPLE_OK=false; };
|
| 72 |
+
SN_APPLE.src='art/sn-apple.webp';
|
| 73 |
/* 吃到食物的爆炸粒子 + 冲击波环(不改玩法,只加视觉反馈) */
|
| 74 |
const parts=[], rings=[];
|
| 75 |
function burst(cx,cy){
|
|
|
|
| 84 |
}
|
| 85 |
/* 成就:首次通关 / 破纪录(localStorage arcade_achv) */
|
| 86 |
/* 成就 toast:破纪录弹"新纪录!",首次通关弹 🎖️ 徽章动画(队列依次展示,不改玩法) */
|
| 87 |
+
(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)})();
|
| 88 |
var _achvQ=[],_achvBusy=false;
|
| 89 |
function showAchvToast(kind){_achvQ.push(kind);pumpAchvToast()}
|
| 90 |
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}}
|
|
|
|
| 205 |
ctx.clearRect(0,0,canvas.width,canvas.height);
|
| 206 |
ctx.save();
|
| 207 |
if(G.shake>0) ctx.translate((Math.random()-0.5)*8*G.shake,(Math.random()-0.5)*8*G.shake);
|
| 208 |
+
// 背景:草原田野(加载失败回退深色网格)
|
| 209 |
+
if(SN_FIELD_OK&&SN_FIELD.naturalWidth){
|
| 210 |
+
ctx.drawImage(SN_FIELD,0,0,canvas.width,canvas.height);
|
| 211 |
+
ctx.strokeStyle='#4a2f14'; ctx.lineWidth=3;
|
| 212 |
+
ctx.strokeRect(OX+1,OY+1,COLS*CELL-2,ROWS*CELL-2);
|
| 213 |
+
}else{
|
| 214 |
+
ctx.fillStyle='#050b16';
|
| 215 |
+
ctx.fillRect(OX,OY,COLS*CELL,ROWS*CELL);
|
| 216 |
+
ctx.fillStyle='rgba(34,230,255,.045)';
|
| 217 |
+
for(let y=0;y<ROWS;y++) for(let x=0;x<COLS;x++)
|
| 218 |
+
if((x+y)%2===0) ctx.fillRect(OX+x*CELL,OY+y*CELL,CELL,CELL);
|
| 219 |
+
ctx.strokeStyle='#12324a'; ctx.lineWidth=2;
|
| 220 |
+
ctx.strokeRect(OX,OY,COLS*CELL,ROWS*CELL);
|
| 221 |
+
}
|
| 222 |
|
| 223 |
+
// 食物(写实苹果精灵,带透明通道直接绘制;加载失败回退矢量绘制)
|
| 224 |
if(G.food&&G.state!=='menu'){
|
| 225 |
const fx=OX+G.food.x*CELL, fy=OY+G.food.y*CELL;
|
| 226 |
const pulse=1+Math.sin(performance.now()/180)*0.12;
|
| 227 |
+
if(SN_APPLE_OK&&SN_APPLE.naturalWidth){
|
| 228 |
const s=CELL*1.7*pulse;
|
| 229 |
+
ctx.drawImage(SN_APPLE,fx+CELL/2-s/2,fy+CELL/2-s/2,s,s);
|
|
|
|
|
|
|
|
|
|
| 230 |
}else{
|
| 231 |
ctx.save();
|
| 232 |
+
ctx.shadowColor='rgba(60,20,10,.4)'; ctx.shadowBlur=6;
|
| 233 |
+
ctx.fillStyle='#d93b3b';
|
| 234 |
ctx.beginPath();
|
| 235 |
ctx.arc(fx+CELL/2,fy+CELL/2,(CELL/2-3)*pulse,0,Math.PI*2);
|
| 236 |
ctx.fill();
|
| 237 |
ctx.restore();
|
| 238 |
+
ctx.fillStyle='#6b4a26';
|
| 239 |
+
ctx.fillRect(fx+CELL/2-1,fy+2,2,4); // 果梗
|
| 240 |
+
ctx.fillStyle='#4c9434'; // 叶子
|
| 241 |
+
ctx.beginPath(); ctx.ellipse(fx+CELL/2+4,fy+4,4,2.2,-0.5,0,Math.PI*2); ctx.fill();
|
| 242 |
}
|
| 243 |
}
|
| 244 |
|
| 245 |
+
// 蛇头拖尾(纯表现层:淡金色残影,越新越亮,不改玩法/碰撞)
|
| 246 |
if(G.headTrail&&G.headTrail.length>1&&G.state!=='menu'){
|
| 247 |
ctx.save();
|
| 248 |
ctx.globalCompositeOperation='lighter';
|
| 249 |
const t=G.headTrail, pad=1.5;
|
| 250 |
for(let i=0;i<t.length-1;i++){
|
| 251 |
const k=(i+1)/t.length;
|
| 252 |
+
ctx.globalAlpha=0.03+0.10*k;
|
| 253 |
+
ctx.fillStyle='#fff3b0';
|
| 254 |
const s=(CELL-pad*2)*(0.5+0.5*k);
|
| 255 |
rr(t[i].x-s/2,t[i].y-s/2,s,s,5);
|
| 256 |
ctx.fill();
|
|
|
|
| 266 |
const x=OX+s.x*CELL, y=OY+s.y*CELL, pad=1.5;
|
| 267 |
const head=i===0;
|
| 268 |
ctx.save();
|
| 269 |
+
if(head){ ctx.shadowColor='rgba(20,40,10,.5)'; ctx.shadowBlur=6; }
|
| 270 |
const grad=ctx.createLinearGradient(x,y,x,y+CELL);
|
| 271 |
+
if(head){ grad.addColorStop(0,'#a8e063'); grad.addColorStop(1,'#6fbf4a'); }
|
| 272 |
+
else{ grad.addColorStop(0,'#8fd45e'); grad.addColorStop(1,'#5aa73f'); }
|
| 273 |
ctx.fillStyle=grad;
|
| 274 |
rr(x+pad,y+pad,CELL-pad*2,CELL-pad*2,5);
|
| 275 |
ctx.fill();
|
| 276 |
ctx.restore();
|
| 277 |
+
if(SN_SCALE_OK&&SN_SCALE.naturalWidth){ // 鳞片纹理叠加
|
| 278 |
+
if(!SN_SCALE_PAT) SN_SCALE_PAT=ctx.createPattern(SN_SCALE,'repeat');
|
| 279 |
+
if(SN_SCALE_PAT){
|
| 280 |
+
ctx.save(); ctx.globalAlpha=.16; ctx.fillStyle=SN_SCALE_PAT;
|
| 281 |
+
rr(x+pad,y+pad,CELL-pad*2,CELL-pad*2,5); ctx.fill(); ctx.restore();
|
| 282 |
+
}
|
| 283 |
+
}
|
| 284 |
+
if(head){ // 蛇头精灵朝移动方向旋转,回退则画眼睛
|
| 285 |
const dx=G.dir.x, dy=G.dir.y;
|
| 286 |
const ex=x+CELL/2, ey=y+CELL/2;
|
| 287 |
+
if(SN_HEAD_OK&&SN_HEAD.naturalWidth){
|
| 288 |
+
const hs=CELL*1.6;
|
| 289 |
+
ctx.save(); ctx.translate(ex,ey); ctx.rotate(Math.atan2(dy,dx));
|
| 290 |
+
ctx.drawImage(SN_HEAD,-hs/2,-hs/2,hs,hs); ctx.restore();
|
| 291 |
+
}else{
|
| 292 |
+
ctx.fillStyle='#1a330e';
|
| 293 |
+
ctx.beginPath();
|
| 294 |
+
ctx.arc(ex+dx*4-dy*4,ey+dy*4+dx*4,2.6,0,Math.PI*2);
|
| 295 |
+
ctx.arc(ex+dx*4+dy*4,ey+dy*4-dx*4,2.6,0,Math.PI*2);
|
| 296 |
+
ctx.fill();
|
| 297 |
+
}
|
| 298 |
if(G.headGlowT){ // 吃红果加速时蛇头短暂发亮(纯表现层,不改速度/碰撞)
|
| 299 |
const ga=performance.now()-G.headGlowT;
|
| 300 |
if(ga<600){
|
|
|
|
| 308 |
ctx.restore();
|
| 309 |
}
|
| 310 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
}
|
| 312 |
}
|
| 313 |
}
|
| 314 |
|
| 315 |
+
// 爆炸粒子(普通合成,草地上加色会过曝) + 冲击波环(加色合成)
|
| 316 |
if(parts.length||rings.length){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
for(const p of parts){
|
| 318 |
const k=1-p.t/p.life;
|
| 319 |
+
ctx.save();
|
| 320 |
ctx.globalAlpha=Math.max(0,k);
|
| 321 |
ctx.fillStyle=p.c;
|
| 322 |
ctx.beginPath(); ctx.arc(p.x,p.y,p.r*k+0.5,0,Math.PI*2); ctx.fill();
|
| 323 |
+
ctx.restore();
|
| 324 |
+
}
|
| 325 |
+
if(rings.length){
|
| 326 |
+
ctx.save();
|
| 327 |
+
ctx.globalCompositeOperation='lighter';
|
| 328 |
+
for(const r of rings){
|
| 329 |
+
const k=r.t/r.life;
|
| 330 |
+
ctx.strokeStyle='rgba('+(r.c||'255,150,180')+','+(0.8*(1-k)).toFixed(3)+')';
|
| 331 |
+
ctx.lineWidth=2.5;
|
| 332 |
+
ctx.beginPath(); ctx.arc(r.x,r.y,Math.max(0.1,r.r),0,Math.PI*2); ctx.stroke();
|
| 333 |
+
}
|
| 334 |
+
ctx.restore();
|
| 335 |
}
|
|
|
|
| 336 |
}
|
| 337 |
ctx.restore(); // 震屏结束
|
| 338 |
|
|
|
|
| 350 |
}
|
| 351 |
function drawHUD(){
|
| 352 |
ctx.textBaseline='alphabetic';
|
| 353 |
+
ctx.fillStyle='#fff8e0';
|
| 354 |
ctx.font='700 26px "PingFang SC","Microsoft YaHei",monospace';
|
| 355 |
ctx.textAlign='left';
|
| 356 |
ctx.fillText('SCORE '+G.score,14,44);
|
| 357 |
ctx.textAlign='right';
|
| 358 |
+
ctx.fillStyle='#e8d5a8';
|
| 359 |
ctx.font='400 18px "PingFang SC","Microsoft YaHei",monospace';
|
| 360 |
ctx.fillText('BEST '+G.best,canvas.width-14,42);
|
| 361 |
ctx.textAlign='left';
|
| 362 |
+
ctx.fillStyle='#c9b183';
|
| 363 |
ctx.font='400 15px "PingFang SC","Microsoft YaHei",monospace';
|
| 364 |
ctx.fillText(IS_TOUCH?'滑动转向 · 右上 ⏸ 暂停':'方向键 / WASD 转向 · P 暂停 · 回车 开始',14,76);
|
| 365 |
}
|
|
|
|
| 371 |
dim();
|
| 372 |
ctx.textAlign='center';
|
| 373 |
ctx.save();
|
| 374 |
+
ctx.shadowColor='rgba(60,120,30,.9)'; ctx.shadowBlur=24;
|
| 375 |
+
ctx.fillStyle='#d8f3a0';
|
| 376 |
ctx.font='700 44px "PingFang SC","Microsoft YaHei",monospace';
|
| 377 |
ctx.fillText('贪 吃 蛇',canvas.width/2,240);
|
| 378 |
ctx.restore();
|
| 379 |
+
ctx.fillStyle='#f0e8d0';
|
| 380 |
ctx.font='400 17px "PingFang SC","Microsoft YaHei",monospace';
|
| 381 |
const lines=IS_TOUCH
|
| 382 |
? ['在棋盘上滑动手指转向','吃到红果加速变长','撞墙或咬到自己就结束']
|
| 383 |
: ['方向键 / WASD 转向','吃到红果加速变长','撞墙或咬到自己就结束'];
|
| 384 |
lines.forEach((t,i)=>ctx.fillText(t,canvas.width/2,300+i*30));
|
| 385 |
+
ctx.fillStyle='#ffe9a8';
|
| 386 |
ctx.font='700 19px "PingFang SC","Microsoft YaHei",monospace';
|
| 387 |
ctx.fillText(IS_TOUCH?'点按屏幕开始':'按 回车 开始',canvas.width/2,420);
|
| 388 |
}
|
|
|
|
| 397 |
ctx.scale(Math.max(0.01,sc),Math.max(0.01,sc));
|
| 398 |
ctx.translate(-canvas.width/2,-275);
|
| 399 |
ctx.textAlign='center';
|
| 400 |
+
ctx.shadowColor='#c0392b'; ctx.shadowBlur=26;
|
| 401 |
+
ctx.fillStyle='#e86a5a';
|
| 402 |
ctx.font='700 40px "PingFang SC","Microsoft YaHei",monospace';
|
| 403 |
ctx.fillText('游 戏 结 束',canvas.width/2,250);
|
| 404 |
+
ctx.fillStyle='#f0e8d0';
|
| 405 |
ctx.font='400 20px "PingFang SC","Microsoft YaHei",monospace';
|
| 406 |
ctx.fillText('得分 '+G.score+' 历史最佳 '+G.best,canvas.width/2,300);
|
| 407 |
ctx.restore();
|
|
|
|
| 409 |
function drawPaused(){
|
| 410 |
dim();
|
| 411 |
ctx.textAlign='center';
|
| 412 |
+
ctx.fillStyle='#fff8e0';
|
| 413 |
ctx.font='700 34px "PingFang SC","Microsoft YaHei",monospace';
|
| 414 |
ctx.fillText('已 暂 停',canvas.width/2,280);
|
| 415 |
+
ctx.fillStyle='#f0e8d0';
|
| 416 |
ctx.font='400 17px "PingFang SC","Microsoft YaHei",monospace';
|
| 417 |
ctx.fillText(IS_TOUCH?'点 ⏸ 继续':'按 P 继续',canvas.width/2,326);
|
| 418 |
}
|