可以帮我写一个贪吃蛇游戏的前端页面吗
- User
可以帮我写一个贪吃蛇游戏的前端页面吗
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
可以帮我写一个贪吃蛇游戏的前端页面吗
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
当然可以!我来为您创建一个完整的贪吃蛇游戏前端页面。这将是一个单独的HTML文件,包含了所有必要的HTML、CSS和JavaScript代码。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>贪吃蛇游戏</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
font-family: 'Arial', sans-serif;
}
.game-container {
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border-radius: 20px;
padding: 30px;
box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37);
border: 1px solid rgba(255, 255, 255, 0.18);
}
h1 {
text-align: center;
color: #fff;
margin-bottom: 20px;
font-size: 2.5em;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}
.game-info {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
color: #fff;
font-size: 1.2em;
}
.score {
background: rgba(255, 255, 255, 0.2);
padding: 10px 20px;
border-radius: 10px;
min-width: 120px;
text-align: center;
}
.controls {
display: flex;
gap: 10px;
}
button {
background: rgba(255, 255, 255, 0.2);
color: #fff;
border: none;
padding: 10px 20px;
border-radius: 10px;
cursor: pointer;
font-size: 1em;
transition: all 0.3s ease;
}
button:hover {
background: rgba(255, 255, 255, 0.3);
transform: translateY(-2px);
}
button:active {
transform: translateY(0);
}
#gameCanvas {
border: 2px solid rgba(255, 255, 255, 0.3);
border-radius: 10px;
background: rgba(0, 0, 0, 0.5);
display: block;
margin: 0 auto;
}
.game-over {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(0, 0, 0, 0.8);
color: #fff;
padding: 30px;
border-radius: 10px;
text-align: center;
display: none;
}
.game-over h2 {
font-size: 2em;
margin-bottom: 10px;
}
.game-over p {
font-size: 1.2em;
margin-bottom: 20px;
}
.instructions {
text-align: center;
color: #fff;
margin-top: 20px;
font-size: 0.9em;
opacity: 0.8;
}
.speed-control {
display: flex;
align-items: center;
gap: 10px;
color: #fff;
}
select {
background: rgba(255, 255, 255, 0.2);
color: #fff;
border: none;
padding: 5px 10px;
border-radius: 5px;
cursor: pointer;
}
select option {
background: #764ba2;
}
</style>
</head>
<body>
<div class="game-container">
<h1>🐍 贪吃蛇游戏</h1>
<div class="game-info">
<div class="score">
得分: <span id="score">0</span>
</div>
<div class="speed-control">
<label>速度:</label>
<select id="speedSelect">
<option value="200">慢速</option>
<option value="150" selected>正常</option>
<option value="100">快速</option>
<option value="50">极速</option>
</select>
</div>
<div class="controls">
<button id="startBtn">开始游戏</button>
<button id="pauseBtn">暂停</button>
<button id="resetBtn">重置</button>
</div>
</div>
<canvas id="gameCanvas" width="600" height="600"></canvas>
<div class="instructions">
使用方向键 ↑ ↓ ← → 或 W A S D 控制蛇的移动
</div>
<div class="game-over" id="gameOver">
<h2>游戏结束!</h2>
<p>最终得分: <span id="finalScore">0</span></p>
<button onclick="resetGame()">重新开始</button>
</div>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const finalScoreElement = document.getElementById('finalScore');
const gameOverElement = document.getElementById('gameOver');
const startBtn = document.getElementById('startBtn');
const pauseBtn = document.getElementById('pauseBtn');
const resetBtn = document.getElementById('resetBtn');
const speedSelect = document.getElementById('speedSelect');
// 游戏配置
const gridSize = 20;
const cellSize = canvas.width / gridSize;
let snake = [{x: 10, y: 10}];
let direction = {x: 0, y: 0};
let food = generateFood();
let score = 0;
let gameRunning = false;
let gamePaused = false;
let gameSpeed = 150;
// 生成食物
function generateFood() {
let newFood;
do {
newFood = {
x: Math.floor(Math.random() * gridSize),
y: Math.floor(Math.random() * gridSize)
};
} while (snake.some(segment => segment.x === newFood.x && segment.y === newFood.y));
return newFood;
}
// 绘制游戏画面
function draw() {
// 清空画布
ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 绘制网格
ctx.strokeStyle = 'rgba(255, 255, 255, 0.1)';
ctx.lineWidth = 0.5;
for (let i = 0; i <= gridSize; i++) {
ctx.beginPath();
ctx.moveTo(i * cellSize, 0);
ctx.lineTo(i * cellSize, canvas.height);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, i * cellSize);
ctx.lineTo(canvas.width, i * cellSize);
ctx.stroke();
}
// 绘制蛇
snake.forEach((segment, index) => {
// 渐变色效果
const hue = (index / snake.length) * 60 + 120; // 从绿色到黄色
ctx.fillStyle = `hsl(${hue}, 70%, 50%)`;
// 蛇身
ctx.fillRect(
segment.x * cellSize + 2,
segment.y * cellSize + 2,
cellSize - 4,
cellSize - 4
);
// 蛇头特殊绘制
if (index === 0) {
ctx.fillStyle = '#fff';
// 画眼睛
if (direction.x === 1) { // 向右
ctx.fillRect(segment.x * cellSize + cellSize - 8, segment.y * cellSize + 5, 3, 3);
ctx.fillRect(segment.x * cellSize + cellSize - 8, segment.y * cellSize + cellSize - 8, 3, 3);
} else if (direction.x === -1) { // 向左
ctx.fillRect(segment.x * cellSize + 5, segment.y * cellSize + 5, 3, 3);
ctx.fillRect(segment.x * cellSize + 5, segment.y * cellSize + cellSize - 8, 3, 3);
} else if (direction.y === -1) { // 向上
ctx.fillRect(segment.x * cellSize + 5, segment.y * cellSize + 5, 3, 3);
ctx.fillRect(segment.x * cellSize + cellSize - 8, segment.y * cellSize + 5, 3, 3);
} else if (direction.y === 1) { // 向下
ctx.fillRect(segment.x * cellSize + 5, segment.y * cellSize + cellSize - 8, 3, 3);
ctx.fillRect(segment.x * cellSize + cellSize - 8, segment.y * cellSize + cellSize - 8, 3, 3);
}
}
});
// 绘制食物(苹果样式)
ctx.fillStyle = '#ff6b6b';
ctx.beginPath();
ctx.arc(
food.x * cellSize + cellSize / 2,
food.y * cellSize + cellSize / 2,
cellSize / 2 - 4,
0,
Math.PI * 2
);
ctx.fill();
// 食物高光效果
ctx.fillStyle = 'rgba(255, 255, 255, 0.3)';
ctx.beginPath();
ctx.arc(
food.x * cellSize + cellSize / 2 - 5,
food.y * cellSize + cellSize / 2 - 5,
cellSize / 4,
0,
Math.PI * 2
);
ctx.fill();
}
// 更新游戏状态
function update() {
if (!gameRunning || gamePaused) return;
// 移动蛇头
const head = {x: snake[0].x + direction.x, y: snake[0].y + direction.y};
// 检查是否撞墙
if (head.x < 0 || head.x >= gridSize || head.y < 0 || head.y >= gridSize) {
gameOver();
return;
}
// 检查是否撞到自己
if (snake.some(segment => segment.x === head.x && segment.y === head.y)) {
gameOver();
return;
}
snake.unshift(head);
// 检查是否吃到食物
if (head.x === food.x && head.y === food.y) {
score += 10;
scoreElement.textContent = score;
food = generateFood();
// 吃到食物时播放一个小动画
ctx.fillStyle = '#fff';
ctx.fillRect(
food.x * cellSize - 5,
food.y * cellSize - 5,
cellSize + 10,
cellSize + 10
);
} else {
snake.pop();
}
}
// 游戏主循环
function gameLoop() {
update();
draw();
setTimeout(gameLoop, gameSpeed);
}
// 键盘控制
document.addEventListener('keydown', (e) => {
if (!gameRunning || gamePaused) return;
switch(e.key) {
case 'ArrowUp':
case 'w':
case 'W':
if (direction.y === 0) {
direction = {x: 0, y: -1};
}
break;
case 'ArrowDown':
case 's':
case 'S':
if (direction.y === 0) {
direction = {x: 0, y: 1};
}
break;
case 'ArrowLeft':
case 'a':
case 'A':
if (direction.x === 0) {
direction = {x: -1, y: 0};
}
break;
case 'ArrowRight':
case 'd':
case 'D':
if (direction.x === 0) {
direction = {x: 1, y: 0};
}
break;
}
});
// 游戏结束
function gameOver() {
gameRunning = false;
finalScoreElement.textContent = score;
gameOverElement.style.display = 'block';
}
// 开始游戏
function startGame() {
if (!gameRunning) {
gameRunning = true;
gamePaused = false;
direction = {x: 1, y: 0};
gameLoop();
startBtn.textContent = '游戏中';
startBtn.disabled = true;
}
}
// 暂停游戏
function pauseGame() {
if (gameRunning) {
gamePaused = !gamePaused;
pauseBtn.textContent = gamePaused ? '继续' : '暂停';
}
}
// 重置游戏
function resetGame() {
gameRunning = false;
gamePaused = false;
snake = [{x: 10, y: 10}];
direction = {x: 0, y: 0};
food = generateFood();
score = 0;
scoreElement.textContent = score;
gameOverElement.style.display = 'none';
startBtn.textContent = '开始游戏';
startBtn.disabled = false;
pauseBtn.textContent = '暂停';
draw();
}
// 速度控制
speedSelect.addEventListener('change', (e) => {
gameSpeed = parseInt(e.target.value);
});
// 按钮事件
startBtn.addEventListener('click', startGame);
pauseBtn.addEventListener('click', pauseGame);
resetBtn.addEventListener('click', resetGame);
// 初始化
draw();
</script>
</body>
</html>
这个贪吃蛇游戏包含了以下功能:
游戏特性:
游戏规则:
使用方法:
.html 文件(例如:snake-game.html)游戏界面采用了现代化的设计风格,包括毛玻璃效果、渐变色彩和流畅的动画。希望您喜欢这个游戏!
我想让贪吃蛇的样子更加精美一些
我来为您升级贪吃蛇的视觉效果,让它看起来更加精美和现代化!
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>精美贪吃蛇游戏</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: linear-gradient(135deg, #1e3c72 0%, #2a5298 50%, #7e22ce 100%);
font-family: 'Arial', sans-serif;
overflow: hidden;
}
/* 动态背景 */
.bg-animation {
position: fixed;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: -1;
opacity: 0.3;
}
.bg-animation span {
position: absolute;
display: block;
width: 20px;
height: 20px;
background: rgba(255, 255, 255, 0.2);
animation: move 25s linear infinite;
bottom: -150px;
}
@keyframes move {
0% {
transform: translateY(0) rotate(0deg);
opacity: 1;
border-radius: 0;
}
100% {
transform: translateY(-1000px) rotate(720deg);
opacity: 0;
border-radius: 50%;
}
}
.game-container {
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border-radius: 20px;
padding: 30px;
box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37);
border: 1px solid rgba(255, 255, 255, 0.18);
position: relative;
}
h1 {
text-align: center;
color: #fff;
margin-bottom: 20px;
font-size: 2.5em;
text-shadow: 0 0 20px rgba(255, 255, 255, 0.5);
animation: glow 2s ease-in-out infinite alternate;
}
@keyframes glow {
from { text-shadow: 0 0 20px rgba(255, 255, 255, 0.5); }
to { text-shadow: 0 0 30px rgba(255, 255, 255, 0.8), 0 0 40px rgba(255, 255, 255, 0.5); }
}
.game-info {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
color: #fff;
font-size: 1.2em;
}
.score {
background: linear-gradient(135deg, rgba(255, 255, 255, 0.2), rgba(255, 255, 255, 0.1));
padding: 10px 20px;
border-radius: 10px;
min-width: 120px;
text-align: center;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
}
.controls {
display: flex;
gap: 10px;
}
button {
background: linear-gradient(135deg, rgba(255, 255, 255, 0.3), rgba(255, 255, 255, 0.1));
color: #fff;
border: none;
padding: 10px 20px;
border-radius: 10px;
cursor: pointer;
font-size: 1em;
transition: all 0.3s ease;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
}
button:hover {
background: linear-gradient(135deg, rgba(255, 255, 255, 0.4), rgba(255, 255, 255, 0.2));
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
}
#gameCanvas {
border: 2px solid rgba(255, 255, 255, 0.3);
border-radius: 10px;
background: rgba(0, 0, 0, 0.5);
display: block;
margin: 0 auto;
box-shadow: 0 0 50px rgba(0, 0, 0, 0.5) inset;
}
.game-over {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(0, 0, 0, 0.9);
color: #fff;
padding: 40px;
border-radius: 20px;
text-align: center;
display: none;
box-shadow: 0 0 50px rgba(255, 255, 255, 0.3);
}
.speed-control {
display: flex;
align-items: center;
gap: 10px;
color: #fff;
}
select {
background: rgba(255, 255, 255, 0.2);
color: #fff;
border: none;
padding: 5px 10px;
border-radius: 5px;
cursor: pointer;
}
</style>
</head>
<body>
<!-- 动态背景 -->
<div class="bg-animation">
<span style="left: 10%; width: 20px; height: 20px; animation-delay: 0s;"></span>
<span style="left: 20%; width: 30px; height: 30px; animation-delay: 3s; animation-duration: 20s;"></span>
<span style="left: 30%; width: 15px; height: 15px; animation-delay: 2s;"></span>
<span style="left: 40%; width: 25px; height: 25px; animation-delay: 0s; animation-duration: 18s;"></span>
<span style="left: 50%; width: 20px; height: 20px; animation-delay: 4s;"></span>
<span style="left: 60%; width: 35px; height: 35px; animation-delay: 2s; animation-duration: 22s;"></span>
<span style="left: 70%; width: 15px; height: 15px; animation-delay: 1s;"></span>
<span style="left: 80%; width: 25px; height: 25px; animation-delay: 3s; animation-duration: 19s;"></span>
<span style="left: 90%; width: 20px; height: 20px; animation-delay: 0s;"></span>
</div>
<div class="game-container">
<h1>🐍 精美贪吃蛇</h1>
<div class="game-info">
<div class="score">
得分: <span id="score">0</span>
</div>
<div class="speed-control">
<label>速度:</label>
<select id="speedSelect">
<option value="200">慢速</option>
<option value="150" selected>正常</option>
<option value="100">快速</option>
<option value="50">极速</option>
</select>
</div>
<div class="controls">
<button id="startBtn">开始游戏</button>
<button id="pauseBtn">暂停</button>
<button id="resetBtn">重置</button>
</div>
</div>
<canvas id="gameCanvas" width="600" height="600"></canvas>
<div class="game-over" id="gameOver">
<h2>游戏结束!</h2>
<p>最终得分: <span id="finalScore">0</span></p>
<button onclick="resetGame()">重新开始</button>
</div>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const finalScoreElement = document.getElementById('finalScore');
const gameOverElement = document.getElementById('gameOver');
const startBtn = document.getElementById('startBtn');
const pauseBtn = document.getElementById('pauseBtn');
const resetBtn = document.getElementById('resetBtn');
const speedSelect = document.getElementById('speedSelect');
// 游戏配置
const gridSize = 20;
const cellSize = canvas.width / gridSize;
let snake = [{x: 10, y: 10}];
let direction = {x: 0, y: 0};
let food = generateFood();
let score = 0;
let gameRunning = false;
let gamePaused = false;
let gameSpeed = 150;
// 粒子效果数组
let particles = [];
// 食物动画
let foodPulse = 0;
// 生成食物
function generateFood() {
let newFood;
do {
newFood = {
x: Math.floor(Math.random() * gridSize),
y: Math.floor(Math.random() * gridSize)
};
} while (snake.some(segment => segment.x === newFood.x && segment.y === newFood.y));
return newFood;
}
// 创建粒子效果
function createParticles(x, y) {
for (let i = 0; i < 15; i++) {
particles.push({
x: x * cellSize + cellSize / 2,
y: y * cellSize + cellSize / 2,
vx: (Math.random() - 0.5) * 8,
vy: (Math.random() - 0.5) * 8,
life: 1.0,
color: `hsl(${Math.random() * 60 + 15}, 100%, 50%)`
});
}
}
// 更新粒子
function updateParticles() {
particles = particles.filter(particle => {
particle.x += particle.vx;
particle.y += particle.vy;
particle.life -= 0.02;
particle.vx *= 0.98;
particle.vy *= 0.98;
return particle.life > 0;
});
}
// 绘制粒子
function drawParticles() {
particles.forEach(particle => {
ctx.globalAlpha = particle.life;
ctx.fillStyle = particle.color;
ctx.shadowBlur = 10;
ctx.shadowColor = particle.color;
ctx.beginPath();
ctx.arc(particle.x, particle.y, 3, 0, Math.PI * 2);
ctx.fill();
});
ctx.globalAlpha = 1;
ctx.shadowBlur = 0;
}
// 绘制圆角矩形
function drawRoundRect(x, y, width, height, radius) {
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.arcTo(x + width, y, x + width, y + height, radius);
ctx.arcTo(x + width, y + height, x, y + height, radius);
ctx.arcTo(x, y + height, x, y, radius);
ctx.arcTo(x, y, x + width, y, radius);
ctx.closePath();
}
// 绘制游戏画面
function draw() {
// 清空画布
ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 更新食物脉冲动画
foodPulse += 0.1;
// 绘制发光网格
ctx.strokeStyle = 'rgba(255, 255, 255, 0.05)';
ctx.lineWidth = 1;
ctx.shadowBlur = 2;
ctx.shadowColor = 'rgba(255, 255, 255, 0.5)';
for (let i = 0; i <= gridSize; i++) {
ctx.beginPath();
ctx.moveTo(i * cellSize, 0);
ctx.lineTo(i * cellSize, canvas.height);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, i * cellSize);
ctx.lineTo(canvas.width, i * cellSize);
ctx.stroke();
}
ctx.shadowBlur = 0;
// 绘制精美的蛇
snake.forEach((segment, index) => {
const x = segment.x * cellSize;
const y = segment.y * cellSize;
// 计算颜色渐变
const hue = (index / snake.length) * 30 + 140;
const lightness = 50 + (index / snake.length) * 10;
// 绘制蛇身阴影
ctx.shadowColor = 'rgba(0, 0, 0, 0.5)';
ctx.shadowBlur = 10;
ctx.shadowOffsetX = 3;
ctx.shadowOffsetY = 3;
// 蛇身主体
const gradient = ctx.createRadialGradient(
x + cellSize/2, y + cellSize/2, 0,
x + cellSize/2, y + cellSize/2, cellSize/2
);
gradient.addColorStop(0, `hsl(${hue}, 70%, ${lightness}%)`);
gradient.addColorStop(1, `hsl(${hue}, 70%, ${lightness - 10}%)`);
ctx.fillStyle = gradient;
drawRoundRect(x + 2, y + 2, cellSize - 4, cellSize - 4, 8);
ctx.fill();
// 蛇身高光
ctx.shadowBlur = 0;
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 0;
const highlightGradient = ctx.createLinearGradient(x, y, x, y + cellSize);
highlightGradient.addColorStop(0, 'rgba(255, 255, 255, 0.4)');
highlightGradient.addColorStop(0.5, 'rgba(255, 255, 255, 0.1)');
highlightGradient.addColorStop(1, 'rgba(255, 255, 255, 0)');
ctx.fillStyle = highlightGradient;
drawRoundRect(x + 4, y + 4, cellSize - 8, cellSize/2 - 4, 6);
ctx.fill();
// 绘制蛇头
if (index === 0) {
// 眼睛外框
ctx.fillStyle = 'rgba(255, 255, 255, 0.9)';
ctx.shadowColor = 'rgba(255, 255, 255, 0.8)';
ctx.shadowBlur = 5;
let eye1X, eye1Y, eye2X, eye2Y;
if (direction.x === 1) { // 向右
eye1X = x + cellSize - 10;
eye1Y = y + 8;
eye2X = x + cellSize - 10;
eye2Y = y + cellSize - 12;
} else if (direction.x === -1) { // 向左
eye1X = x + 10;
eye1Y = y + 8;
eye2X = x + 10;
eye2Y = y + cellSize - 12;
} else if (direction.y === -1) { // 向上
eye1X = x + 8;
eye1Y = y + 10;
eye2X = x + cellSize - 12;
eye2Y = y + 10;
} else if (direction.y === 1) { // 向下
eye1X = x + 8;
eye1Y = y + cellSize - 10;
eye2X = x + cellSize - 12;
eye2Y = y + cellSize - 10;
} else { // 静止
eye1X = x + cellSize - 10;
eye1Y = y + 8;
eye2X = x + cellSize - 10;
eye2Y = y + cellSize - 12;
}
// 绘制眼睛
ctx.beginPath();
ctx.arc(eye1X, eye1Y, 4, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.arc(eye2X, eye2Y, 4, 0, Math.PI * 2);
ctx.fill();
// 瞳孔
ctx.fillStyle = '#000';
ctx.shadowBlur = 0;
ctx.beginPath();
ctx.arc(eye1X, eye1Y, 2, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.arc(eye2X, eye2Y, 2, 0, Math.PI * 2);
ctx.fill();
// 舌头(偶尔出现)
if (Math.random() > 0.95) {
ctx.strokeStyle = '#ff1744';
ctx.lineWidth = 2;
ctx.beginPath();
if (direction.x === 1) {
ctx.moveTo(x + cellSize, y + cellSize/2);
ctx.lineTo(x + cellSize + 8, y + cellSize/2 - 2);
ctx.moveTo(x + cellSize, y + cellSize/2);
ctx.lineTo(x + cellSize + 8, y + cellSize/2 + 2);
}
ctx.stroke();
}
}
});
// 绘制精美的食物
const foodX = food.x * cellSize + cellSize / 2;
const foodY = food.y * cellSize + cellSize / 2;
const pulseSize = Math.sin(foodPulse) * 2 + cellSize / 2 - 4;
// 食物发光效果
ctx.shadowColor = '#ff6b6b';
ctx.shadowBlur = 20 + Math.sin(foodPulse) * 10;
// 食物主体(渐变效果)
const foodGradient = ctx.createRadialGradient(foodX, foodY, 0, foodX, foodY, pulseSize);
foodGradient.addColorStop(0, '#ff8a80');
foodGradient.addColorStop(0.5, '#ff5252');
foodGradient.addColorStop(1, '#d32f2f');
ctx.fillStyle = foodGradient;
ctx.beginPath();
ctx.arc(foodX, foodY, pulseSize, 0, Math.PI * 2);
ctx.fill();
// 食物高光
ctx.shadowBlur = 0;
const highlightGradient = ctx.createRadialGradient(
foodX - pulseSize/3, foodY - pulseSize/3, 0,
foodX - pulseSize/3, foodY - pulseSize/3, pulseSize/2
);
highlightGradient.addColorStop(0, 'rgba(255, 255, 255, 0.8)');
highlightGradient.addColorStop(1, 'rgba(255, 255, 255, 0)');
ctx.fillStyle = highlightGradient;
ctx.beginPath();
ctx.arc(foodX - pulseSize/3, foodY - pulseSize/3, pulseSize/2, 0, Math.PI * 2);
ctx.fill();
// 绘制食物周围的光环
ctx.strokeStyle = 'rgba(255, 107, 107, 0.3)';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.arc(foodX, foodY, pulseSize + 10 + Math.sin(foodPulse * 2) * 5, 0, Math.PI * 2);
ctx.stroke();
// 更新和绘制粒子
updateParticles();
drawParticles();
}
// 更新游戏状态
function update() {
if (!gameRunning || gamePaused) return;
// 移动蛇头
const head = {x: snake[0].x + direction.x, y: snake[0].y + direction.y};
// 检查是否撞墙
if (head.x < 0 || head.x >= gridSize || head.y < 0 || head.y >= gridSize) {
gameOver();
return;
}
// 检查是否撞到自己
if (snake.some(segment => segment.x === head.x && segment.y === head.y)) {
gameOver();
return;
}
snake.unshift(head);
// 检查是否吃到食物
if (head.x === food.x && head.y === food.y) {
score += 10;
scoreElement.textContent = score;
createParticles(food.x, food.y);
food = generateFood();
} else {
snake.pop();
}
}
// 游戏主循环
function gameLoop() {
update();
draw();
setTimeout(gameLoop, gameSpeed);
}
// 键盘控制
document.addEventListener('keydown', (e) => {
if (!gameRunning || gamePaused) return;
switch(e.key) {
case 'ArrowUp':
case 'w':
case 'W':
if (direction.y === 0) {
direction = {x: 0, y: -1};
}
break;
case 'ArrowDown':
case 's':
case 'S':
if (direction.y === 0) {
direction = {x: 0, y: 1};
}
break;
case 'ArrowLeft':
case 'a':
case 'A':
if (direction.x === 0) {
direction = {x: -1, y: 0};
}
break;
case 'ArrowRight':
case 'd':
case 'D':
if (direction.x === 0) {
direction = {x: 1, y: 0};
}
break;
}
});
// 游戏结束
function gameOver() {
gameRunning = false;
finalScoreElement.textContent = score;
gameOverElement.style.display = 'block';
}
// 开始游戏
function startGame() {
if (!gameRunning) {
gameRunning = true;
gamePaused = false;
direction = {x: 1, y: 0};
gameLoop();
startBtn.textContent = '游戏中';
startBtn.disabled = true;
}
}
// 暂停游戏
function pauseGame() {
if (gameRunning) {
gamePaused = !gamePaused;
pauseBtn.textContent = gamePaused ? '继续' : '暂停';
}
}
// 重置游戏
function resetGame() {
gameRunning = false;
gamePaused = false;
snake = [{x: 10, y: 10}];
direction = {x: 0, y: 0};
food = generateFood();
score = 0;
particles = [];
scoreElement.textContent = score;
gameOverElement.style.display = 'none';
startBtn.textContent = '开始游戏';
startBtn.disabled = false;
pauseBtn.textContent = '暂停';
draw();
}
// 速度控制
speedSelect.addEventListener('change', (e) => {
gameSpeed = parseInt(e.target.value);
});
// 按钮事件
startBtn.addEventListener('click', startGame);
pauseBtn.addEventListener('click', pauseGame);
resetBtn.addEventListener('click', resetGame);
// 初始化
draw();
</script>
</body>
</html>
我大幅提升了贪吃蛇的视觉效果,主要改进包括:
🐍 蛇的精美升级:
🍎 食物的视觉增强:
✨ 特效系统:
🎨 整体美化:
这个升级版的贪吃蛇游戏视觉效果更加精美,给玩家带来更好的游戏体验!