592 lines
21 KiB
HTML
592 lines
21 KiB
HTML
<!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="#04060d">
|
||
<title>Neon Arcade · 我的游戏合集</title>
|
||
<style>
|
||
:root{
|
||
--bg:#04060d;
|
||
--text:#e8f2ff;
|
||
--muted:#8ea6c0;
|
||
--line:rgba(255,255,255,.09);
|
||
--neon:#39ffd0;
|
||
--violet:#7c5cff;
|
||
}
|
||
*{box-sizing:border-box}
|
||
html,body{height:100%}
|
||
body{
|
||
margin:0;
|
||
min-height:100dvh;
|
||
background:
|
||
radial-gradient(circle at 18% -5%, rgba(57,255,208,.14) 0%, transparent 45%),
|
||
radial-gradient(circle at 85% 5%, rgba(124,92,255,.18) 0%, transparent 45%),
|
||
var(--bg);
|
||
color:var(--text);
|
||
font-family:ui-sans-serif,system-ui,-apple-system,"PingFang SC","Microsoft YaHei",sans-serif;
|
||
-webkit-font-smoothing:antialiased;
|
||
overflow-x:hidden;
|
||
}
|
||
/* 背景细网格 */
|
||
.bg-grid{
|
||
position:fixed;inset:-2px;pointer-events:none;z-index:0;
|
||
background-image:
|
||
linear-gradient(rgba(120,190,255,.05) 1px, transparent 1px),
|
||
linear-gradient(90deg, rgba(120,190,255,.05) 1px, transparent 1px);
|
||
background-size:44px 44px;
|
||
mask-image:radial-gradient(circle at 50% 0%, #000 0%, transparent 75%);
|
||
-webkit-mask-image:radial-gradient(circle at 50% 0%, #000 0%, transparent 75%);
|
||
}
|
||
.wrap{
|
||
position:relative;z-index:1;
|
||
max-width:1120px;margin:0 auto;padding:46px 20px 60px;
|
||
}
|
||
|
||
/* ---------- 头部 ---------- */
|
||
header{text-align:center;margin-bottom:40px}
|
||
.kicker{
|
||
display:inline-block;font-size:11px;letter-spacing:.34em;color:var(--neon);
|
||
border:1px solid rgba(57,255,208,.3);border-radius:999px;
|
||
padding:6px 14px 6px 16px;margin-bottom:18px;
|
||
background:rgba(57,255,208,.06);
|
||
}
|
||
h1{
|
||
margin:0 0 12px;
|
||
font-size:clamp(34px,7vw,64px);
|
||
font-weight:800;letter-spacing:.06em;line-height:1;
|
||
background:linear-gradient(100deg,#8affd9 0%,var(--neon) 32%,#4ad9ff 58%,var(--violet) 100%);
|
||
-webkit-background-clip:text;background-clip:text;color:transparent;
|
||
filter:drop-shadow(0 0 32px rgba(57,255,208,.28));
|
||
}
|
||
header p{margin:0;color:var(--muted);font-size:clamp(13px,2.4vw,15px);line-height:1.8}
|
||
.meta{
|
||
display:flex;justify-content:center;gap:10px;flex-wrap:wrap;margin-top:22px;
|
||
}
|
||
.meta span{
|
||
font-size:12px;color:var(--muted);
|
||
border:1px solid var(--line);border-radius:999px;padding:6px 13px;
|
||
background:rgba(255,255,255,.03);
|
||
}
|
||
.meta b{color:var(--neon);font-weight:600}
|
||
|
||
/* ---------- 卡片墙 ---------- */
|
||
.grid{
|
||
display:grid;gap:22px;
|
||
grid-template-columns:repeat(auto-fill,minmax(330px,1fr));
|
||
}
|
||
.card{
|
||
--accent:var(--neon);
|
||
position:relative;
|
||
display:flex;flex-direction:column;
|
||
border-radius:20px;overflow:hidden;
|
||
background:linear-gradient(180deg, rgba(255,255,255,.055), rgba(255,255,255,.015));
|
||
border:1px solid var(--line);
|
||
backdrop-filter:blur(8px);
|
||
transition:transform .28s cubic-bezier(.2,.8,.3,1), border-color .28s, box-shadow .28s;
|
||
}
|
||
.card:hover, .card:focus-within{
|
||
transform:translateY(-5px);
|
||
border-color:var(--accent);
|
||
box-shadow:0 24px 60px -20px var(--accent), 0 0 0 1px rgba(255,255,255,.02) inset;
|
||
}
|
||
.preview{
|
||
position:relative;aspect-ratio:360/220;overflow:hidden;
|
||
border-bottom:1px solid var(--line);
|
||
background:#04070f;
|
||
}
|
||
.preview canvas{display:block;width:100%;height:100%}
|
||
.preview::after{
|
||
content:"";position:absolute;inset:0;pointer-events:none;
|
||
background:radial-gradient(circle at 50% 50%, transparent 45%, rgba(0,0,0,.55) 100%);
|
||
}
|
||
.badge{
|
||
position:absolute;top:10px;right:10px;z-index:2;
|
||
font-size:10px;letter-spacing:.1em;
|
||
padding:4px 9px;border-radius:999px;
|
||
color:var(--accent);
|
||
border:1px solid var(--accent);
|
||
background:rgba(4,6,13,.75);
|
||
backdrop-filter:blur(4px);
|
||
}
|
||
.tagline{
|
||
position:absolute;left:12px;bottom:10px;z-index:2;
|
||
font-size:11px;letter-spacing:.18em;color:rgba(232,242,255,.55);
|
||
text-transform:uppercase;
|
||
}
|
||
.body{padding:18px 20px 20px;display:flex;flex-direction:column;gap:12px;flex:1}
|
||
.body h2{
|
||
margin:0;font-size:20px;font-weight:700;letter-spacing:.02em;
|
||
display:flex;align-items:baseline;gap:9px;flex-wrap:wrap;
|
||
}
|
||
.body h2 small{font-size:11px;font-weight:500;letter-spacing:.16em;color:var(--muted)}
|
||
.body p{margin:0;font-size:13.5px;line-height:1.85;color:#b9cbe0}
|
||
.tags{display:flex;flex-wrap:wrap;gap:7px}
|
||
.tags span{
|
||
font-size:11px;padding:4px 10px;border-radius:7px;
|
||
color:var(--accent);
|
||
background:color-mix(in srgb, var(--accent) 11%, transparent);
|
||
border:1px solid color-mix(in srgb, var(--accent) 30%, transparent);
|
||
}
|
||
.keys{
|
||
font-size:11.5px;color:var(--muted);
|
||
border-top:1px dashed var(--line);padding-top:11px;line-height:1.7;
|
||
}
|
||
.actions{display:flex;gap:10px;margin-top:auto;padding-top:4px}
|
||
.btn{
|
||
flex:1;text-align:center;text-decoration:none;
|
||
font-size:13px;font-weight:600;letter-spacing:.03em;
|
||
padding:11px 12px;border-radius:11px;cursor:pointer;
|
||
border:1px solid var(--line);
|
||
color:var(--text);background:rgba(255,255,255,.04);
|
||
transition:background .2s, border-color .2s, color .2s, transform .12s;
|
||
}
|
||
.btn:hover{background:rgba(255,255,255,.09);border-color:rgba(255,255,255,.22)}
|
||
.btn:active{transform:scale(.98)}
|
||
.btn.primary{
|
||
color:#04120e;border-color:transparent;font-weight:700;
|
||
background:linear-gradient(135deg,var(--accent),#4ad9ff);
|
||
box-shadow:0 10px 28px -10px var(--accent);
|
||
}
|
||
.btn.primary:hover{filter:brightness(1.08)}
|
||
|
||
/* 占位卡片 */
|
||
.card.soon{
|
||
align-items:center;justify-content:center;text-align:center;
|
||
border-style:dashed;border-color:rgba(255,255,255,.14);
|
||
background:rgba(255,255,255,.015);
|
||
padding:40px 24px;min-height:260px;gap:12px;
|
||
color:var(--muted);
|
||
}
|
||
.card.soon:hover{transform:none;box-shadow:none;border-color:rgba(255,255,255,.2)}
|
||
.card.soon .plus{
|
||
font-size:34px;line-height:1;color:rgba(255,255,255,.25);font-weight:300;
|
||
}
|
||
.card.soon h3{margin:0;font-size:15px;color:#cfdcec;font-weight:600;letter-spacing:.06em}
|
||
.card.soon p{margin:0;font-size:12.5px;line-height:1.9;max-width:26ch}
|
||
|
||
/* ---------- 底部 ---------- */
|
||
footer{
|
||
margin-top:46px;padding-top:26px;
|
||
border-top:1px solid var(--line);
|
||
font-size:12.5px;color:var(--muted);line-height:2;text-align:center;
|
||
}
|
||
footer code{
|
||
font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:12px;
|
||
background:rgba(255,255,255,.06);border:1px solid var(--line);
|
||
border-radius:6px;padding:2px 6px;color:#cfe3f7;
|
||
}
|
||
footer a{color:var(--neon);text-decoration:none;border-bottom:1px solid rgba(57,255,208,.35)}
|
||
footer a:hover{border-bottom-color:var(--neon)}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div class="bg-grid"></div>
|
||
|
||
<div class="wrap">
|
||
<header>
|
||
<span class="kicker">NEON ARCADE</span>
|
||
<h1>我的游戏合集</h1>
|
||
<p>全部是单文件 HTML5 小游戏 —— 零依赖、零外部资源,双击就能玩。<br>键盘和手机触屏都支持,音效用 WebAudio 现场合成。</p>
|
||
<div class="meta">
|
||
<span><b id="mCount">0</b> 个游戏</span>
|
||
<span>单文件 · <b>0</b> 依赖</span>
|
||
<span>Canvas 2D 渲染</span>
|
||
<span>手机 / 桌面通用</span>
|
||
</div>
|
||
</header>
|
||
|
||
<main class="grid" id="games"></main>
|
||
|
||
<footer>
|
||
每个游戏都有独立的 Git 仓库,可以 <code>git clone</code> 后离线玩,也可以直接点卡片上的「下载 index.html」。<br>
|
||
想加新游戏?编辑本页的 <code>GAMES</code> 数组即可,卡片会自动生成。<br>
|
||
部署到 Gitea Pages 后,这个页面就是你的在线游戏厅。
|
||
</footer>
|
||
</div>
|
||
|
||
<script>
|
||
(() => {
|
||
'use strict';
|
||
|
||
/* ==================================================================
|
||
游戏清单:新增游戏只要往这里加一项,卡片会自动生成。
|
||
demo 可选 'snake' | 'tetris',对应下面的实时预览动画。
|
||
================================================================== */
|
||
const GAMES = [
|
||
{
|
||
id: 'neon-snake',
|
||
title: '霓虹贪吃蛇',
|
||
en: 'NEON SNAKE',
|
||
tagline: 'ARCADE / 街机',
|
||
accent: '#39ffd0',
|
||
version: 'v1.0.0',
|
||
desc: '经典的贪吃蛇,霓虹发光渲染。橙块 +10 分;金色方块 +30 分,但 8 秒不吃就会消失。吃得越多速度越快,撞墙或咬到自己就结束。',
|
||
tags: ['Canvas 2D', '方向缓冲队列', '触屏滑动', 'WebAudio 音效'],
|
||
keys: '方向键 / WASD 移动 · 空格 开始与暂停 · P 暂停',
|
||
demo: 'snake',
|
||
repo: 'https://gitea.compute-thinking.cn/class04f27/neon-snake'
|
||
},
|
||
{
|
||
id: 'neon-tetris',
|
||
title: '霓虹俄罗斯方块',
|
||
en: 'NEON TETRIS',
|
||
tagline: 'PUZZLE / 益智',
|
||
accent: '#c86bff',
|
||
version: 'v1.0.0',
|
||
desc: '按现代 Guideline / SRS 规则实现:完整踢墙表、7-bag 随机器、Hold 暂存、幽灵块落点预览、500ms 锁定延迟,还有 Tetris 与连消加分。',
|
||
tags: ['SRS 旋转', '7-bag 随机', 'Hold 暂存', '连消计分'],
|
||
keys: '← → 移动 · ↓ 软降 · 空格 硬降 · ↑/X 顺时针 · Z 逆时针 · C 暂存',
|
||
demo: 'tetris',
|
||
repo: 'https://gitea.compute-thinking.cn/class04f27/neon-tetris'
|
||
}
|
||
];
|
||
|
||
/* ================== 渲染卡片 ================== */
|
||
const grid = document.getElementById('games');
|
||
|
||
function buildCard(g){
|
||
const el = document.createElement('article');
|
||
el.className = 'card';
|
||
el.style.setProperty('--accent', g.accent);
|
||
el.innerHTML = `
|
||
<div class="preview">
|
||
<canvas class="demo" data-demo="${g.demo}" width="360" height="220"></canvas>
|
||
<span class="badge">${g.version}</span>
|
||
<span class="tagline">${g.tagline}</span>
|
||
</div>
|
||
<div class="body">
|
||
<h2>${g.title}<small>${g.en}</small></h2>
|
||
<p>${g.desc}</p>
|
||
<div class="tags">${g.tags.map(t => `<span>${t}</span>`).join('')}</div>
|
||
<p class="keys">🎮 ${g.keys}</p>
|
||
<div class="actions">
|
||
<a class="btn primary" href="${g.repo}" target="_blank" rel="noopener">打开仓库</a>
|
||
<a class="btn" href="${g.repo}/raw/branch/main/index.html" target="_blank" rel="noopener">下载 index.html</a>
|
||
</div>
|
||
</div>`;
|
||
return el;
|
||
}
|
||
|
||
function buildSoonCard(){
|
||
const el = document.createElement('article');
|
||
el.className = 'card soon';
|
||
el.innerHTML = `
|
||
<div class="plus">+</div>
|
||
<h3>更多游戏开发中</h3>
|
||
<p>打砖块、2048、飞机大战、双人对战……<br>往 GAMES 数组里加一项就能上架。</p>`;
|
||
return el;
|
||
}
|
||
|
||
GAMES.forEach(g => grid.appendChild(buildCard(g)));
|
||
grid.appendChild(buildSoonCard());
|
||
document.getElementById('mCount').textContent = GAMES.length;
|
||
|
||
/* ==================================================================
|
||
实时预览动画:两个迷你引擎,只在卡片可见时运行
|
||
================================================================== */
|
||
const DPR = Math.min(window.devicePixelRatio || 1, 2);
|
||
const reduced = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
||
|
||
function setupCanvas(cv, W, H){
|
||
cv.width = W * DPR;
|
||
cv.height = H * DPR;
|
||
const c = cv.getContext('2d');
|
||
c.setTransform(DPR, 0, 0, DPR, 0, 0);
|
||
return c;
|
||
}
|
||
function rr(c, x, y, w, h, r){
|
||
r = Math.min(r, w / 2, h / 2);
|
||
c.beginPath();
|
||
c.moveTo(x + r, y);
|
||
c.arcTo(x + w, y, x + w, y + h, r);
|
||
c.arcTo(x + w, y + h, x, y + h, r);
|
||
c.arcTo(x, y + h, x, y, r);
|
||
c.arcTo(x, y, x + w, y, r);
|
||
c.closePath();
|
||
}
|
||
function hx(h){
|
||
const n = parseInt(h.slice(1), 16);
|
||
return [n >> 16 & 255, n >> 8 & 255, n & 255];
|
||
}
|
||
function mix(a, b, t){
|
||
const A = hx(a), B = hx(b);
|
||
return `rgb(${Math.round(A[0]+(B[0]-A[0])*t)},${Math.round(A[1]+(B[1]-A[1])*t)},${Math.round(A[2]+(B[2]-A[2])*t)})`;
|
||
}
|
||
function block(c, x, y, s, color, glow){
|
||
const pad = Math.max(1, s * .1);
|
||
c.save();
|
||
if (glow){ c.shadowColor = color; c.shadowBlur = s * .45; }
|
||
const g = c.createLinearGradient(x, y, x + s, y + s);
|
||
g.addColorStop(0, mix(color, '#ffffff', .45));
|
||
g.addColorStop(.55, color);
|
||
g.addColorStop(1, mix(color, '#000000', .3));
|
||
c.fillStyle = g;
|
||
rr(c, x + pad, y + pad, s - pad * 2, s - pad * 2, s * .22);
|
||
c.fill();
|
||
c.restore();
|
||
}
|
||
function bg(c, W, H){
|
||
const g = c.createLinearGradient(0, 0, 0, H);
|
||
g.addColorStop(0, '#0b1327');
|
||
g.addColorStop(1, '#04070f');
|
||
c.fillStyle = g;
|
||
c.fillRect(0, 0, W, H);
|
||
}
|
||
function gridLines(c, W, H, step, alpha){
|
||
c.strokeStyle = `rgba(120,190,255,${alpha})`;
|
||
c.lineWidth = 1;
|
||
c.beginPath();
|
||
for (let x = step; x < W; x += step){ c.moveTo(x + .5, 0); c.lineTo(x + .5, H); }
|
||
for (let y = step; y < H; y += step){ c.moveTo(0, y + .5); c.lineTo(W, y + .5); }
|
||
c.stroke();
|
||
}
|
||
|
||
/* ---------- 预览 1:贪吃蛇(自动寻路) ---------- */
|
||
function snakeDemo(cv){
|
||
const W = 360, H = 220, COLS = 18, ROWS = 11, CELL = 20, ACC = 118;
|
||
const c = setupCanvas(cv, W, H);
|
||
let snake, dir, food, acc, pause, raf, last;
|
||
|
||
function randFood(){
|
||
const occ = new Set(snake.map(s => s.x + ',' + s.y));
|
||
const free = [];
|
||
for (let y = 0; y < ROWS; y++)
|
||
for (let x = 0; x < COLS; x++)
|
||
if (!occ.has(x + ',' + y)) free.push({x, y});
|
||
return free.length ? free[(Math.random() * free.length) | 0] : null;
|
||
}
|
||
function reset(){
|
||
snake = [{x:5,y:5},{x:4,y:5},{x:3,y:5}];
|
||
dir = {x:1, y:0};
|
||
food = randFood();
|
||
acc = 0; pause = 0;
|
||
}
|
||
function space(h, body){
|
||
const blocked = new Set(body.map(s => s.x + ',' + s.y));
|
||
const seen = new Set([h.x + ',' + h.y]);
|
||
const stack = [h];
|
||
let n = 0;
|
||
while (stack.length){
|
||
const p = stack.pop(); n++;
|
||
const nb = [{x:1,y:0},{x:-1,y:0},{x:0,y:1},{x:0,y:-1}];
|
||
for (const d of nb){
|
||
const q = {x:p.x + d.x, y:p.y + d.y}, k = q.x + ',' + q.y;
|
||
if (q.x < 0 || q.y < 0 || q.x >= COLS || q.y >= ROWS) continue;
|
||
if (seen.has(k) || blocked.has(k)) continue;
|
||
seen.add(k); stack.push(q);
|
||
}
|
||
}
|
||
return n;
|
||
}
|
||
function step(){
|
||
if (!food){ reset(); return; }
|
||
const body = snake.slice(0, -1);
|
||
let best = null;
|
||
for (const d of [{x:1,y:0},{x:-1,y:0},{x:0,y:1},{x:0,y:-1}]){
|
||
if (d.x === -dir.x && d.y === -dir.y) continue;
|
||
const h = {x:snake[0].x + d.x, y:snake[0].y + d.y};
|
||
if (h.x < 0 || h.y < 0 || h.x >= COLS || h.y >= ROWS) continue;
|
||
if (body.some(s => s.x === h.x && s.y === h.y)) continue;
|
||
const dist = Math.abs(h.x - food.x) + Math.abs(h.y - food.y);
|
||
const sc = dist * 1.25 - space(h, body) * .55 + Math.random() * .9;
|
||
if (!best || sc < best.sc) best = {d, h, sc};
|
||
}
|
||
if (!best){ pause = 900; return; }
|
||
dir = best.d;
|
||
snake.unshift(best.h);
|
||
if (best.h.x === food.x && best.h.y === food.y) food = randFood();
|
||
else snake.pop();
|
||
}
|
||
function draw(t){
|
||
bg(c, W, H);
|
||
gridLines(c, W, H, CELL, .05);
|
||
if (pause > 0){
|
||
c.fillStyle = 'rgba(232,242,255,.5)';
|
||
c.font = '600 13px ui-sans-serif,system-ui,sans-serif';
|
||
c.textAlign = 'center';
|
||
c.fillText('重新开始…', W / 2, H / 2 + 4);
|
||
}
|
||
if (food){
|
||
const pulse = 1 + .14 * Math.sin(t / 190);
|
||
const px = food.x * CELL + CELL / 2, py = food.y * CELL + CELL / 2;
|
||
c.save();
|
||
c.shadowColor = '#ff9f43'; c.shadowBlur = 20;
|
||
c.fillStyle = '#ff9f43';
|
||
c.beginPath(); c.arc(px, py, CELL * .3 * pulse, 0, Math.PI * 2); c.fill();
|
||
c.restore();
|
||
}
|
||
const pts = snake.map(s => ({x:s.x * CELL + CELL / 2, y:s.y * CELL + CELL / 2}));
|
||
c.save();
|
||
c.lineCap = 'round'; c.lineJoin = 'round';
|
||
c.beginPath();
|
||
c.moveTo(pts[0].x, pts[0].y);
|
||
for (let i = 1; i < pts.length; i++) c.lineTo(pts[i].x, pts[i].y);
|
||
const tail = pts[pts.length - 1];
|
||
const g = c.createLinearGradient(pts[0].x, pts[0].y, tail.x, tail.y);
|
||
g.addColorStop(0, '#b6ffe9'); g.addColorStop(.4, '#39ffd0'); g.addColorStop(1, '#6a5cff');
|
||
c.shadowColor = '#39ffd0'; c.shadowBlur = 18;
|
||
c.strokeStyle = g;
|
||
c.lineWidth = CELL * .72;
|
||
c.stroke();
|
||
c.shadowBlur = 0;
|
||
c.strokeStyle = 'rgba(255,255,255,.3)';
|
||
c.lineWidth = CELL * .2;
|
||
c.stroke();
|
||
c.restore();
|
||
}
|
||
function loop(now){
|
||
if (!last) last = now;
|
||
const dt = Math.min(now - last, 100);
|
||
last = now;
|
||
if (pause > 0){
|
||
pause -= dt;
|
||
if (pause <= 0) reset();
|
||
} else {
|
||
acc += dt;
|
||
while (acc >= ACC){ acc -= ACC; if (pause <= 0) step(); }
|
||
}
|
||
draw(now);
|
||
raf = requestAnimationFrame(loop);
|
||
}
|
||
reset();
|
||
if (reduced){ draw(0); return () => {}; }
|
||
raf = requestAnimationFrame(loop);
|
||
return () => cancelAnimationFrame(raf);
|
||
}
|
||
|
||
/* ---------- 预览 2:俄罗斯方块(自动堆叠) ---------- */
|
||
function tetrisDemo(cv){
|
||
const W = 360, H = 220, COLS = 8, ROWS = 12, CELL = 18;
|
||
const OX = (W - COLS * CELL) / 2, OY = (H - ROWS * CELL) / 2;
|
||
const c = setupCanvas(cv, W, H);
|
||
const PIECES = [
|
||
{m:[[0,0,0,0],[1,1,1,1],[0,0,0,0],[0,0,0,0]], c:'#4ee1ff'},
|
||
{m:[[1,1],[1,1]], c:'#ffd54a'},
|
||
{m:[[0,1,0],[1,1,1],[0,0,0]], c:'#c86bff'},
|
||
{m:[[1,0,0],[1,1,1],[0,0,0]], c:'#5a7bff'},
|
||
{m:[[0,1,1],[1,1,0],[0,0,0]], c:'#39ffa0'},
|
||
{m:[[1,1,0],[0,1,1],[0,0,0]], c:'#ff5f7e'}
|
||
];
|
||
let board, cur, acc, flash, raf, last;
|
||
const FALL = 300, FLASH = 150;
|
||
|
||
const rcw = m => m.map((_, y) => m.map(row => row[y]).reverse());
|
||
function hits(m, px, py){
|
||
for (let y = 0; y < m.length; y++)
|
||
for (let x = 0; x < m[y].length; x++){
|
||
if (!m[y][x]) continue;
|
||
const bx = px + x, by = py + y;
|
||
if (bx < 0 || bx >= COLS || by >= ROWS) return true;
|
||
if (by >= 0 && board[by][bx]) return true;
|
||
}
|
||
return false;
|
||
}
|
||
function reset(){
|
||
board = Array.from({length: ROWS}, () => new Array(COLS).fill(null));
|
||
flash = null; acc = 0; spawn();
|
||
}
|
||
function spawn(){
|
||
const p = PIECES[(Math.random() * PIECES.length) | 0];
|
||
let m = p.m;
|
||
const turns = (Math.random() * 4) | 0;
|
||
for (let i = 0; i < turns; i++) m = rcw(m);
|
||
cur = {m, c:p.c, x: Math.floor((COLS - m[0].length) / 2), y: 0};
|
||
if (hits(m, cur.x, cur.y)) { board = Array.from({length: ROWS}, () => new Array(COLS).fill(null)); }
|
||
}
|
||
function lock(){
|
||
for (let y = 0; y < cur.m.length; y++)
|
||
for (let x = 0; x < cur.m[y].length; x++)
|
||
if (cur.m[y][x] && cur.y + y >= 0) board[cur.y + y][cur.x + x] = cur.c;
|
||
const full = [];
|
||
for (let y = 0; y < ROWS; y++) if (board[y].every(Boolean)) full.push(y);
|
||
if (full.length){ flash = {rows: full, t: 0}; cur = null; }
|
||
else spawn();
|
||
}
|
||
function step(){
|
||
if (flash) return;
|
||
if (Math.random() < .28) { // 偶尔左右挪一下,看起来更自然
|
||
const d = Math.random() < .5 ? -1 : 1;
|
||
if (!hits(cur.m, cur.x + d, cur.y)) cur.x += d;
|
||
}
|
||
if (!hits(cur.m, cur.x, cur.y + 1)) cur.y++;
|
||
else lock();
|
||
}
|
||
function draw(){
|
||
bg(c, W, H);
|
||
gridLines(c, W, H, CELL, .05);
|
||
c.save();
|
||
c.translate(OX, OY);
|
||
c.strokeStyle = 'rgba(57,255,208,.18)';
|
||
c.lineWidth = 1;
|
||
c.strokeRect(.5, .5, COLS * CELL - 1, ROWS * CELL - 1);
|
||
for (let y = 0; y < ROWS; y++)
|
||
for (let x = 0; x < COLS; x++)
|
||
if (board[y][x]) block(c, x * CELL, y * CELL, CELL, board[y][x], true);
|
||
if (cur)
|
||
for (let y = 0; y < cur.m.length; y++)
|
||
for (let x = 0; x < cur.m[y].length; x++)
|
||
if (cur.m[y][x]) block(c, (cur.x + x) * CELL, (cur.y + y) * CELL, CELL, cur.c, true);
|
||
if (flash){
|
||
const p = Math.min(1, flash.t / FLASH);
|
||
c.fillStyle = `rgba(255,255,255,${.85 * (1 - p)})`;
|
||
for (const y of flash.rows) c.fillRect(0, y * CELL, COLS * CELL, CELL);
|
||
}
|
||
c.restore();
|
||
}
|
||
function loop(now){
|
||
if (!last) last = now;
|
||
const dt = Math.min(now - last, 100);
|
||
last = now;
|
||
if (flash){
|
||
flash.t += dt;
|
||
if (flash.t >= FLASH){
|
||
board = board.filter((_, y) => flash.rows.indexOf(y) === -1);
|
||
for (let i = 0; i < flash.rows.length; i++) board.unshift(new Array(COLS).fill(null));
|
||
flash = null;
|
||
spawn();
|
||
}
|
||
} else {
|
||
acc += dt;
|
||
while (acc >= FALL){ acc -= FALL; step(); }
|
||
}
|
||
draw();
|
||
raf = requestAnimationFrame(loop);
|
||
}
|
||
reset();
|
||
for (let i = 0; i < 5; i++){ while (!flash && cur) step(); } // 先堆几块,避免开局空荡
|
||
if (reduced){ draw(); return () => {}; }
|
||
raf = requestAnimationFrame(loop);
|
||
return () => cancelAnimationFrame(raf);
|
||
}
|
||
|
||
const DEMOS = { snake: snakeDemo, tetris: tetrisDemo };
|
||
|
||
/* ---------- 只在可见时播放,省电 ---------- */
|
||
const canvases = [...document.querySelectorAll('canvas.demo')];
|
||
const stops = new Map();
|
||
|
||
if ('IntersectionObserver' in window){
|
||
const io = new IntersectionObserver(entries => {
|
||
for (const e of entries){
|
||
const cv = e.target;
|
||
if (e.isIntersecting){
|
||
if (!stops.has(cv)){
|
||
const fn = DEMOS[cv.dataset.demo];
|
||
if (fn) stops.set(cv, fn(cv));
|
||
}
|
||
} else if (stops.has(cv)){
|
||
stops.get(cv)();
|
||
stops.delete(cv);
|
||
}
|
||
}
|
||
}, {rootMargin: '120px'});
|
||
canvases.forEach(cv => io.observe(cv));
|
||
} else {
|
||
canvases.forEach(cv => { const fn = DEMOS[cv.dataset.demo]; if (fn) stops.set(cv, fn(cv)); });
|
||
}
|
||
})();
|
||
</script>
|
||
</body>
|
||
</html>
|