Add neon breakout game
This commit is contained in:
+869
@@ -0,0 +1,869 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
|
||||
<title>霓虹打砖块 · Neon Breakout</title>
|
||||
<style>
|
||||
*{box-sizing:border-box;margin:0;padding:0;}
|
||||
html,body{height:100%;}
|
||||
body{
|
||||
background:radial-gradient(circle at 50% -10%, #1b2350 0%, #0a0e20 50%, #04060f 100%);
|
||||
color:#e6f1ff;
|
||||
font-family:ui-sans-serif,system-ui,"Segoe UI",Roboto,"PingFang SC","Microsoft YaHei",sans-serif;
|
||||
display:flex;flex-direction:column;align-items:center;justify-content:center;
|
||||
min-height:100%;padding:16px;gap:12px;
|
||||
-webkit-tap-highlight-color:transparent;overflow:hidden;
|
||||
}
|
||||
#stage{
|
||||
position:relative;width:min(100%,820px);aspect-ratio:4/3;
|
||||
border-radius:18px;
|
||||
box-shadow:0 0 0 1px rgba(120,180,255,.18), 0 0 60px rgba(34,211,238,.18), 0 30px 70px rgba(0,0,0,.6);
|
||||
overflow:hidden;background:#04060f;
|
||||
}
|
||||
canvas{display:block;width:100%;height:100%;touch-action:none;cursor:none;}
|
||||
#overlay{
|
||||
position:absolute;inset:0;display:none;align-items:center;justify-content:center;
|
||||
background:rgba(4,6,15,.78);backdrop-filter:blur(6px);-webkit-backdrop-filter:blur(6px);
|
||||
padding:24px;text-align:center;
|
||||
}
|
||||
#overlay.show{display:flex;}
|
||||
.panel{max-width:520px;animation:pop .28s cubic-bezier(.2,1.4,.4,1);}
|
||||
@keyframes pop{from{opacity:0;transform:translateY(12px) scale(.96);}to{opacity:1;transform:none;}}
|
||||
.panel h1{
|
||||
font-size:clamp(24px,5vw,40px);letter-spacing:.06em;margin-bottom:14px;
|
||||
background:linear-gradient(90deg,#22d3ee,#a78bfa,#f472b6);
|
||||
-webkit-background-clip:text;background-clip:text;color:transparent;
|
||||
filter:drop-shadow(0 0 18px rgba(129,140,248,.55));
|
||||
}
|
||||
.panel p{color:#9fb3d4;line-height:1.75;font-size:clamp(13px,2.4vw,15px);}
|
||||
.panel p b{color:#7dd3fc;font-weight:600;}
|
||||
.keys{margin-top:14px;display:flex;flex-wrap:wrap;gap:8px;justify-content:center;}
|
||||
.keys span{
|
||||
font-size:12px;color:#a8bbd9;background:rgba(120,180,255,.08);
|
||||
border:1px solid rgba(120,180,255,.18);border-radius:8px;padding:5px 10px;
|
||||
}
|
||||
.btn{
|
||||
margin-top:22px;cursor:pointer;border:0;border-radius:12px;padding:13px 34px;
|
||||
font-size:16px;font-weight:700;letter-spacing:.08em;color:#04121a;
|
||||
background:linear-gradient(135deg,#67e8f9,#22d3ee 45%,#818cf8);
|
||||
box-shadow:0 0 24px rgba(34,211,238,.45);
|
||||
transition:transform .12s ease, box-shadow .12s ease;
|
||||
}
|
||||
.btn:hover{transform:translateY(-2px);box-shadow:0 0 36px rgba(34,211,238,.7);}
|
||||
.btn:active{transform:translateY(0) scale(.98);}
|
||||
#toolbar{display:flex;gap:10px;align-items:center;justify-content:center;}
|
||||
.tbtn{
|
||||
cursor:pointer;border:1px solid rgba(120,180,255,.22);background:rgba(120,180,255,.07);
|
||||
color:#a8bbd9;border-radius:10px;padding:8px 16px;font-size:13px;font-weight:600;
|
||||
transition:all .15s ease;
|
||||
}
|
||||
.tbtn:hover{color:#e6f1ff;border-color:rgba(120,180,255,.5);background:rgba(120,180,255,.15);}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div id="stage">
|
||||
<canvas id="game" width="800" height="600"></canvas>
|
||||
<div id="overlay" class="show">
|
||||
<div class="panel">
|
||||
<h1 id="ovTitle">霓虹打砖块</h1>
|
||||
<p id="ovSub">接住小球,击碎所有砖块。<br>挡板中心接球会反弹更直,边缘接球角度更斜。</p>
|
||||
<div class="keys" id="ovKeys">
|
||||
<span>🖱️ 鼠标移动</span><span>👆 手指滑动</span><span>← → / A D</span><span>空格 发球</span><span>P 暂停</span>
|
||||
</div>
|
||||
<button class="btn" id="ovBtn">开始游戏</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="toolbar">
|
||||
<button class="tbtn" id="btnPause">⏸ 暂停</button>
|
||||
<button class="tbtn" id="btnSound">🔊 音效</button>
|
||||
<button class="tbtn" id="btnRestart">↻ 重开</button>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
/* ======================= 基础 ======================= */
|
||||
const canvas = document.getElementById('game');
|
||||
const ctx = canvas.getContext('2d');
|
||||
const W = canvas.width, H = canvas.height;
|
||||
|
||||
const clamp = (v, a, b) => (v < a ? a : v > b ? b : v);
|
||||
const rand = (a, b) => a + Math.random() * (b - a);
|
||||
|
||||
function roundRect(x, y, w, h, r) {
|
||||
r = Math.min(r, w / 2, h / 2);
|
||||
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();
|
||||
}
|
||||
|
||||
/* ======================= 音效 ======================= */
|
||||
let audioCtx = null, muted = false;
|
||||
function initAudio() {
|
||||
if (audioCtx) return;
|
||||
try { audioCtx = new (window.AudioContext || window.webkitAudioContext)(); } catch (e) { audioCtx = null; }
|
||||
}
|
||||
function tone(freq, dur, type, vol, slideTo) {
|
||||
if (muted || !audioCtx) return;
|
||||
const t = audioCtx.currentTime;
|
||||
const o = audioCtx.createOscillator(), g = audioCtx.createGain();
|
||||
o.type = type || 'square';
|
||||
o.frequency.setValueAtTime(freq, t);
|
||||
if (slideTo) o.frequency.exponentialRampToValueAtTime(Math.max(30, slideTo), t + dur);
|
||||
g.gain.setValueAtTime(0.0001, t);
|
||||
g.gain.exponentialRampToValueAtTime(vol || 0.05, t + 0.008);
|
||||
g.gain.exponentialRampToValueAtTime(0.0001, t + dur);
|
||||
o.connect(g); g.connect(audioCtx.destination);
|
||||
o.start(t); o.stop(t + dur + 0.03);
|
||||
}
|
||||
const SFX = {
|
||||
paddle: () => tone(300, 0.08, 'square', 0.055, 540),
|
||||
brick: () => tone(700, 0.07, 'triangle', 0.05, 1050),
|
||||
wall: () => tone(210, 0.05, 'sine', 0.03),
|
||||
power: () => { tone(540, 0.09, 'sine', 0.06, 900); setTimeout(() => tone(950, 0.13, 'sine', 0.05, 1500), 80); },
|
||||
lose: () => tone(330, 0.5, 'sawtooth', 0.06, 70),
|
||||
clear: () => [523, 659, 784, 1046].forEach((f, i) => setTimeout(() => tone(f, 0.22, 'sine', 0.06), i * 110)),
|
||||
win: () => [523, 659, 784, 1046, 1318].forEach((f, i) => setTimeout(() => tone(f, 0.32, 'triangle', 0.06), i * 140))
|
||||
};
|
||||
|
||||
/* ======================= 关卡 ======================= */
|
||||
const LEVELS = [
|
||||
["1111111111",
|
||||
"1111111111",
|
||||
"1111111111"],
|
||||
|
||||
[".11111111.",
|
||||
"1112222111",
|
||||
"1122332211",
|
||||
"1122112211"],
|
||||
|
||||
["2222222222",
|
||||
"2..3333..2",
|
||||
"2.333333.2",
|
||||
"2..3333..2",
|
||||
"2222222222"]
|
||||
];
|
||||
|
||||
const HP_COLORS = {
|
||||
1: ['#67e8f9', '#0891b2'],
|
||||
2: ['#f0abfc', '#a21caf'],
|
||||
3: ['#fcd34d', '#c2410c'],
|
||||
4: ['#fca5a5', '#b91c1c']
|
||||
};
|
||||
|
||||
/* ======================= 状态 ======================= */
|
||||
const state = { mode: 'menu', score: 0, lives: 3, level: 0, timer: 0 };
|
||||
let bricks = [], balls = [], particles = [], powerups = [], stars = [];
|
||||
let stuck = true, wideTimer = 0;
|
||||
let shakeT = 0, shakeMag = 0;
|
||||
|
||||
const paddle = { w: 124, h: 15, baseW: 124, x: W / 2 - 62, y: H - 52, speed: 680 };
|
||||
const keys = Object.create(null);
|
||||
let pointerActive = false;
|
||||
|
||||
/* ======================= 星空背景 ======================= */
|
||||
for (let i = 0; i < 70; i++) {
|
||||
stars.push({ x: Math.random() * W, y: Math.random() * H, r: rand(0.5, 1.7), a: rand(0.08, 0.4), vy: rand(4, 16) });
|
||||
}
|
||||
|
||||
/* ======================= 关卡构建 ======================= */
|
||||
function makeBricks(pattern) {
|
||||
const cols = pattern[0].length;
|
||||
const marginX = 46, marginTop = 86, gap = 8;
|
||||
const bw = (W - marginX * 2 - gap * (cols - 1)) / cols;
|
||||
const bh = 27;
|
||||
const out = [];
|
||||
pattern.forEach((row, r) => {
|
||||
for (let c = 0; c < row.length; c++) {
|
||||
const ch = row[c];
|
||||
if (ch === '.' || ch === ' ') continue;
|
||||
const hp = parseInt(ch, 10);
|
||||
out.push({
|
||||
x: marginX + c * (bw + gap),
|
||||
y: marginTop + r * (bh + gap),
|
||||
w: bw, h: bh, hp, maxHp: hp, alive: true, flash: 0
|
||||
});
|
||||
}
|
||||
});
|
||||
return out;
|
||||
}
|
||||
|
||||
function setPaddleW(w) {
|
||||
const c = paddle.x + paddle.w / 2;
|
||||
paddle.w = w;
|
||||
paddle.x = clamp(c - w / 2, 0, W - w);
|
||||
}
|
||||
|
||||
function resetBall() {
|
||||
balls = [{ x: paddle.x + paddle.w / 2, y: paddle.y - 12, r: 8, vx: 0, vy: 0, trail: [], dead: false }];
|
||||
stuck = true;
|
||||
}
|
||||
|
||||
function loadLevel() {
|
||||
bricks = makeBricks(LEVELS[state.level]);
|
||||
powerups = [];
|
||||
particles = [];
|
||||
wideTimer = 0;
|
||||
setPaddleW(paddle.baseW);
|
||||
paddle.x = W / 2 - paddle.w / 2;
|
||||
resetBall();
|
||||
}
|
||||
|
||||
function startGame() {
|
||||
state.score = 0; state.lives = 3; state.level = 0;
|
||||
loadLevel();
|
||||
state.mode = 'playing';
|
||||
hideOverlay();
|
||||
updateToolbar();
|
||||
}
|
||||
|
||||
function nextLevel() {
|
||||
state.level++;
|
||||
if (state.level >= LEVELS.length) {
|
||||
state.mode = 'won';
|
||||
SFX.win();
|
||||
showOverlay({
|
||||
title: '🏆 全部通关!',
|
||||
sub: '最终得分 <b>' + state.score + '</b><br>你是打砖块大师!',
|
||||
keys: false,
|
||||
btn: '再玩一次',
|
||||
action: startGame
|
||||
});
|
||||
return;
|
||||
}
|
||||
loadLevel();
|
||||
state.mode = 'playing';
|
||||
hideOverlay();
|
||||
}
|
||||
|
||||
/* ======================= 特效 ======================= */
|
||||
function spawnParticles(x, y, colors, count, power) {
|
||||
for (let i = 0; i < count; i++) {
|
||||
const a = Math.random() * Math.PI * 2;
|
||||
const sp = rand(40, 260) * (power || 1);
|
||||
particles.push({
|
||||
x, y, vx: Math.cos(a) * sp, vy: Math.sin(a) * sp - 40,
|
||||
life: rand(0.3, 0.75), max: 0.75, size: rand(1.6, 4.2),
|
||||
color: colors[(Math.random() * colors.length) | 0]
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function updateParticles(dt) {
|
||||
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.vy += 480 * dt;
|
||||
p.x += p.vx * dt;
|
||||
p.y += p.vy * dt;
|
||||
p.vx *= 0.99;
|
||||
}
|
||||
for (const s of stars) {
|
||||
s.y += s.vy * dt;
|
||||
if (s.y > H) { s.y = -4; s.x = Math.random() * W; }
|
||||
}
|
||||
}
|
||||
|
||||
function shake(mag) { shakeMag = Math.max(shakeMag, mag); shakeT = 0.22; }
|
||||
|
||||
/* ======================= 道具 ======================= */
|
||||
const POWER_TYPES = ['wide', 'multi', 'slow', 'life'];
|
||||
const POWER_STYLE = {
|
||||
wide: { color: '#4ade80', label: 'W', name: '加宽挡板' },
|
||||
multi: { color: '#60a5fa', label: 'M', name: '三重球' },
|
||||
slow: { color: '#c084fc', label: 'S', name: '减速' },
|
||||
life: { color: '#fb7185', label: '♥', name: '加命' }
|
||||
};
|
||||
|
||||
function maybeDrop(x, y) {
|
||||
if (Math.random() > 0.14) return;
|
||||
const type = POWER_TYPES[(Math.random() * POWER_TYPES.length) | 0];
|
||||
powerups.push({ x, y, r: 15, vy: 145, type, spin: 0 });
|
||||
}
|
||||
|
||||
function applyPower(type) {
|
||||
SFX.power();
|
||||
shake(4);
|
||||
if (type === 'wide') {
|
||||
setPaddleW(Math.min(paddle.baseW * 1.7, 230));
|
||||
wideTimer = 13;
|
||||
} else if (type === 'multi') {
|
||||
const extra = [];
|
||||
for (const b of balls) {
|
||||
if (balls.length + extra.length >= 6) break;
|
||||
const sp = Math.hypot(b.vx, b.vy) || 380;
|
||||
const base = Math.atan2(b.vy, b.vx);
|
||||
for (const off of [-0.45, 0.45]) {
|
||||
if (balls.length + extra.length >= 6) break;
|
||||
extra.push({
|
||||
x: b.x, y: b.y, r: b.r,
|
||||
vx: Math.cos(base + off) * sp, vy: Math.sin(base + off) * sp,
|
||||
trail: [], dead: false
|
||||
});
|
||||
}
|
||||
}
|
||||
balls = balls.concat(extra);
|
||||
} else if (type === 'slow') {
|
||||
for (const b of balls) {
|
||||
const sp = Math.hypot(b.vx, b.vy);
|
||||
if (sp > 0) {
|
||||
const ns = Math.max(sp * 0.72, 280);
|
||||
b.vx = b.vx / sp * ns; b.vy = b.vy / sp * ns;
|
||||
}
|
||||
}
|
||||
} else if (type === 'life') {
|
||||
state.lives = Math.min(state.lives + 1, 9);
|
||||
}
|
||||
}
|
||||
|
||||
function updatePowerups(dt) {
|
||||
for (let i = powerups.length - 1; i >= 0; i--) {
|
||||
const p = powerups[i];
|
||||
p.y += p.vy * dt;
|
||||
p.spin += dt * 3;
|
||||
if (p.y - p.r > H) { powerups.splice(i, 1); continue; }
|
||||
if (p.y + p.r > paddle.y && p.y - p.r < paddle.y + paddle.h &&
|
||||
p.x + p.r > paddle.x && p.x - p.r < paddle.x + paddle.w) {
|
||||
spawnParticles(p.x, p.y, [POWER_STYLE[p.type].color, '#ffffff'], 16, 0.9);
|
||||
applyPower(p.type);
|
||||
powerups.splice(i, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ======================= 击碎砖块 ======================= */
|
||||
function hitBrick(br) {
|
||||
br.hp--;
|
||||
br.flash = 1;
|
||||
const cols = HP_COLORS[Math.min(br.maxHp, 4)];
|
||||
if (br.hp <= 0) {
|
||||
br.alive = false;
|
||||
state.score += br.maxHp * 10;
|
||||
spawnParticles(br.x + br.w / 2, br.y + br.h / 2, cols, 14, 1);
|
||||
shake(2.5);
|
||||
maybeDrop(br.x + br.w / 2, br.y + br.h / 2);
|
||||
} else {
|
||||
state.score += 5;
|
||||
spawnParticles(br.x + br.w / 2, br.y + br.h / 2, [cols[0]], 4, 0.5);
|
||||
}
|
||||
SFX.brick();
|
||||
}
|
||||
|
||||
/* ======================= 物理 ======================= */
|
||||
function moveBall(b, dt) {
|
||||
b.x += b.vx * dt;
|
||||
b.y += b.vy * dt;
|
||||
|
||||
if (b.x - b.r < 0) { b.x = b.r; b.vx = Math.abs(b.vx); SFX.wall(); }
|
||||
else if (b.x + b.r > W) { b.x = W - b.r; b.vx = -Math.abs(b.vx); SFX.wall(); }
|
||||
if (b.y - b.r < 0) { b.y = b.r; b.vy = Math.abs(b.vy); SFX.wall(); }
|
||||
|
||||
// 挡板
|
||||
if (b.vy > 0 &&
|
||||
b.x + b.r > paddle.x && b.x - b.r < paddle.x + paddle.w &&
|
||||
b.y + b.r > paddle.y && b.y - b.r < paddle.y + paddle.h) {
|
||||
b.y = paddle.y - b.r;
|
||||
const rel = clamp((b.x - (paddle.x + paddle.w / 2)) / (paddle.w / 2), -1, 1);
|
||||
const ang = rel * (Math.PI / 3);
|
||||
const sp = clamp(Math.hypot(b.vx, b.vy) * 1.012, 300, 760);
|
||||
b.vx = sp * Math.sin(ang);
|
||||
b.vy = -sp * Math.cos(ang);
|
||||
SFX.paddle();
|
||||
spawnParticles(b.x, paddle.y, ['#67e8f9'], 5, 0.5);
|
||||
}
|
||||
|
||||
// 砖块
|
||||
for (let i = 0; i < bricks.length; i++) {
|
||||
const br = bricks[i];
|
||||
if (!br.alive) continue;
|
||||
if (b.x + b.r < br.x || b.x - b.r > br.x + br.w ||
|
||||
b.y + b.r < br.y || b.y - b.r > br.y + br.h) continue;
|
||||
|
||||
const cx = br.x + br.w / 2, cy = br.y + br.h / 2;
|
||||
const ox = (b.r + br.w / 2) - Math.abs(b.x - cx);
|
||||
const oy = (b.r + br.h / 2) - Math.abs(b.y - cy);
|
||||
if (ox < oy) {
|
||||
b.vx = -b.vx;
|
||||
b.x += (b.x < cx ? -ox : ox);
|
||||
} else {
|
||||
b.vy = -b.vy;
|
||||
b.y += (b.y < cy ? -oy : oy);
|
||||
}
|
||||
hitBrick(br);
|
||||
break;
|
||||
}
|
||||
|
||||
if (b.y - b.r > H + 20) b.dead = true;
|
||||
}
|
||||
|
||||
function launchBall() {
|
||||
if (!stuck || balls.length === 0) return;
|
||||
stuck = false;
|
||||
const sp = 400 + state.level * 32;
|
||||
const ang = -Math.PI / 2 + rand(-0.32, 0.32);
|
||||
balls[0].vx = Math.cos(ang) * sp;
|
||||
balls[0].vy = Math.sin(ang) * sp;
|
||||
SFX.paddle();
|
||||
}
|
||||
|
||||
function loseLife() {
|
||||
state.lives--;
|
||||
wideTimer = 0;
|
||||
setPaddleW(paddle.baseW);
|
||||
if (state.lives <= 0) {
|
||||
state.mode = 'gameover';
|
||||
SFX.lose();
|
||||
showOverlay({
|
||||
title: '💥 游戏结束',
|
||||
sub: '本局得分 <b>' + state.score + '</b><br>再来一次,挑战更高分!',
|
||||
keys: false,
|
||||
btn: '重新开始',
|
||||
action: startGame
|
||||
});
|
||||
} else {
|
||||
resetBall();
|
||||
}
|
||||
}
|
||||
|
||||
/* ======================= 主更新 ======================= */
|
||||
function update(dt) {
|
||||
updateParticles(dt);
|
||||
|
||||
if (shakeT > 0) {
|
||||
shakeT -= dt;
|
||||
shakeMag *= 0.88;
|
||||
if (shakeT <= 0) shakeMag = 0;
|
||||
}
|
||||
for (const br of bricks) if (br.flash > 0) br.flash = Math.max(0, br.flash - dt * 4);
|
||||
for (const b of balls) if (b.vx || b.vy) { /* trail handled below */ }
|
||||
|
||||
if (state.mode === 'levelclear') {
|
||||
state.timer -= dt;
|
||||
if (state.timer <= 0) nextLevel();
|
||||
return;
|
||||
}
|
||||
if (state.mode !== 'playing') return;
|
||||
|
||||
// 挡板键盘控制
|
||||
let dir = 0;
|
||||
if (keys['ArrowLeft'] || keys['a'] || keys['A']) dir -= 1;
|
||||
if (keys['ArrowRight'] || keys['d'] || keys['D']) dir += 1;
|
||||
if (dir !== 0) {
|
||||
pointerActive = false;
|
||||
paddle.x += dir * paddle.speed * dt;
|
||||
}
|
||||
paddle.x = clamp(paddle.x, 0, W - paddle.w);
|
||||
|
||||
// 加宽倒计时
|
||||
if (wideTimer > 0) {
|
||||
wideTimer -= dt;
|
||||
if (wideTimer <= 0) setPaddleW(paddle.baseW);
|
||||
}
|
||||
|
||||
// 小球吸附挡板
|
||||
if (stuck && balls.length) {
|
||||
balls[0].x = paddle.x + paddle.w / 2;
|
||||
balls[0].y = paddle.y - 12;
|
||||
balls[0].trail.length = 0;
|
||||
}
|
||||
|
||||
// 子步进物理,防止穿模
|
||||
const step = 1 / 240;
|
||||
let remain = dt;
|
||||
while (remain > 0) {
|
||||
const s = Math.min(step, remain);
|
||||
remain -= s;
|
||||
for (const b of balls) if (!b.dead) moveBall(b, s);
|
||||
}
|
||||
|
||||
// 拖尾
|
||||
for (const b of balls) {
|
||||
if (b.vx || b.vy) {
|
||||
b.trail.push({ x: b.x, y: b.y });
|
||||
if (b.trail.length > 12) b.trail.shift();
|
||||
}
|
||||
}
|
||||
|
||||
balls = balls.filter(b => !b.dead);
|
||||
updatePowerups(dt);
|
||||
|
||||
if (balls.length === 0) loseLife();
|
||||
|
||||
// 过关判定
|
||||
if (state.mode === 'playing' && bricks.every(b => !b.alive)) {
|
||||
state.mode = 'levelclear';
|
||||
state.timer = 1.1;
|
||||
SFX.clear();
|
||||
for (let i = 0; i < 40; i++) {
|
||||
spawnParticles(rand(0, W), rand(60, 380), ['#67e8f9', '#f0abfc', '#fcd34d'], 1, 1.4);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ======================= 渲染 ======================= */
|
||||
function drawBackground() {
|
||||
const g = ctx.createRadialGradient(W / 2, -80, 40, W / 2, H * 0.75, H * 1.25);
|
||||
g.addColorStop(0, '#141c3d');
|
||||
g.addColorStop(0.55, '#080c1c');
|
||||
g.addColorStop(1, '#04060f');
|
||||
ctx.fillStyle = g;
|
||||
ctx.fillRect(0, 0, W, H);
|
||||
|
||||
ctx.save();
|
||||
ctx.strokeStyle = 'rgba(110,170,255,.05)';
|
||||
ctx.lineWidth = 1;
|
||||
for (let x = 0; x <= W; x += 40) { ctx.beginPath(); ctx.moveTo(x, 0); ctx.lineTo(x, H); ctx.stroke(); }
|
||||
for (let y = 0; y <= H; y += 40) { ctx.beginPath(); ctx.moveTo(0, y); ctx.lineTo(W, y); ctx.stroke(); }
|
||||
ctx.restore();
|
||||
|
||||
for (const s of stars) {
|
||||
ctx.globalAlpha = s.a;
|
||||
ctx.fillStyle = '#cfe6ff';
|
||||
ctx.beginPath(); ctx.arc(s.x, s.y, s.r, 0, Math.PI * 2); ctx.fill();
|
||||
}
|
||||
ctx.globalAlpha = 1;
|
||||
}
|
||||
|
||||
function drawHUD() {
|
||||
ctx.save();
|
||||
ctx.textBaseline = 'middle';
|
||||
|
||||
ctx.textAlign = 'left';
|
||||
ctx.font = '600 14px ui-monospace, Menlo, Consolas, monospace';
|
||||
ctx.fillStyle = '#7f93b8';
|
||||
ctx.fillText('SCORE', 28, 32);
|
||||
ctx.font = '700 26px ui-monospace, Menlo, Consolas, monospace';
|
||||
ctx.fillStyle = '#e6f1ff';
|
||||
ctx.shadowColor = 'rgba(103,232,249,.7)';
|
||||
ctx.shadowBlur = 14;
|
||||
ctx.fillText(String(state.score).padStart(6, '0'), 28, 58);
|
||||
ctx.shadowBlur = 0;
|
||||
|
||||
ctx.textAlign = 'center';
|
||||
ctx.font = '600 15px ui-monospace, Menlo, Consolas, monospace';
|
||||
ctx.fillStyle = '#7f93b8';
|
||||
ctx.fillText('LEVEL ' + (state.level + 1) + ' / ' + LEVELS.length, W / 2, 44);
|
||||
|
||||
ctx.textAlign = 'right';
|
||||
ctx.font = '600 14px ui-monospace, Menlo, Consolas, monospace';
|
||||
ctx.fillStyle = '#7f93b8';
|
||||
ctx.fillText('LIVES', W - 28, 32);
|
||||
for (let i = 0; i < state.lives; i++) {
|
||||
const x = W - 34 - i * 22, y = 58;
|
||||
ctx.beginPath();
|
||||
ctx.arc(x, y, 7, 0, Math.PI * 2);
|
||||
ctx.fillStyle = '#67e8f9';
|
||||
ctx.shadowColor = '#22d3ee';
|
||||
ctx.shadowBlur = 12;
|
||||
ctx.fill();
|
||||
}
|
||||
ctx.shadowBlur = 0;
|
||||
|
||||
// 加宽倒计时条
|
||||
if (wideTimer > 0) {
|
||||
ctx.textAlign = 'center';
|
||||
ctx.font = '700 13px ui-monospace, monospace';
|
||||
ctx.fillStyle = '#4ade80';
|
||||
ctx.fillText('WIDE ' + wideTimer.toFixed(1) + 's', W / 2, 70);
|
||||
}
|
||||
ctx.restore();
|
||||
}
|
||||
|
||||
function drawBricks() {
|
||||
for (const br of bricks) {
|
||||
if (!br.alive) continue;
|
||||
const c = HP_COLORS[Math.min(br.hp, 4)] || HP_COLORS[1];
|
||||
ctx.save();
|
||||
ctx.shadowColor = c[0];
|
||||
ctx.shadowBlur = 14;
|
||||
const g = ctx.createLinearGradient(br.x, br.y, br.x, br.y + br.h);
|
||||
g.addColorStop(0, c[0]);
|
||||
g.addColorStop(1, c[1]);
|
||||
ctx.fillStyle = g;
|
||||
roundRect(br.x, br.y, br.w, br.h, 7);
|
||||
ctx.fill();
|
||||
ctx.restore();
|
||||
|
||||
// 高光
|
||||
ctx.save();
|
||||
ctx.globalAlpha = 0.35;
|
||||
ctx.fillStyle = '#ffffff';
|
||||
roundRect(br.x + 3, br.y + 3, br.w - 6, br.h * 0.28, 4);
|
||||
ctx.fill();
|
||||
ctx.restore();
|
||||
|
||||
// 受击闪白
|
||||
if (br.flash > 0) {
|
||||
ctx.save();
|
||||
ctx.globalAlpha = br.flash * 0.85;
|
||||
ctx.fillStyle = '#ffffff';
|
||||
roundRect(br.x, br.y, br.w, br.h, 7);
|
||||
ctx.fill();
|
||||
ctx.restore();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function drawPaddle() {
|
||||
ctx.save();
|
||||
const g = ctx.createLinearGradient(paddle.x, paddle.y, paddle.x + paddle.w, paddle.y + paddle.h);
|
||||
g.addColorStop(0, '#67e8f9');
|
||||
g.addColorStop(0.5, '#a78bfa');
|
||||
g.addColorStop(1, '#f472b6');
|
||||
ctx.fillStyle = g;
|
||||
ctx.shadowColor = '#818cf8';
|
||||
ctx.shadowBlur = 22;
|
||||
roundRect(paddle.x, paddle.y, paddle.w, paddle.h, 7.5);
|
||||
ctx.fill();
|
||||
ctx.restore();
|
||||
|
||||
ctx.save();
|
||||
ctx.globalAlpha = 0.5;
|
||||
ctx.fillStyle = '#ffffff';
|
||||
roundRect(paddle.x + 5, paddle.y + 3, paddle.w - 10, 3.5, 2);
|
||||
ctx.fill();
|
||||
ctx.restore();
|
||||
}
|
||||
|
||||
function drawBalls() {
|
||||
for (const b of balls) {
|
||||
// 拖尾
|
||||
for (let i = 0; i < b.trail.length; i++) {
|
||||
const t = b.trail[i];
|
||||
const k = (i + 1) / b.trail.length;
|
||||
ctx.globalAlpha = k * 0.32;
|
||||
ctx.fillStyle = '#7dd3fc';
|
||||
ctx.beginPath();
|
||||
ctx.arc(t.x, t.y, b.r * (0.25 + 0.65 * k), 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
}
|
||||
ctx.globalAlpha = 1;
|
||||
|
||||
ctx.save();
|
||||
const g = ctx.createRadialGradient(b.x - 3, b.y - 3, 1, b.x, b.y, b.r);
|
||||
g.addColorStop(0, '#ffffff');
|
||||
g.addColorStop(0.5, '#a5f3fc');
|
||||
g.addColorStop(1, '#22d3ee');
|
||||
ctx.fillStyle = g;
|
||||
ctx.shadowColor = '#22d3ee';
|
||||
ctx.shadowBlur = 22;
|
||||
ctx.beginPath();
|
||||
ctx.arc(b.x, b.y, b.r, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
ctx.restore();
|
||||
}
|
||||
|
||||
// 瞄准虚线
|
||||
if (stuck && balls.length) {
|
||||
const b = balls[0];
|
||||
ctx.save();
|
||||
ctx.strokeStyle = 'rgba(103,232,249,.45)';
|
||||
ctx.lineWidth = 2;
|
||||
ctx.setLineDash([7, 9]);
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(b.x, b.y - b.r - 6);
|
||||
ctx.lineTo(b.x, b.y - 150);
|
||||
ctx.stroke();
|
||||
ctx.restore();
|
||||
}
|
||||
}
|
||||
|
||||
function drawPowerups() {
|
||||
for (const p of powerups) {
|
||||
const s = POWER_STYLE[p.type];
|
||||
ctx.save();
|
||||
ctx.translate(p.x, p.y);
|
||||
ctx.rotate(Math.sin(p.spin) * 0.25);
|
||||
ctx.shadowColor = s.color;
|
||||
ctx.shadowBlur = 18;
|
||||
ctx.fillStyle = 'rgba(6,10,22,.9)';
|
||||
roundRect(-p.r, -p.r, p.r * 2, p.r * 2, 7);
|
||||
ctx.fill();
|
||||
ctx.strokeStyle = s.color;
|
||||
ctx.lineWidth = 2.5;
|
||||
ctx.stroke();
|
||||
ctx.shadowBlur = 0;
|
||||
ctx.fillStyle = s.color;
|
||||
ctx.font = '700 15px ui-sans-serif, system-ui, sans-serif';
|
||||
ctx.textAlign = 'center';
|
||||
ctx.textBaseline = 'middle';
|
||||
ctx.fillText(s.label, 0, 1);
|
||||
ctx.restore();
|
||||
}
|
||||
}
|
||||
|
||||
function drawParticles() {
|
||||
for (const p of particles) {
|
||||
const a = clamp(p.life / p.max, 0, 1);
|
||||
ctx.globalAlpha = a;
|
||||
ctx.fillStyle = p.color;
|
||||
ctx.beginPath();
|
||||
ctx.arc(p.x, p.y, p.size * a, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
}
|
||||
ctx.globalAlpha = 1;
|
||||
}
|
||||
|
||||
function render() {
|
||||
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
||||
ctx.clearRect(0, 0, W, H);
|
||||
drawBackground();
|
||||
|
||||
ctx.save();
|
||||
if (shakeMag > 0.3) ctx.translate(rand(-shakeMag, shakeMag), rand(-shakeMag, shakeMag));
|
||||
|
||||
drawBricks();
|
||||
drawPowerups();
|
||||
drawParticles();
|
||||
drawPaddle();
|
||||
drawBalls();
|
||||
|
||||
ctx.restore();
|
||||
|
||||
drawHUD();
|
||||
}
|
||||
|
||||
/* ======================= 循环 ======================= */
|
||||
let last = performance.now();
|
||||
function loop(now) {
|
||||
let dt = (now - last) / 1000;
|
||||
last = now;
|
||||
if (!isFinite(dt) || dt < 0) dt = 0;
|
||||
dt = Math.min(dt, 1 / 30);
|
||||
update(dt);
|
||||
render();
|
||||
requestAnimationFrame(loop);
|
||||
}
|
||||
|
||||
/* ======================= 覆盖层 ======================= */
|
||||
const overlay = document.getElementById('overlay');
|
||||
const ovTitle = document.getElementById('ovTitle');
|
||||
const ovSub = document.getElementById('ovSub');
|
||||
const ovBtn = document.getElementById('ovBtn');
|
||||
const ovKeys = document.getElementById('ovKeys');
|
||||
let ovAction = null;
|
||||
|
||||
function showOverlay(o) {
|
||||
ovTitle.innerHTML = o.title;
|
||||
ovSub.innerHTML = o.sub;
|
||||
ovBtn.textContent = o.btn;
|
||||
ovKeys.style.display = o.keys === false ? 'none' : 'flex';
|
||||
ovAction = o.action;
|
||||
overlay.classList.add('show');
|
||||
}
|
||||
function hideOverlay() { overlay.classList.remove('show'); }
|
||||
|
||||
ovBtn.addEventListener('click', () => {
|
||||
initAudio();
|
||||
if (audioCtx && audioCtx.state === 'suspended') audioCtx.resume();
|
||||
if (ovAction) ovAction();
|
||||
});
|
||||
|
||||
/* ======================= 暂停 ======================= */
|
||||
function togglePause() {
|
||||
if (state.mode === 'playing') {
|
||||
state.mode = 'paused';
|
||||
showOverlay({ title: '⏸ 已暂停', sub: '需要歇一会儿?', keys: false, btn: '继续游戏', action: resume });
|
||||
} else if (state.mode === 'paused') {
|
||||
resume();
|
||||
}
|
||||
}
|
||||
function resume() {
|
||||
state.mode = 'playing';
|
||||
hideOverlay();
|
||||
}
|
||||
function updateToolbar() {
|
||||
document.getElementById('btnPause').textContent = state.mode === 'paused' ? '▶ 继续' : '⏸ 暂停';
|
||||
}
|
||||
|
||||
/* ======================= 输入 ======================= */
|
||||
function pointerToX(clientX) {
|
||||
const rect = canvas.getBoundingClientRect();
|
||||
return (clientX - rect.left) * (W / rect.width);
|
||||
}
|
||||
|
||||
canvas.addEventListener('pointermove', (e) => {
|
||||
if (state.mode !== 'playing') return;
|
||||
pointerActive = true;
|
||||
const x = pointerToX(e.clientX);
|
||||
paddle.x = clamp(x - paddle.w / 2, 0, W - paddle.w);
|
||||
});
|
||||
|
||||
canvas.addEventListener('pointerdown', (e) => {
|
||||
initAudio();
|
||||
if (audioCtx && audioCtx.state === 'suspended') audioCtx.resume();
|
||||
if (state.mode === 'playing') {
|
||||
pointerActive = true;
|
||||
const x = pointerToX(e.clientX);
|
||||
paddle.x = clamp(x - paddle.w / 2, 0, W - paddle.w);
|
||||
if (stuck) launchBall();
|
||||
}
|
||||
});
|
||||
|
||||
window.addEventListener('keydown', (e) => {
|
||||
const k = e.key;
|
||||
if (['ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown', ' ', 'Spacebar'].includes(k)) e.preventDefault();
|
||||
keys[k] = true;
|
||||
|
||||
if (k === ' ' || k === 'Spacebar' || k === 'Enter') {
|
||||
initAudio();
|
||||
if (audioCtx && audioCtx.state === 'suspended') audioCtx.resume();
|
||||
if (state.mode === 'playing') launchBall();
|
||||
else if (state.mode === 'menu' || state.mode === 'gameover' || state.mode === 'won') {
|
||||
if (ovAction) ovAction();
|
||||
}
|
||||
}
|
||||
if (k === 'p' || k === 'P' || k === 'Escape') togglePause();
|
||||
|
||||
if (k === 'm' || k === 'M') toggleSound();
|
||||
updateToolbar();
|
||||
});
|
||||
|
||||
window.addEventListener('keyup', (e) => { keys[e.key] = false; });
|
||||
window.addEventListener('blur', () => {
|
||||
for (const k in keys) keys[k] = false;
|
||||
if (state.mode === 'playing') togglePause();
|
||||
});
|
||||
|
||||
/* ======================= 工具栏 ======================= */
|
||||
function toggleSound() {
|
||||
muted = !muted;
|
||||
document.getElementById('btnSound').textContent = muted ? '🔇 静音' : '🔊 音效';
|
||||
if (!muted) { initAudio(); if (audioCtx && audioCtx.state === 'suspended') audioCtx.resume(); }
|
||||
}
|
||||
document.getElementById('btnSound').addEventListener('click', toggleSound);
|
||||
document.getElementById('btnPause').addEventListener('click', () => { initAudio(); togglePause(); updateToolbar(); });
|
||||
document.getElementById('btnRestart').addEventListener('click', () => {
|
||||
initAudio();
|
||||
if (audioCtx && audioCtx.state === 'suspended') audioCtx.resume();
|
||||
startGame();
|
||||
updateToolbar();
|
||||
});
|
||||
|
||||
/* ======================= 初始化 ======================= */
|
||||
bricks = makeBricks(LEVELS[0]);
|
||||
resetBall();
|
||||
|
||||
showOverlay({
|
||||
title: '霓虹打砖块',
|
||||
sub: '接住小球,击碎所有砖块。<br>挡板中心接球反弹更直,边缘接球角度更斜。',
|
||||
keys: true,
|
||||
btn: '开始游戏',
|
||||
action: startGame
|
||||
});
|
||||
|
||||
requestAnimationFrame(loop);
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user