Meta Description" name="description" />
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<style>
body { margin: 0; background: #000; color: #fff; text-align: center; touch-action: none; overflow: hidden; height: 100vh; display: flex; flex-direction: column; justify-content: center; }
canvas { background: #111; border: 2px solid #444; width: 95vw; max-width: 400px; margin: 0 auto; display: block; }
#startBtn { background: #28a745; color: #fff; border: none; padding: 20px; font-size: 20px; border-radius: 10px; margin-top: 20px; -webkit-appearance: none; }
</style>
</head>
<body>
<div id="score">SCORE: 0</div>
<canvas id="g" width="400" height="500"></canvas>
<button id="startBtn" onclick="startGame()">點擊這裡開始遊戲</button>
<script>
const canvas = document.getElementById("g"), ctx = canvas.getContext("2d");
let x, y, dx, dy, px, score = 0, bricks = [], alive = false;
function startGame() {
document.getElementById("startBtn").style.display = "none";
x = 200; y = 400; dx = 4; dy = -4; px = 150; score = 0; alive = true;
bricks = [];
for(let i=0; i<4; i++) {
for(let j=0; j<5; j++) bricks.push({x: j*80+5, y: i*30+50, v: 1});
}
loop();
}
function loop() {
if(!alive) return;
ctx.clearRect(0, 0, 400, 500);
// 畫磚塊
ctx.fillStyle = "orange";
bricks.forEach(b => {
if(b.v) {
ctx.fillRect(b.x, b.y, 70, 20);
if(x>b.x && x<b.x+70 && y>b.y && y<b.y+20) { dy=-dy; b.v=0; score++; document.getElementById("score").innerText="SCORE: "+score; }
}
});
// 畫球與擋板
ctx.fillStyle = "#00d2ff"; ctx.beginPath(); ctx.arc(x,y,10,0,Math.PI*2); ctx.fill();
ctx.fillStyle = "#fff"; ctx.fillRect(px, 480, 100, 15);
// 碰撞
if(x<10 || x>390) dx=-dx;
if(y<10) dy=-dy;
if(y>470 && x>px && x<px+100) dy=-dy;
if(y>500) { alert("Game Over"); location.reload(); }
x+=dx; y+=dy;
requestAnimationFrame(loop);
}
// 手指滑動控制
canvas.addEventListener("touchmove", (e) => {
let r = canvas.getBoundingClientRect();
px = (e.touches[0].clientX - r.left) * (400/r.width) - 50;
e.preventDefault();
}, {passive: false});
</script>
</body>
</html>
1
1
3KB
3KB
92.0ms
192.0ms
92.0ms