使用DeepSeek生成基于HTML5的贪吃蛇游戏,效果非常棒,界面简约,操作简便。
提示词
帮我生成一个贪吃蛇游戏 基于HTML5
游戏说明
1 游戏控制:
使用键盘方向键(↑ ↓ ← →)控制蛇的移动方向
点击"开始游戏"按钮开始新游戏
点击"暂停"按钮暂停/继续游戏
2 游戏规则:
控制蛇吃掉红色食物来增长身体和增加分数
撞到墙壁或自己的身体会导致游戏结束
蛇不能直接反向移动(例如不能从左直接转向右)
3 自定义修改:
可以修改gridSize变量来改变网格大小
可以修改setInterval(game, 100)中的100来调整游戏速度(数字越小速度越快)
可以修改颜色、画布大小等样式
将这段代码复制到一个HTML文件中,用浏览器打开即可开始游戏。
贪吃蛇全部源码如下
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>贪吃蛇游戏</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
canvas {
border: 2px solid #333;
background-color: #fff;
}
.score {
font-size: 24px;
margin-bottom: 10px;
}
.controls {
margin-top: 20px;
text-align: center;
}
button {
padding: 8px 16px;
margin: 0 5px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>贪吃蛇游戏</h1>
<div class="score">得分: <span id="score">0</span></div>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<div class="controls">
<button id="startBtn">开始游戏</button>
<button id="pauseBtn">暂停</button>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const startBtn = document.getElementById('startBtn');
const pauseBtn = document.getElementById('pauseBtn');
const gridSize = 20;
const tileCount = canvas.width / gridSize;
let snake = [];
let food = {};
let score = 0;
let velocityX = 0;
let velocityY = 0;
let gameLoop;
let isPaused = false;
let isGameRunning = false;
// 初始化游戏
function initGame() {
snake = [
{x: 10, y: 10}
];
score = 0;
velocityX = 0;
velocityY = 0;
scoreElement.textContent = score;
generateFood();
}
// 生成食物
function generateFood() {
food = {
x: Math.floor(Math.random() * tileCount),
y: Math.floor(Math.random() * tileCount)
};
// 确保食物不会出现在蛇身上
for (let segment of snake) {
if (segment.x === food.x && segment.y === food.y) {
return generateFood();
}
}
}
// 游戏主循环
function game() {
if (isPaused) return;
// 清空画布
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 移动蛇
const head = {x: snake[0].x + velocityX, y: snake[0].y + velocityY};
snake.unshift(head);
// 检查是否吃到食物
if (head.x === food.x && head.y === food.y) {
score++;
scoreElement.textContent = score;
generateFood();
} else {
snake.pop();
}
// 检查碰撞
if (
head.x < 0 || head.x >= tileCount ||
head.y < 0 || head.y >= tileCount ||
checkSelfCollision()
) {
gameOver();
return;
}
// 绘制食物
ctx.fillStyle = 'red';
ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize, gridSize);
// 绘制蛇
ctx.fillStyle = 'green';
for (let segment of snake) {
ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize, gridSize);
// 绘制蛇身边框
ctx.strokeStyle = 'darkgreen';
ctx.strokeRect(segment.x * gridSize, segment.y * gridSize, gridSize, gridSize);
}
// 绘制蛇头(不同颜色)
ctx.fillStyle = 'darkgreen';
ctx.fillRect(snake[0].x * gridSize, snake[0].y * gridSize, gridSize, gridSize);
}
// 检查蛇是否撞到自己
function checkSelfCollision() {
for (let i = 1; i < snake.length; i++) {
if (snake[i].x === snake[0].x && snake[i].y === snake[0].y) {
return true;
}
}
return false;
}
// 游戏结束
function gameOver() {
clearInterval(gameLoop);
isGameRunning = false;
alert(`游戏结束!你的得分是: ${score}`);
}
// 键盘控制
document.addEventListener('keydown', (e) => {
if (!isGameRunning) return;
switch(e.key) {
case 'ArrowUp':
if (velocityY !== 1) {
velocityX = 0;
velocityY = -1;
}
break;
case 'ArrowDown':
if (velocityY !== -1) {
velocityX = 0;
velocityY = 1;
}
break;
case 'ArrowLeft':
if (velocityX !== 1) {
velocityX = -1;
velocityY = 0;
}
break;
case 'ArrowRight':
if (velocityX !== -1) {
velocityX = 1;
velocityY = 0;
}
break;
}
});
// 开始游戏按钮
startBtn.addEventListener('click', () => {
if (isGameRunning) return;
initGame();
isGameRunning = true;
isPaused = false;
gameLoop = setInterval(game, 100);
});
// 暂停游戏按钮
pauseBtn.addEventListener('click', () => {
if (!isGameRunning) return;
isPaused = !isPaused;
pauseBtn.textContent = isPaused ? '继续' : '暂停';
});
});
</script>
</body>
</html>