给我写一个用js做贪吃蛇游戏的代码
时间: 2023-05-23 21:04:50 浏览: 166
好的,以下是使用JavaScript编写的贪吃蛇游戏代码:
HTML:
```
<!DOCTYPE html>
<html>
<head>
<title>贪吃蛇游戏</title>
<style type="text/css">
canvas {
border: 1px solid black;
background-color: #f1f1f1;
}
</style>
</head>
<body>
<canvas id="canvas" width="400" height="400"></canvas>
</body>
<script type="text/javascript" src="snake.js"></script>
</html>
```
JS:
```
// 贪吃蛇游戏
window.onload = function() {
// 获取画布对象
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
// 设置每个方块的大小
var blockSize = 10;
// 设置画布的宽度和高度
var width = canvas.width;
var height = canvas.height;
// 设置画布边框的厚度
var borderThickness = 1;
// 设置得分、速度和游戏状态等变量
var score = 0;
var speed = 100;
var gameOver = false;
// 定义贪吃蛇对象
var snake = {
x: 0,
y: 0,
dx: 10,
dy: 0,
tail: [],
length: 4
};
// 定义食物对象
var food = {
x: Math.floor(Math.random() * ((width / blockSize) - 1)) * blockSize,
y: Math.floor(Math.random() * ((height / blockSize) - 1)) * blockSize
};
// 游戏循环函数
function gameLoop() {
// 清空画布
ctx.clearRect(0, 0, width, height);
// 渲染贪吃蛇
ctx.fillStyle = "#000000";
for (var i = 0; i < snake.tail.length; i++) {
ctx.fillRect(snake.tail[i].x, snake.tail[i].y, blockSize, blockSize);
// 如果蛇头撞到身体则游戏结束
if (snake.tail[i].x == snake.x && snake.tail[i].y == snake.y) {
gameOver = true;
}
}
// 渲染食物
ctx.fillStyle = "#FF0000";
ctx.fillRect(food.x, food.y, blockSize, blockSize);
// 更新蛇的位置
snake.tail.unshift({x: snake.x, y: snake.y});
if (snake.tail.length > snake.length) {
snake.tail.pop();
}
snake.x += snake.dx;
snake.y += snake.dy;
// 判断蛇是否出界
if (snake.x < 0) {
snake.x = width - blockSize;
}
if (snake.x > width - blockSize) {
snake.x = 0;
}
if (snake.y < 0) {
snake.y = height - blockSize;
}
if (snake.y > height - blockSize) {
snake.y = 0;
}
// 判断蛇是否吃到了食物
if (snake.x == food.x && snake.y == food.y) {
snake.length++;
score++;
// 重新生成食物
food.x = Math.floor(Math.random() * ((width / blockSize) - 1)) * blockSize;
food.y = Math.floor(Math.random() * ((height / blockSize) - 1)) * blockSize;
// 加速游戏
speed -= 5;
}
// 渲染得分
ctx.fillStyle = "#000000";
ctx.font = "20px Arial";
ctx.fillText("Score: " + score, 10, 30);
if (gameOver) {
// 游戏结束,停止游戏循环并提示游戏结束
clearInterval(intervalId);
ctx.fillStyle = "#000000";
ctx.font = "30px Arial";
ctx.fillText("Game Over!", 100, 200);
}
}
// 监听键盘事件,控制蛇的移动方向
document.addEventListener("keydown", function(event) {
if (event.keyCode == 37 && snake.dx != blockSize) { // left arrow
snake.dx = -blockSize;
snake.dy = 0;
} else if (event.keyCode == 38 && snake.dy != blockSize) { // up arrow
snake.dx = 0;
snake.dy = -blockSize;
} else if (event.keyCode == 39 && snake.dx != -blockSize) { // right arrow
snake.dx = blockSize;
snake.dy = 0;
} else if (event.keyCode == 40 && snake.dy != -blockSize) { // down arrow
snake.dx = 0;
snake.dy = blockSize;
} else if (event.keyCode == 32 && gameOver) { // space bar
// 重新开始游戏
snake.x = 0;
snake.y = 0;
snake.dx = 10;
snake.dy = 0;
snake.tail = [];
snake.length = 4;
score = 0;
speed = 100;
gameOver = false;
intervalId = setInterval(gameLoop, speed);
}
});
// 开始游戏循环
var intervalId = setInterval(gameLoop, speed);
};
```
注意:本代码中的细节和优化可能不够完善,仅供参考。
阅读全文