写一个手机版的网页贪吃蛇小游戏
- 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.
以下是一个简单的手机版贪吃蛇小游戏的实现代码,使用HTML5 Canvas和JavaScript开发,适配移动端触屏操作:
<!DOCTYPE html>
<html>
<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 {
background: #111;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
overflow: hidden;
font-family: Arial, sans-serif;
}
#game-container {
position: relative;
width: 90vw;
height: 90vw;
max-width: 400px;
max-height: 400px;
}
canvas {
width: 100%;
height: 100%;
background: #000;
display: block;
border: 2px solid #fff;
}
#controls {
position: absolute;
bottom: 0;
width: 100%;
display: flex;
justify-content: center;
gap: 20px;
padding: 10px;
background: rgba(0,0,0,0.5);
}
.btn {
width: 60px;
height: 60px;
border: 2px solid #fff;
border-radius: 50%;
background: #333;
color: #fff;
font-size: 24px;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
transition: background 0.2s;
}
.btn:hover {
background: #555;
}
</style>
</head>
<body>
<div id="game-container">
<canvas id="gameCanvas"></canvas>
<div id="controls">
<div class="btn" onclick="changeDirection('up')">▲</div>
<div class="btn" onclick="changeDirection('left')">◀</div>
<div class="btn" onclick="changeDirection('down')">▼</div>
<div class="btn" onclick="changeDirection('right')">▶</div>
</div>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// 设置画布尺寸
function resizeCanvas() {
const size = Math.min(window.innerWidth * 0.9, window.innerHeight * 0.9);
canvas.width = size;
canvas.height = size;
}
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
const gridSize = 20;
const tileCount = canvas.width / gridSize;
let snake = [{x: 10, y: 10}];
let direction = {x: 0, y: 0};
let food = getRandomFoodPosition();
let score = 0;
let gameInterval;
function getRandomFoodPosition() {
return {
x: Math.floor(Math.random() * tileCount),
y: Math.floor(Math.random() * tileCount)
};
}
function changeDirection(newDirection) {
// 防止反向移动
if (newDirection === 'up' && direction.y === 0) {
direction = {x: 0, y: -1};
} else if (newDirection === 'down' && direction.y === 0) {
direction = {x: 0, y: 1};
} else if (newDirection === 'left' && direction.x === 0) {
direction = {x: -1, y: 0};
} else if (newDirection === 'right' && direction.x === 0) {
direction = {x: 1, y: 0};
}
}
function update() {
const head = {x: snake[0].x + direction.x, y: snake[0].y + direction.y};
// 碰撞检测
if (
head.x < 0 || head.x >= tileCount ||
head.y < 0 || head.y >= tileCount ||
snake.some(segment => segment.x === head.x && segment.y === head.y)
) {
clearInterval(gameInterval);
alert(`游戏结束!得分:${score}`);
return;
}
snake.unshift(head);
// 吃到食物
if (head.x === food.x && head.y === food.y) {
score++;
food = getRandomFoodPosition();
} else {
snake.pop();
}
}
function draw() {
// 清空画布
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 绘制食物
ctx.fillStyle = 'red';
ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize, gridSize);
// 绘制蛇
ctx.fillStyle = 'lime';
snake.forEach(segment => {
ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize, gridSize);
});
// 绘制得分
ctx.fillStyle = '#fff';
ctx.font = '16px Arial';
ctx.fillText(`得分: ${score}`, 10, 20);
}
function gameLoop() {
update();
draw();
}
// 启动游戏
gameInterval = setInterval(gameLoop, 150);
</script>
</body>
</html>
这个游戏在手机上运行时会自动适配屏幕尺寸,方向按钮位于屏幕底部方便触摸操作。你可以根据需要进一步优化样式和增加功能。