贪吃蛇
一个增强版的贪吃蛇游戏,包含多种特殊道具(变色、加速、超级食物、毒药等),撞墙会传送到对面,只有撞到自己才会死亡,并且具有积分排行榜功能。修复了游戏开始时立即死亡的bug。
预览
源代码版本 4
export default {
async fetch(request, env) {
const url = new URL(request.url);
// API routes
if (url.pathname.startsWith('/api/')) {
if (request.method === 'OPTIONS') {
return new Response(null, {
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
}
});
}
// Initialize database
if (!env.DB) {
return new Response(JSON.stringify({ error: 'Database not configured' }), {
status: 500,
headers: { 'Content-Type': 'application/json' }
});
}
// Initialize tables
await env.DB.exec(`
CREATE TABLE IF NOT EXISTS scores (
id TEXT PRIMARY KEY,
player_name TEXT NOT NULL,
score INTEGER NOT NULL,
created_at TEXT DEFAULT (datetime('now'))
)
`);
if (url.pathname === '/api/scores' && request.method === 'POST') {
try {
const { playerName, score } = await request.json();
if (!playerName || score === undefined) {
return new Response(JSON.stringify({ error: 'Missing playerName or score' }), {
status: 400,
headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' }
});
}
const id = crypto.randomUUID();
await env.DB.prepare(`
INSERT INTO scores (id, player_name, score, created_at)
VALUES (?1, ?2, ?3, datetime('now'))
`).bind(id, playerName, score).run();
return new Response(JSON.stringify({ ok: true }), {
headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' }
});
} catch (error) {
return new Response(JSON.stringify({ error: 'Failed to save score' }), {
status: 500,
headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' }
});
}
}
if (url.pathname === '/api/scores' && request.method === 'GET') {
try {
const result = await env.DB.prepare(`
SELECT player_name, score, created_at
FROM scores
ORDER BY score DESC, created_at ASC
LIMIT 10
`).all();
return new Response(JSON.stringify({ data: result.results }), {
headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' }
});
} catch (error) {
return new Response(JSON.stringify({ error: 'Failed to fetch scores' }), {
status: 500,
headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' }
});
}
}
return new Response(JSON.stringify({ error: 'Not found' }), {
status: 404,
headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' }
});
}
// Serve HTML
return new Response(getHTML(), {
headers: { 'Content-Type': 'text/html' }
});
}
};
function getHTML() {
return `<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>🐍 超级贪吃蛇大作战</title>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
<style>
body { font-family: 'Inter', sans-serif; }
.game-canvas {
border: 3px solid;
border-image: linear-gradient(45deg, #8b5cf6, #3b82f6) 1;
box-shadow: 0 0 30px rgba(139, 92, 246, 0.3);
}
.shimmer {
background: linear-gradient(90deg, transparent 0%, rgba(255,255,255,0.1) 50%, transparent 100%);
animation: shimmer 1.5s infinite;
}
@keyframes shimmer {
0% { transform: translateX(-100%); }
100% { transform: translateX(100%); }
}
.food-glow {
animation: glow 2s ease-in-out infinite alternate;
}
@keyframes glow {
from { filter: drop-shadow(0 0 5px currentColor); }
to { filter: drop-shadow(0 0 15px currentColor); }
}
.powerup-pulse {
animation: pulse 1s ease-in-out infinite alternate;
}
@keyframes pulse {
from { transform: scale(1); }
to { transform: scale(1.1); }
}
.color-change {
animation: colorChange 0.5s ease-in-out;
}
@keyframes colorChange {
0%, 100% { filter: brightness(1); }
50% { filter: brightness(1.5) hue-rotate(180deg); }
}
</style>
</head>
<body class="bg-gray-900 text-white min-h-screen">
<div class="container mx-auto px-4 py-8">
<!-- Header -->
<div class="text-center mb-8">
<h1 class="text-5xl font-bold bg-gradient-to-r from-violet-500 to-blue-500 bg-clip-text text-transparent mb-4">
🐍 超级贪吃蛇大作战
</h1>
<p class="text-gr
...
还没有评论,来写第一条吧!