feat: 霓虹贪吃蛇 v1.0(单文件 HTML5 游戏)
This commit is contained in:
+563
@@ -0,0 +1,563 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh-CN">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
|
||||||
|
<meta name="theme-color" content="#050810">
|
||||||
|
<title>霓虹贪吃蛇 · Neon Snake</title>
|
||||||
|
<style>
|
||||||
|
:root{
|
||||||
|
--neon:#39ffd0;
|
||||||
|
--violet:#7c5cff;
|
||||||
|
--gold:#ffd54a;
|
||||||
|
--bg:#050810;
|
||||||
|
--muted:#9fb3cc;
|
||||||
|
}
|
||||||
|
*{box-sizing:border-box}
|
||||||
|
html,body{height:100%}
|
||||||
|
body{
|
||||||
|
margin:0;padding:14px 14px 18px;
|
||||||
|
min-height:100dvh;
|
||||||
|
overflow-x:hidden;
|
||||||
|
display:flex;flex-direction:column;align-items:center;gap:10px;
|
||||||
|
background:radial-gradient(circle at 50% 0%, #10203f 0%, var(--bg) 62%);
|
||||||
|
color:#e8f4ff;
|
||||||
|
font-family:ui-sans-serif,system-ui,-apple-system,"PingFang SC","Microsoft YaHei",sans-serif;
|
||||||
|
-webkit-tap-highlight-color:transparent;
|
||||||
|
user-select:none;
|
||||||
|
}
|
||||||
|
header{width:100%;max-width:672px;display:flex;align-items:center;justify-content:space-between;gap:10px}
|
||||||
|
h1{
|
||||||
|
margin:0;font-size:clamp(15px,4vw,19px);font-weight:600;letter-spacing:.16em;
|
||||||
|
background:linear-gradient(90deg,var(--neon),var(--violet));
|
||||||
|
-webkit-background-clip:text;background-clip:text;color:transparent;
|
||||||
|
white-space:nowrap;
|
||||||
|
}
|
||||||
|
.right{display:flex;align-items:center;gap:12px}
|
||||||
|
.stats{display:flex;gap:12px;font-size:13px;font-variant-numeric:tabular-nums;color:#c8dcf0}
|
||||||
|
.stats .v{color:var(--neon);font-weight:700}
|
||||||
|
.stats .best .v{color:var(--gold)}
|
||||||
|
button{font:inherit;color:inherit;cursor:pointer;border-radius:10px}
|
||||||
|
.icon{
|
||||||
|
background:rgba(124,92,255,.15);border:1px solid rgba(124,92,255,.45);
|
||||||
|
padding:6px 10px;font-size:13px;line-height:1;
|
||||||
|
transition:background .15s ease;
|
||||||
|
}
|
||||||
|
.icon:hover{background:rgba(124,92,255,.32)}
|
||||||
|
.stage{
|
||||||
|
position:relative;
|
||||||
|
width:min(94vw, 672px, calc(100dvh - 265px));
|
||||||
|
min-width:240px;
|
||||||
|
aspect-ratio:1/1;
|
||||||
|
}
|
||||||
|
canvas{
|
||||||
|
width:100%;height:100%;display:block;border-radius:16px;
|
||||||
|
border:1px solid rgba(57,255,208,.25);
|
||||||
|
box-shadow:0 0 60px rgba(57,255,208,.12), inset 0 0 60px rgba(0,0,0,.6);
|
||||||
|
touch-action:none;
|
||||||
|
}
|
||||||
|
#overlay{
|
||||||
|
position:absolute;inset:0;border-radius:16px;padding:22px;
|
||||||
|
display:flex;flex-direction:column;align-items:center;justify-content:center;gap:12px;
|
||||||
|
text-align:center;background:rgba(5,8,16,.8);backdrop-filter:blur(6px);
|
||||||
|
}
|
||||||
|
#overlay.hidden{display:none}
|
||||||
|
#overlay h2{margin:0;font-size:clamp(19px,5vw,28px);letter-spacing:.06em}
|
||||||
|
#overlay p{margin:0;color:var(--muted);font-size:14px;line-height:1.8;max-width:34ch}
|
||||||
|
#overlay b{color:var(--neon)}
|
||||||
|
#playBtn{
|
||||||
|
margin-top:6px;padding:12px 32px;border:none;border-radius:999px;
|
||||||
|
font-weight:700;letter-spacing:.12em;color:#04120e;
|
||||||
|
background:linear-gradient(135deg,var(--neon),#4ad9ff);
|
||||||
|
box-shadow:0 8px 30px rgba(57,255,208,.35);
|
||||||
|
transition:transform .12s ease;
|
||||||
|
}
|
||||||
|
#playBtn:active{transform:scale(.96)}
|
||||||
|
.pad{
|
||||||
|
display:grid;gap:8px;
|
||||||
|
grid-template-columns:repeat(3,54px);
|
||||||
|
grid-template-rows:repeat(2,54px);
|
||||||
|
touch-action:none;
|
||||||
|
}
|
||||||
|
.padBtn{
|
||||||
|
border:1px solid rgba(57,255,208,.3);
|
||||||
|
background:rgba(57,255,208,.08);
|
||||||
|
color:var(--neon);font-size:18px;
|
||||||
|
display:flex;align-items:center;justify-content:center;
|
||||||
|
}
|
||||||
|
.padBtn:active{background:rgba(57,255,208,.3)}
|
||||||
|
footer{color:#5f748c;font-size:12px;text-align:center;line-height:1.7}
|
||||||
|
@media (hover:hover) and (pointer:fine){
|
||||||
|
.pad{display:none}
|
||||||
|
.stage{width:min(94vw, 672px, calc(100dvh - 150px))}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header>
|
||||||
|
<h1>霓虹贪吃蛇</h1>
|
||||||
|
<div class="right">
|
||||||
|
<div class="stats">
|
||||||
|
<span>分数 <span class="v" id="score">0</span></span>
|
||||||
|
<span class="best">最高 <span class="v" id="best">0</span></span>
|
||||||
|
</div>
|
||||||
|
<button class="icon" id="muteBtn" title="音效开关">🔊</button>
|
||||||
|
</div>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
<div class="stage">
|
||||||
|
<canvas id="game"></canvas>
|
||||||
|
<div id="overlay">
|
||||||
|
<h2 id="ovTitle">霓虹贪吃蛇</h2>
|
||||||
|
<p id="ovText">方向键 / WASD 控制方向<br>橙色方块 +10 分,金色方块 +30 分但会消失<br>撞墙或咬到自己就结束了</p>
|
||||||
|
<button id="playBtn">开始游戏</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="pad">
|
||||||
|
<button class="padBtn" data-dir="up" style="grid-area:1/2">▲</button>
|
||||||
|
<button class="padBtn" data-dir="left" style="grid-area:2/1">◀</button>
|
||||||
|
<button class="padBtn" data-dir="down" style="grid-area:2/2">▼</button>
|
||||||
|
<button class="padBtn" data-dir="right" style="grid-area:2/3">▶</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<footer>方向键 / WASD 移动 · 空格 开始与暂停 · P 暂停</footer>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
(() => {
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
/* ---------- 基础常量 ---------- */
|
||||||
|
const canvas = document.getElementById('game');
|
||||||
|
const ctx = canvas.getContext('2d');
|
||||||
|
const COLS = 24, ROWS = 24, CELL = 28;
|
||||||
|
const W = COLS * CELL, H = ROWS * CELL;
|
||||||
|
canvas.width = W; canvas.height = H;
|
||||||
|
|
||||||
|
const GOLD_LIFE = 8000; // 金色方块存活时间(ms)
|
||||||
|
const BASE_TICK = 135; // 初始每步耗时(ms)
|
||||||
|
const MIN_TICK = 62; // 最快每步耗时(ms)
|
||||||
|
|
||||||
|
const overlay = document.getElementById('overlay');
|
||||||
|
const ovTitle = document.getElementById('ovTitle');
|
||||||
|
const ovText = document.getElementById('ovText');
|
||||||
|
const playBtn = document.getElementById('playBtn');
|
||||||
|
const scoreEl = document.getElementById('score');
|
||||||
|
const bestEl = document.getElementById('best');
|
||||||
|
const muteBtn = document.getElementById('muteBtn');
|
||||||
|
|
||||||
|
/* ---------- 状态 ---------- */
|
||||||
|
let snake, dir, queue, food, score, state, tickAcc, particles, deathFlash;
|
||||||
|
let best = Number(localStorage.getItem('neon-snake-best') || 0);
|
||||||
|
let muted = localStorage.getItem('neon-snake-muted') === '1';
|
||||||
|
let actx = null;
|
||||||
|
|
||||||
|
/* ---------- 音效 ---------- */
|
||||||
|
function unlockAudio(){
|
||||||
|
if (actx) { if (actx.state === 'suspended') actx.resume(); return; }
|
||||||
|
const AC = window.AudioContext || window.webkitAudioContext;
|
||||||
|
if (AC) actx = new AC();
|
||||||
|
}
|
||||||
|
function beep(freq, dur = 0.08, type = 'square', vol = 0.05){
|
||||||
|
if (!actx || muted) return;
|
||||||
|
const t = actx.currentTime;
|
||||||
|
const osc = actx.createOscillator();
|
||||||
|
const gain = actx.createGain();
|
||||||
|
osc.type = type;
|
||||||
|
osc.frequency.setValueAtTime(freq, t);
|
||||||
|
gain.gain.setValueAtTime(vol, t);
|
||||||
|
gain.gain.exponentialRampToValueAtTime(0.0001, t + dur);
|
||||||
|
osc.connect(gain).connect(actx.destination);
|
||||||
|
osc.start(t);
|
||||||
|
osc.stop(t + dur);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------- 游戏流程 ---------- */
|
||||||
|
function reset(){
|
||||||
|
snake = [{x:12,y:12}, {x:11,y:12}, {x:10,y:12}];
|
||||||
|
dir = {x:1, y:0};
|
||||||
|
queue = [];
|
||||||
|
particles = [];
|
||||||
|
deathFlash = 0;
|
||||||
|
score = 0;
|
||||||
|
tickAcc = 0;
|
||||||
|
food = null;
|
||||||
|
spawnFood();
|
||||||
|
state = 'ready';
|
||||||
|
updateHud();
|
||||||
|
}
|
||||||
|
|
||||||
|
function start(){
|
||||||
|
reset();
|
||||||
|
state = 'playing';
|
||||||
|
hideOverlay();
|
||||||
|
beep(660, .07, 'triangle', .04);
|
||||||
|
}
|
||||||
|
|
||||||
|
function tickMs(){
|
||||||
|
return Math.max(MIN_TICK, BASE_TICK - Math.floor(score / 30) * 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
function emptyCells(){
|
||||||
|
const occupied = new Set(snake.map(s => s.x + ',' + s.y));
|
||||||
|
const list = [];
|
||||||
|
for (let y = 0; y < ROWS; y++)
|
||||||
|
for (let x = 0; x < COLS; x++)
|
||||||
|
if (!occupied.has(x + ',' + y)) list.push({x, y});
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
function spawnFood(){
|
||||||
|
const cells = emptyCells();
|
||||||
|
if (!cells.length){ food = null; return; }
|
||||||
|
const c = cells[(Math.random() * cells.length) | 0];
|
||||||
|
food = {x:c.x, y:c.y, type: Math.random() < 0.18 ? 'gold' : 'normal', born: performance.now()};
|
||||||
|
}
|
||||||
|
|
||||||
|
function pushDir(nd){
|
||||||
|
const last = queue.length ? queue[queue.length - 1] : dir;
|
||||||
|
if (nd.x === last.x && nd.y === last.y) return; // 同方向
|
||||||
|
if (nd.x === -last.x && nd.y === -last.y) return; // 180° 掉头
|
||||||
|
if (queue.length >= 2) return;
|
||||||
|
queue.push(nd);
|
||||||
|
}
|
||||||
|
|
||||||
|
function step(){
|
||||||
|
if (queue.length) dir = queue.shift();
|
||||||
|
|
||||||
|
const head = {x: snake[0].x + dir.x, y: snake[0].y + dir.y};
|
||||||
|
|
||||||
|
// 撞墙
|
||||||
|
if (head.x < 0 || head.y < 0 || head.x >= COLS || head.y >= ROWS){ die(); return; }
|
||||||
|
|
||||||
|
const willGrow = !!food && head.x === food.x && head.y === food.y;
|
||||||
|
// 不吃食物时尾巴会挪走,所以尾巴那格不算障碍
|
||||||
|
const body = willGrow ? snake : snake.slice(0, snake.length - 1);
|
||||||
|
if (body.some(s => s.x === head.x && s.y === head.y)){ die(); return; }
|
||||||
|
|
||||||
|
snake.unshift(head);
|
||||||
|
|
||||||
|
if (willGrow){
|
||||||
|
const gold = food.type === 'gold';
|
||||||
|
score += gold ? 30 : 10;
|
||||||
|
if (gold) snake.push({...snake[snake.length - 1]}); // 金块多长一节
|
||||||
|
burst(head.x, head.y, gold);
|
||||||
|
beep(gold ? 1046 : 620, .09, 'triangle', .06);
|
||||||
|
if (gold) setTimeout(() => beep(1568, .1, 'triangle', .05), 80);
|
||||||
|
spawnFood();
|
||||||
|
updateHud();
|
||||||
|
if (!food){ win(); return; }
|
||||||
|
} else {
|
||||||
|
snake.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function die(){
|
||||||
|
state = 'over';
|
||||||
|
deathFlash = 1;
|
||||||
|
beep(240, .22, 'sawtooth', .05);
|
||||||
|
setTimeout(() => beep(150, .34, 'sawtooth', .05), 110);
|
||||||
|
if (score > best){ best = score; localStorage.setItem('neon-snake-best', String(best)); }
|
||||||
|
updateHud();
|
||||||
|
showOverlay('游戏结束', `本局得分 <b>${score}</b> · 最高分 <b>${best}</b>`, '再来一局');
|
||||||
|
}
|
||||||
|
|
||||||
|
function win(){
|
||||||
|
state = 'over';
|
||||||
|
if (score > best){ best = score; localStorage.setItem('neon-snake-best', String(best)); }
|
||||||
|
updateHud();
|
||||||
|
showOverlay('满屏通关!', `你填满了整个棋盘,得分 <b>${score}</b>`, '再来一局');
|
||||||
|
}
|
||||||
|
|
||||||
|
function togglePause(){
|
||||||
|
if (state === 'playing'){
|
||||||
|
state = 'paused';
|
||||||
|
showOverlay('已暂停', '按 P 或点击按钮继续', '继续');
|
||||||
|
} else if (state === 'paused'){
|
||||||
|
state = 'playing';
|
||||||
|
hideOverlay();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------- 界面 ---------- */
|
||||||
|
function updateHud(){
|
||||||
|
scoreEl.textContent = score;
|
||||||
|
bestEl.textContent = best;
|
||||||
|
}
|
||||||
|
function showOverlay(title, text, btn){
|
||||||
|
ovTitle.textContent = title;
|
||||||
|
ovText.innerHTML = text;
|
||||||
|
playBtn.textContent = btn;
|
||||||
|
overlay.classList.remove('hidden');
|
||||||
|
}
|
||||||
|
function hideOverlay(){ overlay.classList.add('hidden'); }
|
||||||
|
|
||||||
|
function primary(){
|
||||||
|
unlockAudio();
|
||||||
|
if (state === 'ready' || state === 'over') start();
|
||||||
|
else togglePause();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------- 特效 ---------- */
|
||||||
|
function burst(gx, gy, gold){
|
||||||
|
const cx = gx * CELL + CELL / 2, cy = gy * CELL + CELL / 2;
|
||||||
|
const n = gold ? 20 : 12;
|
||||||
|
for (let i = 0; i < n; i++){
|
||||||
|
const a = Math.random() * Math.PI * 2;
|
||||||
|
const sp = 60 + Math.random() * 190;
|
||||||
|
particles.push({
|
||||||
|
x: cx, y: cy,
|
||||||
|
vx: Math.cos(a) * sp, vy: Math.sin(a) * sp,
|
||||||
|
life: 380 + Math.random() * 320,
|
||||||
|
max: 700,
|
||||||
|
r: 1.6 + Math.random() * 2.6,
|
||||||
|
color: gold ? '#ffd54a' : '#ff8a5c'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------- 更新 ---------- */
|
||||||
|
function update(dt, now){
|
||||||
|
for (let i = particles.length - 1; i >= 0; i--){
|
||||||
|
const p = particles[i];
|
||||||
|
p.life -= dt;
|
||||||
|
if (p.life <= 0){ particles.splice(i, 1); continue; }
|
||||||
|
p.x += p.vx * dt / 1000;
|
||||||
|
p.y += p.vy * dt / 1000;
|
||||||
|
p.vx *= 0.985;
|
||||||
|
p.vy *= 0.985;
|
||||||
|
}
|
||||||
|
if (deathFlash > 0) deathFlash = Math.max(0, deathFlash - dt / 400);
|
||||||
|
if (state !== 'playing') return;
|
||||||
|
|
||||||
|
if (food && food.type === 'gold' && now - food.born > GOLD_LIFE) food.type = 'normal';
|
||||||
|
|
||||||
|
tickAcc += dt;
|
||||||
|
let guard = 0;
|
||||||
|
while (state === 'playing' && tickAcc >= tickMs() && guard++ < 6){
|
||||||
|
tickAcc -= tickMs();
|
||||||
|
step();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------- 绘制 ---------- */
|
||||||
|
function drawFood(now){
|
||||||
|
if (!food) return;
|
||||||
|
const cx = food.x * CELL + CELL / 2;
|
||||||
|
const cy = food.y * CELL + CELL / 2;
|
||||||
|
const gold = food.type === 'gold';
|
||||||
|
const pulse = 1 + 0.12 * Math.sin(now / 200);
|
||||||
|
const r = CELL * (gold ? 0.33 : 0.27) * pulse;
|
||||||
|
|
||||||
|
ctx.save();
|
||||||
|
ctx.shadowColor = gold ? '#ffd54a' : '#ff7a59';
|
||||||
|
ctx.shadowBlur = 22;
|
||||||
|
ctx.fillStyle = gold ? '#ffd54a' : '#ff7a59';
|
||||||
|
ctx.beginPath(); ctx.arc(cx, cy, r, 0, Math.PI * 2); ctx.fill();
|
||||||
|
ctx.restore();
|
||||||
|
|
||||||
|
ctx.fillStyle = 'rgba(255,255,255,.85)';
|
||||||
|
ctx.beginPath(); ctx.arc(cx - r * 0.28, cy - r * 0.28, r * 0.3, 0, Math.PI * 2); ctx.fill();
|
||||||
|
|
||||||
|
if (gold){
|
||||||
|
const left = Math.max(0, 1 - (now - food.born) / GOLD_LIFE);
|
||||||
|
ctx.strokeStyle = 'rgba(255,213,74,.8)';
|
||||||
|
ctx.lineWidth = 3;
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.arc(cx, cy, CELL * 0.46, -Math.PI / 2, -Math.PI / 2 + Math.PI * 2 * left);
|
||||||
|
ctx.stroke();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function drawSnake(){
|
||||||
|
const n = snake.length;
|
||||||
|
if (!n) return;
|
||||||
|
const pts = snake.map(s => ({x: s.x * CELL + CELL / 2, y: s.y * CELL + CELL / 2}));
|
||||||
|
const head = pts[0], tail = pts[n - 1];
|
||||||
|
|
||||||
|
ctx.save();
|
||||||
|
ctx.lineCap = 'round';
|
||||||
|
ctx.lineJoin = 'round';
|
||||||
|
|
||||||
|
if (n === 1){
|
||||||
|
ctx.shadowColor = '#39ffd0'; ctx.shadowBlur = 24;
|
||||||
|
ctx.fillStyle = '#8affd9';
|
||||||
|
ctx.beginPath(); ctx.arc(head.x, head.y, CELL * 0.4, 0, Math.PI * 2); ctx.fill();
|
||||||
|
ctx.restore();
|
||||||
|
drawEyes(head);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.moveTo(head.x, head.y);
|
||||||
|
for (let i = 1; i < n; i++) ctx.lineTo(pts[i].x, pts[i].y);
|
||||||
|
|
||||||
|
// 外发光
|
||||||
|
ctx.shadowColor = '#39ffd0';
|
||||||
|
ctx.shadowBlur = 26;
|
||||||
|
ctx.strokeStyle = 'rgba(57,255,208,.22)';
|
||||||
|
ctx.lineWidth = CELL * 1.0;
|
||||||
|
ctx.stroke();
|
||||||
|
|
||||||
|
// 主体渐变
|
||||||
|
const g = ctx.createLinearGradient(head.x, head.y, tail.x, tail.y);
|
||||||
|
g.addColorStop(0, '#b6ffe9');
|
||||||
|
g.addColorStop(0.35, '#39ffd0');
|
||||||
|
g.addColorStop(1, '#6a5cff');
|
||||||
|
ctx.shadowBlur = 14;
|
||||||
|
ctx.strokeStyle = g;
|
||||||
|
ctx.lineWidth = CELL * 0.74;
|
||||||
|
ctx.stroke();
|
||||||
|
|
||||||
|
// 高光核心
|
||||||
|
ctx.shadowBlur = 0;
|
||||||
|
ctx.strokeStyle = 'rgba(255,255,255,.32)';
|
||||||
|
ctx.lineWidth = CELL * 0.2;
|
||||||
|
ctx.stroke();
|
||||||
|
ctx.restore();
|
||||||
|
|
||||||
|
// 头部
|
||||||
|
ctx.save();
|
||||||
|
ctx.shadowColor = '#39ffd0';
|
||||||
|
ctx.shadowBlur = 18;
|
||||||
|
ctx.fillStyle = '#eafff8';
|
||||||
|
ctx.beginPath(); ctx.arc(head.x, head.y, CELL * 0.42, 0, Math.PI * 2); ctx.fill();
|
||||||
|
ctx.restore();
|
||||||
|
drawEyes(head);
|
||||||
|
}
|
||||||
|
|
||||||
|
function drawEyes(head){
|
||||||
|
const perpX = dir.y !== 0 ? 1 : 0;
|
||||||
|
const perpY = dir.x !== 0 ? 1 : 0;
|
||||||
|
ctx.fillStyle = '#06121a';
|
||||||
|
for (const s of [-1, 1]){
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.arc(
|
||||||
|
head.x + dir.x * CELL * 0.14 + perpX * s * CELL * 0.16,
|
||||||
|
head.y + dir.y * CELL * 0.14 + perpY * s * CELL * 0.16,
|
||||||
|
CELL * 0.075, 0, Math.PI * 2
|
||||||
|
);
|
||||||
|
ctx.fill();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function drawParticles(){
|
||||||
|
for (const p of particles){
|
||||||
|
ctx.globalAlpha = Math.max(0, Math.min(1, p.life / p.max));
|
||||||
|
ctx.fillStyle = p.color;
|
||||||
|
ctx.beginPath(); ctx.arc(p.x, p.y, p.r, 0, Math.PI * 2); ctx.fill();
|
||||||
|
}
|
||||||
|
ctx.globalAlpha = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
function draw(now){
|
||||||
|
const bg = ctx.createRadialGradient(W / 2, H * 0.15, 0, W / 2, H * 0.15, H);
|
||||||
|
bg.addColorStop(0, '#0d1a30');
|
||||||
|
bg.addColorStop(1, '#050810');
|
||||||
|
ctx.fillStyle = bg;
|
||||||
|
ctx.fillRect(0, 0, W, H);
|
||||||
|
|
||||||
|
ctx.strokeStyle = 'rgba(120,190,255,.06)';
|
||||||
|
ctx.lineWidth = 1;
|
||||||
|
ctx.beginPath();
|
||||||
|
for (let i = 1; i < COLS; i++){ const x = i * CELL + .5; ctx.moveTo(x, 0); ctx.lineTo(x, H); }
|
||||||
|
for (let j = 1; j < ROWS; j++){ const y = j * CELL + .5; ctx.moveTo(0, y); ctx.lineTo(W, y); }
|
||||||
|
ctx.stroke();
|
||||||
|
|
||||||
|
ctx.strokeStyle = 'rgba(57,255,208,.3)';
|
||||||
|
ctx.lineWidth = 2;
|
||||||
|
ctx.strokeRect(1, 1, W - 2, H - 2);
|
||||||
|
|
||||||
|
drawFood(now);
|
||||||
|
drawSnake();
|
||||||
|
drawParticles();
|
||||||
|
|
||||||
|
if (deathFlash > 0){
|
||||||
|
ctx.fillStyle = `rgba(255,90,120,${deathFlash * 0.35})`;
|
||||||
|
ctx.fillRect(0, 0, W, H);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------- 主循环 ---------- */
|
||||||
|
let last = performance.now();
|
||||||
|
function loop(now){
|
||||||
|
const dt = Math.min(now - last, 120);
|
||||||
|
last = now;
|
||||||
|
update(dt, now);
|
||||||
|
draw(now);
|
||||||
|
requestAnimationFrame(loop);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------- 输入 ---------- */
|
||||||
|
const DIRS = {
|
||||||
|
arrowup:{x:0,y:-1}, arrowdown:{x:0,y:1}, arrowleft:{x:-1,y:0}, arrowright:{x:1,y:0},
|
||||||
|
w:{x:0,y:-1}, s:{x:0,y:1}, a:{x:-1,y:0}, d:{x:1,y:0}
|
||||||
|
};
|
||||||
|
|
||||||
|
function input(nd){
|
||||||
|
unlockAudio();
|
||||||
|
if (state === 'ready') start();
|
||||||
|
if (state === 'playing') pushDir(nd);
|
||||||
|
}
|
||||||
|
|
||||||
|
window.addEventListener('keydown', e => {
|
||||||
|
const k = (e.key || '').toLowerCase();
|
||||||
|
if (DIRS[k]){
|
||||||
|
e.preventDefault();
|
||||||
|
input(DIRS[k]);
|
||||||
|
} else if (k === ' ' || k === 'enter'){
|
||||||
|
e.preventDefault();
|
||||||
|
primary();
|
||||||
|
} else if (k === 'p' || k === 'escape'){
|
||||||
|
e.preventDefault();
|
||||||
|
togglePause();
|
||||||
|
}
|
||||||
|
}, {passive: false});
|
||||||
|
|
||||||
|
document.querySelectorAll('.padBtn').forEach(btn => {
|
||||||
|
const nd = {up:{x:0,y:-1}, down:{x:0,y:1}, left:{x:-1,y:0}, right:{x:1,y:0}}[btn.dataset.dir];
|
||||||
|
btn.addEventListener('pointerdown', e => { e.preventDefault(); input(nd); });
|
||||||
|
});
|
||||||
|
|
||||||
|
playBtn.addEventListener('click', primary);
|
||||||
|
muteBtn.addEventListener('click', () => {
|
||||||
|
muted = !muted;
|
||||||
|
localStorage.setItem('neon-snake-muted', muted ? '1' : '0');
|
||||||
|
muteBtn.textContent = muted ? '🔇' : '🔊';
|
||||||
|
if (!muted){ unlockAudio(); beep(880, .06, 'triangle', .05); }
|
||||||
|
});
|
||||||
|
|
||||||
|
// 触屏滑动 + 轻点
|
||||||
|
let touch = null;
|
||||||
|
canvas.addEventListener('pointerdown', e => {
|
||||||
|
unlockAudio();
|
||||||
|
touch = {x: e.clientX, y: e.clientY, t: performance.now()};
|
||||||
|
});
|
||||||
|
canvas.addEventListener('pointerup', e => {
|
||||||
|
if (!touch) return;
|
||||||
|
const dx = e.clientX - touch.x, dy = e.clientY - touch.y;
|
||||||
|
const dist = Math.hypot(dx, dy);
|
||||||
|
touch = null;
|
||||||
|
if (dist < 24){ primary(); return; }
|
||||||
|
if (Math.abs(dx) > Math.abs(dy)) input({x: dx > 0 ? 1 : -1, y: 0});
|
||||||
|
else input({x: 0, y: dy > 0 ? 1 : -1});
|
||||||
|
});
|
||||||
|
canvas.addEventListener('touchmove', e => e.preventDefault(), {passive: false});
|
||||||
|
|
||||||
|
document.addEventListener('visibilitychange', () => {
|
||||||
|
if (document.hidden && state === 'playing') togglePause();
|
||||||
|
});
|
||||||
|
|
||||||
|
/* ---------- 启动 ---------- */
|
||||||
|
muteBtn.textContent = muted ? '🔇' : '🔊';
|
||||||
|
reset();
|
||||||
|
showOverlay(
|
||||||
|
'霓虹贪吃蛇',
|
||||||
|
'方向键 / WASD 控制方向<br>橙色方块 +10 分,金色方块 +30 分但会消失<br>撞墙或咬到自己就结束了',
|
||||||
|
'开始游戏'
|
||||||
|
);
|
||||||
|
requestAnimationFrame(loop);
|
||||||
|
})();
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user