ymo2017 commited on
Commit
0b5daa2
·
verified ·
1 Parent(s): 7b3a8ce

新增贪吃蛇游戏(触屏滑动操作)

Browse files
Files changed (2) hide show
  1. index.html +9 -0
  2. snake.html +334 -0
index.html CHANGED
@@ -139,6 +139,15 @@
139
  <span class="play">开始游戏</span>
140
  </div>
141
  </a>
 
 
 
 
 
 
 
 
 
142
  </main>
143
  <footer>INSERT COIN · HAVE FUN</footer>
144
  </body>
 
139
  <span class="play">开始游戏</span>
140
  </div>
141
  </a>
142
+ <a class="card" href="snake.html">
143
+ <div class="art"><img src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='640' height='360' viewBox='0 0 640 360'%3E%3Cdefs%3E%3CradialGradient id='g' cx='50%25' cy='40%25' r='80%25'%3E%3Cstop offset='0%25' stop-color='%230a1a2e'/%3E%3Cstop offset='100%25' stop-color='%23020409'/%3E%3C/radialGradient%3E%3C/defs%3E%3Crect width='640' height='360' fill='url(%23g)'/%3E%3Cg fill='none' stroke='%230f2b44' stroke-width='1'%3E%3Cpath d='M40 20v320M120 20v320M200 20v320M280 20v320M360 20v320M440 20v320M520 20v320M600 20v320'/%3E%3Cpath d='M20 40h600M20 120h600M20 200h600M20 280h600M20 340h600'/%3E%3C/g%3E%3Cpath d='M120 220h160v-60h120v80h100' fill='none' stroke='%234dff7a' stroke-width='26' stroke-linecap='round' stroke-linejoin='round'/%3E%3Ccircle cx='500' cy='220' r='20' fill='%23ff5c78'/%3E%3C/svg%3E" alt="贪吃蛇"></div>
144
+ <div class="body">
145
+ <h2>贪吃蛇</h2>
146
+ <p>霓虹版贪吃蛇:吃红果加速变长,撞墙或咬到自己就结束,看看你能拿多少分。</p>
147
+ <div class="keys"><span>触屏滑动转向</span><span>方向键 / WASD</span><span>P 暂停</span></div>
148
+ <span class="play">开始游戏</span>
149
+ </div>
150
+ </a>
151
  </main>
152
  <footer>INSERT COIN · HAVE FUN</footer>
153
  </body>
snake.html ADDED
@@ -0,0 +1,334 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!doctype html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <title>贪吃蛇</title>
7
+ <style>
8
+ *{box-sizing:border-box;-webkit-tap-highlight-color:transparent}
9
+ html,body{margin:0;height:100%;background:#020409;overflow:hidden}
10
+ body{display:flex;align-items:center;justify-content:center;
11
+ background:radial-gradient(1200px 700px at 50% 30%,#0a1424 0%,#020409 70%)}
12
+ body.touch{flex-direction:column;justify-content:flex-start;padding-top:8px;overflow-y:auto}
13
+ canvas{max-height:92vh;max-width:96vw;
14
+ border:1px solid #12324a;box-shadow:0 0 60px rgba(34,230,255,.12),0 0 140px rgba(0,0,0,.8)}
15
+ #stage{position:relative;line-height:0}
16
+ #retryBtn{position:absolute;left:50%;bottom:10%;transform:translateX(-50%);display:none;z-index:5;
17
+ font:700 18px "PingFang SC","Microsoft YaHei",monospace;letter-spacing:.3em;text-indent:.3em;
18
+ color:#03141b;background:linear-gradient(180deg,#4ff7ff,#00c8e0);border:0;border-radius:999px;
19
+ padding:14px 36px;cursor:pointer;box-shadow:0 0 26px rgba(0,229,255,.55);touch-action:manipulation}
20
+ #retryBtn.show{display:block}
21
+ #retryBtn:active{transform:translateX(-50%) scale(.96)}
22
+ #pauseBtn{display:none;position:absolute;right:10px;top:10px;z-index:5;
23
+ width:44px;height:44px;border-radius:12px;border:1px solid #2a5a7c;
24
+ background:rgba(34,230,255,.08);color:#cdeefc;font-size:18px;touch-action:manipulation}
25
+ body.touch #pauseBtn{display:block}
26
+ </style>
27
+ </head>
28
+ <body>
29
+ <div id="stage">
30
+ <canvas id="game" width="420" height="520"></canvas>
31
+ <button id="retryBtn" type="button">再来一局</button>
32
+ <button id="pauseBtn" type="button" aria-label="暂停">⏸</button>
33
+ </div>
34
+ <script>
35
+ 'use strict';
36
+ /* ================= 配置 ================= */
37
+ const COLS=21, ROWS=21, CELL=20;
38
+ const OX=0, OY=100; // 棋盘左上角:canvas 420x520,顶部 100px 留给 HUD
39
+ const canvas=document.getElementById('game');
40
+ const ctx=canvas.getContext('2d');
41
+ const retryBtn=document.getElementById('retryBtn');
42
+ const pauseBtn=document.getElementById('pauseBtn');
43
+ const IS_TOUCH=('ontouchstart' in window)||(navigator.maxTouchPoints>0);
44
+ if(IS_TOUCH) document.body.classList.add('touch');
45
+
46
+ const G={
47
+ state:'menu', paused:false,
48
+ snake:[], dir:{x:1,y:0}, queued:null,
49
+ food:null, score:0, best:0, stepMs:150, acc:0, last:0,
50
+ flash:0, deadAt:0
51
+ };
52
+ try{ G.best=parseInt(localStorage.getItem('snake_best')||'0',10)||0; }catch(e){}
53
+
54
+ /* ================= 音频(Web Audio 合成,首次交互初始化) ================= */
55
+ let AC=null;
56
+ function audio(){
57
+ if(!AC){ try{ AC=new (window.AudioContext||window.webkitAudioContext)(); }catch(e){ return null; } }
58
+ if(AC && AC.state==='suspended') AC.resume();
59
+ return AC;
60
+ }
61
+ function tone(freq,dur,opt){
62
+ const ac=audio(); if(!ac) return;
63
+ opt=opt||{};
64
+ const t=ac.currentTime+(opt.delay||0);
65
+ const o=ac.createOscillator(), g=ac.createGain();
66
+ o.type=opt.type||'square'; o.frequency.setValueAtTime(freq,t);
67
+ if(opt.slide) o.frequency.exponentialRampToValueAtTime(opt.slide,t+dur);
68
+ g.gain.setValueAtTime(0.0001,t);
69
+ g.gain.exponentialRampToValueAtTime(opt.vol||0.08,t+0.008);
70
+ g.gain.exponentialRampToValueAtTime(0.0001,t+dur);
71
+ o.connect(g); g.connect(ac.destination);
72
+ o.start(t); o.stop(t+dur+0.02);
73
+ }
74
+ const SFX={
75
+ eat(){ tone(520,0.07,{vol:0.10,slide:880}); tone(880,0.06,{vol:0.08,delay:0.05,slide:1320}); },
76
+ die(){ tone(300,0.35,{vol:0.12,type:'sawtooth',slide:60}); },
77
+ turn(){ tone(260,0.04,{vol:0.05}); }
78
+ };
79
+
80
+ /* ================= 游戏逻辑 ================= */
81
+ function reset(){
82
+ const cx=Math.floor(COLS/2), cy=Math.floor(ROWS/2);
83
+ G.snake=[{x:cx,y:cy},{x:cx-1,y:cy},{x:cx-2,y:cy}];
84
+ G.dir={x:1,y:0}; G.queued=null;
85
+ G.score=0; G.stepMs=150; G.acc=0; G.paused=false; G.flash=0;
86
+ retryBtn.classList.remove('show');
87
+ spawnFood();
88
+ }
89
+ function start(){
90
+ reset();
91
+ G.state='playing';
92
+ }
93
+ function spawnFood(){
94
+ const taken=new Set(G.snake.map(s=>s.x+','+s.y));
95
+ const free=[];
96
+ for(let y=0;y<ROWS;y++) for(let x=0;x<COLS;x++)
97
+ if(!taken.has(x+','+y)) free.push({x,y});
98
+ if(!free.length){ G.food=null; return; }
99
+ G.food=free[(Math.random()*free.length)|0];
100
+ }
101
+ function setDir(x,y){
102
+ if(G.state!=='playing'||G.paused) return;
103
+ const cur=G.queued||G.dir;
104
+ if(x===-cur.x&&y===-cur.y) return; // 禁止 180° 掉头
105
+ if(x===cur.x&&y===cur.y) return;
106
+ G.queued={x,y}; SFX.turn();
107
+ }
108
+ function step(){
109
+ if(G.queued){ G.dir=G.queued; G.queued=null; }
110
+ const head={x:G.snake[0].x+G.dir.x, y:G.snake[0].y+G.dir.y};
111
+ if(head.x<0||head.y<0||head.x>=COLS||head.y>=ROWS||
112
+ G.snake.some(s=>s.x===head.x&&s.y===head.y)){
113
+ G.state='over'; G.deadAt=performance.now();
114
+ retryBtn.classList.add('show');
115
+ if(G.score>G.best){ G.best=G.score;
116
+ try{ localStorage.setItem('snake_best',String(G.best)); }catch(e){} }
117
+ SFX.die();
118
+ return;
119
+ }
120
+ G.snake.unshift(head);
121
+ if(G.food&&head.x===G.food.x&&head.y===G.food.y){
122
+ G.score+=10;
123
+ G.stepMs=Math.max(70,150-G.score/10*4); // 吃得越多越快
124
+ G.flash=1;
125
+ SFX.eat();
126
+ spawnFood();
127
+ }else{
128
+ G.snake.pop();
129
+ }
130
+ }
131
+ function update(dt){
132
+ if(G.state!=='playing'||G.paused) return;
133
+ G.acc+=dt;
134
+ while(G.acc>=G.stepMs){ G.acc-=G.stepMs; step();
135
+ if(G.state!=='playing') break; }
136
+ if(G.flash>0) G.flash=Math.max(0,G.flash-dt/300);
137
+ }
138
+
139
+ /* ================= 渲染 ================= */
140
+ function rr(x,y,w,h,r){
141
+ ctx.beginPath();
142
+ ctx.moveTo(x+r,y);
143
+ ctx.arcTo(x+w,y,x+w,y+h,r); ctx.arcTo(x+w,y+h,x,y+h,r);
144
+ ctx.arcTo(x,y+h,x,y,r); ctx.arcTo(x,y,x+w,y,r);
145
+ ctx.closePath();
146
+ }
147
+ function draw(){
148
+ ctx.clearRect(0,0,canvas.width,canvas.height);
149
+ // 背景网格
150
+ ctx.fillStyle='#050b16';
151
+ ctx.fillRect(OX,OY,COLS*CELL,ROWS*CELL);
152
+ ctx.fillStyle='rgba(34,230,255,.045)';
153
+ for(let y=0;y<ROWS;y++) for(let x=0;x<COLS;x++)
154
+ if((x+y)%2===0) ctx.fillRect(OX+x*CELL,OY+y*CELL,CELL,CELL);
155
+ ctx.strokeStyle='#12324a'; ctx.lineWidth=2;
156
+ ctx.strokeRect(OX,OY,COLS*CELL,ROWS*CELL);
157
+
158
+ // 食物
159
+ if(G.food&&G.state!=='menu'){
160
+ const fx=OX+G.food.x*CELL, fy=OY+G.food.y*CELL;
161
+ const pulse=1+Math.sin(performance.now()/180)*0.12;
162
+ ctx.save();
163
+ ctx.shadowColor='#ff4d6d'; ctx.shadowBlur=18;
164
+ ctx.fillStyle='#ff5c78';
165
+ ctx.beginPath();
166
+ ctx.arc(fx+CELL/2,fy+CELL/2,(CELL/2-3)*pulse,0,Math.PI*2);
167
+ ctx.fill();
168
+ ctx.restore();
169
+ ctx.fillStyle='#ffd93b';
170
+ ctx.fillRect(fx+CELL/2-1.5,fy+3,3,4); // 果梗
171
+ }
172
+
173
+ // 蛇
174
+ if(G.state!=='menu'){
175
+ const n=G.snake.length;
176
+ for(let i=n-1;i>=0;i--){
177
+ const s=G.snake[i];
178
+ const x=OX+s.x*CELL, y=OY+s.y*CELL, pad=1.5;
179
+ const head=i===0;
180
+ ctx.save();
181
+ if(head){ ctx.shadowColor='#4dff7a'; ctx.shadowBlur=16; }
182
+ const grad=ctx.createLinearGradient(x,y,x,y+CELL);
183
+ if(head){ grad.addColorStop(0,'#8dff9f'); grad.addColorStop(1,'#2fd65a'); }
184
+ else{ grad.addColorStop(0,'#3fe06e'); grad.addColorStop(1,'#1fae4c'); }
185
+ ctx.fillStyle=grad;
186
+ rr(x+pad,y+pad,CELL-pad*2,CELL-pad*2,5);
187
+ ctx.fill();
188
+ ctx.restore();
189
+ if(head){ // 眼睛:朝向移动方向
190
+ const dx=G.dir.x, dy=G.dir.y;
191
+ const ex=x+CELL/2, ey=y+CELL/2;
192
+ ctx.fillStyle='#02120a';
193
+ ctx.beginPath();
194
+ ctx.arc(ex+dx*4-dy*4,ey+dy*4+dx*4,2.6,0,Math.PI*2);
195
+ ctx.arc(ex+dx*4+dy*4,ey+dy*4-dx*4,2.6,0,Math.PI*2);
196
+ ctx.fill();
197
+ }
198
+ }
199
+ }
200
+
201
+ drawHUD();
202
+
203
+ if(G.state==='menu') drawMenu();
204
+ else if(G.state==='over') drawOver();
205
+ else if(G.paused) drawPaused();
206
+ }
207
+ function drawHUD(){
208
+ ctx.textBaseline='alphabetic';
209
+ ctx.fillStyle='#22e6ff';
210
+ ctx.font='700 26px "PingFang SC","Microsoft YaHei",monospace';
211
+ ctx.textAlign='left';
212
+ ctx.fillText('SCORE '+G.score,14,44);
213
+ ctx.textAlign='right';
214
+ ctx.fillStyle='#7fa8c9';
215
+ ctx.font='400 18px "PingFang SC","Microsoft YaHei",monospace';
216
+ ctx.fillText('BEST '+G.best,canvas.width-14,42);
217
+ ctx.textAlign='left';
218
+ ctx.fillStyle='#3d6079';
219
+ ctx.font='400 15px "PingFang SC","Microsoft YaHei",monospace';
220
+ ctx.fillText(IS_TOUCH?'滑动转向 · 右上 ⏸ 暂停':'方向键 / WASD 转向 · P 暂停 · 回车 开始',14,76);
221
+ }
222
+ function dim(){
223
+ ctx.fillStyle='rgba(2,8,16,.72)';
224
+ ctx.fillRect(0,0,canvas.width,canvas.height);
225
+ }
226
+ function drawMenu(){
227
+ dim();
228
+ ctx.textAlign='center';
229
+ ctx.save();
230
+ ctx.shadowColor='#4dff7a'; ctx.shadowBlur=30;
231
+ ctx.fillStyle='#8dff9f';
232
+ ctx.font='700 44px "PingFang SC","Microsoft YaHei",monospace';
233
+ ctx.fillText('贪 吃 蛇',canvas.width/2,240);
234
+ ctx.restore();
235
+ ctx.fillStyle='#9fd8ef';
236
+ ctx.font='400 17px "PingFang SC","Microsoft YaHei",monospace';
237
+ const lines=IS_TOUCH
238
+ ? ['在棋盘上滑动手指转向','吃到红果加速变长','撞墙或咬到自己就结束']
239
+ : ['方向键 / WASD 转向','吃到红果加速变长','撞墙或咬到自己就结束'];
240
+ lines.forEach((t,i)=>ctx.fillText(t,canvas.width/2,300+i*30));
241
+ ctx.fillStyle='#22e6ff';
242
+ ctx.font='700 19px "PingFang SC","Microsoft YaHei",monospace';
243
+ ctx.fillText(IS_TOUCH?'点按屏幕开始':'按 回车 开始',canvas.width/2,420);
244
+ }
245
+ function drawOver(){
246
+ dim();
247
+ ctx.textAlign='center';
248
+ ctx.save();
249
+ ctx.shadowColor='#ff4d6d'; ctx.shadowBlur=26;
250
+ ctx.fillStyle='#ff7a8f';
251
+ ctx.font='700 40px "PingFang SC","Microsoft YaHei",monospace';
252
+ ctx.fillText('游 戏 结 束',canvas.width/2,250);
253
+ ctx.restore();
254
+ ctx.fillStyle='#9fd8ef';
255
+ ctx.font='400 20px "PingFang SC","Microsoft YaHei",monospace';
256
+ ctx.fillText('得分 '+G.score+' 历史最佳 '+G.best,canvas.width/2,300);
257
+ }
258
+ function drawPaused(){
259
+ dim();
260
+ ctx.textAlign='center';
261
+ ctx.fillStyle='#22e6ff';
262
+ ctx.font='700 34px "PingFang SC","Microsoft YaHei",monospace';
263
+ ctx.fillText('已 暂 停',canvas.width/2,280);
264
+ ctx.fillStyle='#9fd8ef';
265
+ ctx.font='400 17px "PingFang SC","Microsoft YaHei",monospace';
266
+ ctx.fillText(IS_TOUCH?'点 ⏸ 继续':'按 P 继续',canvas.width/2,326);
267
+ }
268
+
269
+ /* ================= 主循环 ================= */
270
+ let rafId=0;
271
+ function tick(ts){
272
+ const dt=Math.min(100,ts-(G.last||ts));
273
+ G.last=ts;
274
+ update(dt);
275
+ draw();
276
+ rafId=requestAnimationFrame(tick);
277
+ }
278
+ rafId=requestAnimationFrame(tick);
279
+
280
+ /* ================= 输入 ================= */
281
+ window.addEventListener('keydown',e=>{
282
+ const k=e.key;
283
+ if(['ArrowUp','ArrowDown','ArrowLeft','ArrowRight',' '].includes(k)) e.preventDefault();
284
+ audio();
285
+ if(k==='p'||k==='P'){ togglePause(); return; }
286
+ if(k==='Enter'){
287
+ if(G.state==='menu'||G.state==='over') start();
288
+ return;
289
+ }
290
+ if(G.state==='menu'){ start(); }
291
+ if(k==='ArrowUp'||k==='w'||k==='W') setDir(0,-1);
292
+ else if(k==='ArrowDown'||k==='s'||k==='S') setDir(0,1);
293
+ else if(k==='ArrowLeft'||k==='a'||k==='A') setDir(-1,0);
294
+ else if(k==='ArrowRight'||k==='d'||k==='D') setDir(1,0);
295
+ });
296
+ function togglePause(){
297
+ if(G.state!=='playing') return;
298
+ G.paused=!G.paused;
299
+ }
300
+ pauseBtn.addEventListener('click',e=>{ e.preventDefault(); audio(); togglePause(); });
301
+ retryBtn.addEventListener('click',e=>{ e.preventDefault(); audio(); start(); });
302
+
303
+ /* 触屏:点按开始 / 滑动转向 */
304
+ let touchStart=null;
305
+ canvas.addEventListener('touchstart',e=>{
306
+ audio();
307
+ const t=e.changedTouches[0];
308
+ touchStart={x:t.clientX,y:t.clientY,moved:false};
309
+ },{passive:true});
310
+ canvas.addEventListener('touchmove',e=>{
311
+ if(!touchStart) return;
312
+ e.preventDefault();
313
+ const t=e.changedTouches[0];
314
+ const dx=t.clientX-touchStart.x, dy=t.clientY-touchStart.y;
315
+ if(Math.abs(dx)<24&&Math.abs(dy)<24) return;
316
+ touchStart.moved=true;
317
+ if(Math.abs(dx)>Math.abs(dy)) setDir(dx>0?1:-1,0);
318
+ else setDir(0,dy>0?1:-1);
319
+ touchStart={x:t.clientX,y:t.clientY,moved:true}; // 连续滑动可多次转向
320
+ },{passive:false});
321
+ canvas.addEventListener('touchend',e=>{
322
+ if(touchStart&&!touchStart.moved){
323
+ if(G.state==='menu'||G.state==='over') start();
324
+ }
325
+ touchStart=null;
326
+ },{passive:true});
327
+ canvas.addEventListener('mousedown',()=>{
328
+ audio();
329
+ if(G.state==='menu'||G.state==='over') start();
330
+ });
331
+ window.addEventListener('blur',()=>{ if(G.state==='playing') G.paused=true; });
332
+ </script>
333
+ </body>
334
+ </html>