写一个手机版的网页贪吃蛇小游戏
- 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.
我来为你创建一个适合手机的贪吃蛇网页游戏:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title>贪吃蛇</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
min-height: 100vh;
padding: 10px;
}
.game-container {
background: white;
border-radius: 15px;
box-shadow: 0 10px 30px rgba(0,0,0,0.3);
padding: 20px;
max-width: 400px;
width: 100%;
}
.score-board {
text-align: center;
margin-bottom: 15px;
}
.score {
font-size: 24px;
font-weight: bold;
color: #333;
}
.game-board {
width: 100%;
max-width: 350px;
height: 350px;
border: 3px solid #333;
border-radius: 10px;
margin: 0 auto;
background: #f0f0f0;
position: relative;
overflow: hidden;
}
.controls {
margin-top: 20px;
text-align: center;
}
.control-btn {
background: #667eea;
color: white;
border: none;
padding: 12px 24px;
font-size: 16px;
border-radius: 25px;
cursor: pointer;
margin: 5px;
transition: background 0.3s;
}
.control-btn:hover {
background: #5a6fd8;
}
.control-btn:disabled {
background: #ccc;
cursor: not-allowed;
}
.instructions {
text-align: center;
margin-top: 15px;
color: #666;
font-size: 14px;
}
.game-over {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(0,0,0,0.8);
color: white;
padding: 20px;
border-radius: 10px;
text-align: center;
display: none;
}
.snake-segment {
position: absolute;
background: #4CAF50;
border-radius: 2px;
}
.snake-head {
background: #2E7D32;
}
.food {
position: absolute;
background: #FF5722;
border-radius: 50%;
}
</style>
</head>
<body>
<div class="game-container">
<div class="score-board">
<div class="score">得分: <span id="score">0</span></div>
</div>
<div class="game-board" id="gameBoard">
<div class="game-over" id="gameOver">
<h3>游戏结束!</h3>
<p>最终得分: <span id="finalScore">0</span></p>
<button class="control-btn" onclick="startGame()">重新开始</button>
</div>
</div>
<div class="controls">
<button class="control-btn" id="startBtn" onclick="startGame()">开始游戏</button>
<button class="control-btn" id="pauseBtn" onclick="pauseGame()" disabled>暂停</button>
</div>
<div class="instructions">
在屏幕上滑动来控制蛇的方向<br>
吃到红色食物来增长身体
</div>
</div>
<script>
class SnakeGame {
constructor() {
this.board = document.getElementById('gameBoard');
this.scoreElement = document.getElementById('score');
this.gameOverElement = document.getElementById('gameOver');
this.finalScoreElement = document.getElementById('finalScore');
this.startBtn = document.getElementById('startBtn');
this.pauseBtn = document.getElementById('pauseBtn');
this.boardSize = 20; // 20x20 格子
this.cellSize = 0;
this.snake = [];
this.food = {};
this.direction = { x: 1, y: 0 };
this.score = 0;
this.gameRunning = false;
this.gameLoop = null;
this.initializeBoard();
this.setupTouchControls();
}
initializeBoard() {
const boardRect = this.board.getBoundingClientRect();
this.cellSize = Math.floor(Math.min(boardRect.width, boardRect.height) / this.boardSize);
}
setupTouchControls() {
let startX, startY;
this.board.addEventListener('touchstart', (e) => {
e.preventDefault();
const touch = e.touches[0];
startX = touch.clientX;
startY = touch.clientY;
});
this.board.addEventListener('touchend', (e) => {
e.preventDefault();
if (!this.gameRunning) return;
const touch = e.changedTouches[0];
const deltaX = touch.clientX - startX;
const deltaY = touch.clientY - startY;
const minSwipeDistance = 30;
if (Math.abs(deltaX) > Math.abs(deltaY)) {
if (Math.abs(deltaX) > minSwipeDistance) {
if (deltaX > 0 && this.direction.x !== -1) {
this.direction = { x: 1, y: 0 }; // 右
} else if (deltaX < 0 && this.direction.x !== 1) {
this.direction = { x: -1, y: 0 }; // 左
}
}
} else {
if (Math.abs(deltaY) > minSwipeDistance) {
if (deltaY > 0 && this.direction.y !== -1) {
this.direction = { x: 0, y: 1 }; // 下
} else if (deltaY < 0 && this.direction.y !== 1) {
this.direction = { x: 0, y: -1 }; // 上
}
}
}
});
// 键盘控制(用于调试)
document.addEventListener('keydown', (e) => {
if (!this.gameRunning) return;
switch(e.key) {
case 'ArrowUp':
if (this.direction.y !== 1) this.direction = { x: 0, y: -1 };
break;
case 'ArrowDown':
if (this.direction.y !== -1) this.direction = { x: 0, y: 1 };
break;
case 'ArrowLeft':
if (this.direction.x !== 1) this.direction = { x: -1, y: 0 };
break;
case 'ArrowRight':
if (this.direction.x !== -1) this.direction = { x: 1, y: 0 };
break;
}
});
}
start() {
this.initializeBoard();
this.snake = [
{ x: 10, y: 10 },
{ x: 9, y: 10 },
{ x: 8, y: 10 }
];
this.direction = { x: 1, y: 0 };
this.score = 0;
this.gameRunning = true;
this.generateFood();
this.updateScore();
this.render();
this.gameLoop = setInterval(() => {
this.update();
this.render();
}, 150);
this.startBtn.disabled = true;
this.pauseBtn.disabled = false;
this.gameOverElement.style.display = 'none';
}
pause() {
if (this.gameRunning) {
clearInterval(this.gameLoop);
this.gameRunning = false;
this.startBtn.disabled = false;
this.pauseBtn.disabled = true;
this.startBtn.textContent = '继续游戏';
}
}
update() {
if (!this.gameRunning) return;
const head = { ...this.snake[0] };
head.x += this.direction.x;
head.y += this.direction.y;
// 检查撞墙
if (head.x < 0 || head.x >= this.boardSize ||
head.y < 0 || head.y >= this.boardSize) {
this.gameOver();
return;
}
// 检查撞到自己
if (this.snake.some(segment => segment.x === head.x && segment.y === head.y)) {
this.gameOver();
return;
}
this.snake.unshift(head);
// 检查是否吃到食物
if (head.x === this.food.x && head.y === this.food.y) {
this.score += 10;
this.updateScore();
this.generateFood();
} else {
this.snake.pop();
}
}
generateFood() {
do {
this.food = {
x: Math.floor(Math.random() * this.boardSize),
y: Math.floor(Math.random() * this.boardSize)
};
} while (this.snake.some(segment =>
segment.x === this.food.x && segment.y === this.food.y
));
}
render() {
// 清除画板
this.board.querySelectorAll('.snake-segment, .food').forEach(el => el.remove());
// 绘制蛇
this.snake.forEach((segment, index) => {
const segmentElement = document.createElement('div');
segmentElement.className = index === 0 ? 'snake-segment snake-head' : 'snake-segment';
segmentElement.style.left = segment.x * this.cellSize + 'px';
segmentElement.style.top = segment.y * this.cellSize + 'px';
segmentElement.style.width = this.cellSize - 1 + 'px';
segmentElement.style.height = this.cellSize - 1 + 'px';
this.board.appendChild(segmentElement);
});
// 绘制食物
const foodElement = document.createElement('div');
foodElement.className = 'food';
foodElement.style.left = this.food.x * this.cellSize + 'px';
foodElement.style.top = this.food.y * this.cellSize + 'px';
foodElement.style.width = this.cellSize - 1 + 'px';
foodElement.style.height = this.cellSize - 1 + 'px';
this.board.appendChild(foodElement);
}
updateScore() {
this.scoreElement.textContent = this.score;
}
gameOver() {
this.gameRunning = false;
clearInterval(this.gameLoop);
this.finalScoreElement.textContent = this.score;
this.gameOverElement.style.display = 'block';
this.startBtn.disabled = false;
this.pauseBtn.disabled = true;
this.startBtn.textContent = '开始游戏';
}
}
const game = new SnakeGame();
function startGame() {
game.start();
}
function pauseGame() {
game.pause();
}
// 防止页面滚动
document.addEventListener('touchmove', (e) => {
if (e.target.closest('.game-board')) {
e.preventDefault();
}
}, { passive: false });
</script>
</body>
</html>
这个贪吃蛇游戏具有以下特点:
移动端优化:
游戏功能:
用户体验:
操作方式:
直接将这段代码保存为HTML文件,用手机浏览器打开就能游玩了!