你好,给出贪吃蛇代码
- 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实现:
<!DOCTYPE html>
<html>
<head>
<title>贪吃蛇游戏</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #333;
font-family: Arial, sans-serif;
}
#gameContainer {
text-align: center;
}
#gameCanvas {
border: 2px solid #fff;
background-color: #111;
}
#score {
color: #fff;
font-size: 20px;
margin-top: 10px;
}
#gameOver {
color: #ff0000;
font-size: 24px;
margin-top: 10px;
display: none;
}
</style>
</head>
<body>
<div id="gameContainer">
<canvas id="gameCanvas" width="400" height="400"></canvas>
<div id="score">得分: 0</div>
<div id="gameOver">游戏结束!按空格键重新开始</div>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const gameOverElement = document.getElementById('gameOver');
// 游戏设置
const gridSize = 20;
const tileCount = canvas.width / gridSize;
let snake = [
{x: 10, y: 10}
];
let dx = 0;
let dy = 0;
let foodX = 15;
let foodY = 15;
let score = 0;
let gameRunning = true;
// 游戏主循环
function gameLoop() {
if (gameRunning) {
clearCanvas();
moveSnake();
checkCollision();
drawFood();
drawSnake();
setTimeout(gameLoop, 100);
}
}
// 清空画布
function clearCanvas() {
ctx.fillStyle = '#111';
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
// 绘制蛇
function drawSnake() {
ctx.fillStyle = '#0f0';
snake.forEach(segment => {
ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize - 2, gridSize - 2);
});
}
// 移动蛇
function moveSnake() {
const head = {x: snake[0].x + dx, y: snake[0].y + dy};
snake.unshift(head);
// 检查是否吃到食物
if (head.x === foodX && head.y === foodY) {
score++;
scoreElement.textContent = `得分: ${score}`;
generateFood();
} else {
snake.pop();
}
}
// 生成食物
function generateFood() {
foodX = Math.floor(Math.random() * tileCount);
foodY = Math.floor(Math.random() * tileCount);
// 确保食物不在蛇身上
snake.forEach(segment => {
if (segment.x === foodX && segment.y === foodY) {
generateFood();
}
});
}
// 绘制食物
function drawFood() {
ctx.fillStyle = '#f00';
ctx.fillRect(foodX * gridSize, foodY * gridSize, gridSize - 2, gridSize - 2);
}
// 检查碰撞
function checkCollision() {
const head = snake[0];
// 撞墙检测
if (head.x < 0 || head.x >= tileCount || head.y < 0 || head.y >= tileCount) {
gameOver();
}
// 撞自己检测
for (let i = 1; i < snake.length; i++) {
if (head.x === snake[i].x && head.y === snake[i].y) {
gameOver();
}
}
}
// 游戏结束
function gameOver() {
gameRunning = false;
gameOverElement.style.display = 'block';
}
// 重新开始游戏
function resetGame() {
snake = [{x: 10, y: 10}];
dx = 0;
dy = 0;
score = 0;
scoreElement.textContent = `得分: ${score}`;
generateFood();
gameRunning = true;
gameOverElement.style.display = 'none';
gameLoop();
}
// 键盘控制
document.addEventListener('keydown', (e) => {
if (!gameRunning && e.key === ' ') {
resetGame();
return;
}
if (!gameRunning) return;
switch(e.key) {
case 'ArrowUp':
if (dy === 0) {
dx = 0;
dy = -1;
}
break;
case 'ArrowDown':
if (dy === 0) {
dx = 0;
dy = 1;
}
break;
case 'ArrowLeft':
if (dx === 0) {
dx = -1;
dy = 0;
}
break;
case 'ArrowRight':
if (dx === 0) {
dx = 1;
dy = 0;
}
break;
}
});
// 开始游戏
gameLoop();
</script>
</body>
</html>
这个贪吃蛇游戏包含以下功能:
将代码保存为HTML文件,在浏览器中打开即可游玩。游戏使用方向键控制蛇的移动方向,吃到红色食物会增加分数和蛇的长度。