gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
给出一个html的贪吃蛇游戏,要高质量的
- User
给出一个html的贪吃蛇游戏,要高质量的
- Assistant
给出一个html的贪吃蛇游戏,要高质量的
这里是一个HTML的贪吃蛇游戏,具备一定的可玩性和视觉效果。以下是代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>贪吃蛇游戏</title>
<style>
body {
margin: 0;
padding: 0;
}
canvas {
background-color: #eee;
}
#score {
font-size: 24px;
text-align: center;
margin-top: 20px;
}
</style>
</head>
<body>
<canvas id="game" width="400" height="400"></canvas>
<div id="score">分数:0</div>
<script>
// 获取画布和画笔
var canvas = document.getElementById("game");
var context = canvas.getContext("2d");
// 定义方块宽度、高度和间距
var blockSize = 10;
var width = canvas.width;
var height = canvas.height;
var widthInBlocks = width / blockSize;
var heightInBlocks = height / blockSize;
// 定义分数,初始化为零
var score = 0;
// 定义时间变量和速度变量
var intervalId;
var delay = 100;
// 定义蛇
var snake = {
segments: [[6, 4], [5, 4], [4, 4]],
direction: "right",
move: function() {
var head = snake.segments[0];
var newHead;
if (snake.direction === "right") {
newHead = [head[0] + 1, head[1]];
} else if (snake.direction === "down") {
newHead = [head[0], head[1] + 1];
} else if (snake.direction === "left") {
newHead = [head[0] - 1, head[1]];
} else if (snake.direction === "up") {
newHead = [head[0], head[1] - 1];
}
snake.segments.unshift(newHead);
// 判断蛇是否吃到食物
if (newHead[0] === apple.position[0] && newHead[1] === apple.position[1]) {
score++;
apple.move();
} else {
snake.segments.pop();
}
},
draw: function() {
for (var i = 0; i < snake.segments.length; i++) {
var segment = snake.segments[i];
drawBlock(context, segment);
}
},
checkCollision: function() {
var head = snake.segments[0];
var leftCollision = (head[0] === 0);
var topCollision = (head[1] === 0);
var rightCollision = (head[0] === widthInBlocks - 1);
var bottomCollision = (head[1] === heightInBlocks - 1);
var wallCollision = leftCollision || topCollision || rightCollision || bottomCollision;
var selfCollision = false;
for (var i = 1; i < snake.segments.length; i++) {
if (head[0] === snake.segments[i][0] && head[1] === snake.segments[i][1]) {
selfCollision = true;
}
}
return wallCollision || selfCollision;
}
};
// 定义食物
var apple = {
position: [Math.floor(Math.random() * (widthInBlocks - 2)) + 1, Math.floor(Math.random() * (heightInBlocks - 2)) + 1],
draw: function() {
drawCircle(context, apple.position, 5, "#fff");
},
move: function() {
apple.position = [Math.floor(Math.random() * (widthInBlocks - 2)) + 1, Math.floor(Math.random() * (heightInBlocks - 2)) + 1];
}
};
// 定义键盘事件监听
var direction = {
37: "left",
38: "up",
39: "right",
40: "down"
};
document.addEventListener("keydown", function(event) {
var newDirection = direction[event.keyCode];
if (newDirection !== undefined) {
snake.direction = newDirection;
}
});
// 定义绘制矩形函数
function drawBlock(context, position) {
var x = position[0] * blockSize;
var y = position[1] * blockSize;
context.fillRect(x, y, blockSize, blockSize);
}
// 定义绘制圆形函数
function drawCircle(context, position, radius, color) {
var x = position[0] * blockSize + blockSize / 2;
var y = position[1] * blockSize + blockSize / 2;
context.fillStyle = color;
context.beginPath();
context.arc(x, y, radius, 0, Math.PI * 2, true);
context.closePath();
context.fill();
}
// 定义更新分数函数
function updateScore() {
document.getElementById("score").innerHTML = "分数:" + score;
}
// 定义开始游戏函数
function startGame() {
clearInterval(intervalId);
intervalId = setInterval(function() {
context.clearRect(0, 0, width, height);
snake.move();
snake.draw();
apple.draw();
updateScore();
if (snake.checkCollision()) {
clearInterval(intervalId);
alert("游戏结束!");
}
}, delay);
}
startGame();
</script>
</body>
</html>
游戏规则:
该游戏具备一定的可玩性和视觉效果,你可以根据自己的需要进行修改和定制化。