用js写一个贪吃蛇代码
时间: 2024-05-16 20:14:42 浏览: 142
贪吃蛇js代码
好的,以下是一个简单的贪吃蛇代码示例,使用 HTML5 的 Canvas 和 JavaScript 编写:
HTML 代码:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>贪吃蛇</title>
</head>
<body>
<canvas id="snake" width="400" height="400"></canvas>
<script src="snake.js"></script>
</body>
</html>
```
JavaScript 代码:
```javascript
// 获取画布和上下文
var canvas = document.getElementById("snake");
var ctx = canvas.getContext("2d");
// 设置格子大小
var gridSize = 10;
// 初始化蛇和食物
var snake = {
body: [{x: 10, y: 10}, {x: 9, y: 10}, {x: 8, y: 10}],
direction: "right"
};
var food = {x: 20, y: 10};
// 绘制格子
function drawGrid(x, y) {
ctx.fillStyle = "lightgray";
ctx.fillRect(x * gridSize, y * gridSize, gridSize, gridSize);
ctx.strokeStyle = "white";
ctx.strokeRect(x * gridSize, y * gridSize, gridSize, gridSize);
}
// 绘制蛇
function drawSnake() {
ctx.fillStyle = "green";
for (var i = 0; i < snake.body.length; i++) {
var cell = snake.body[i];
ctx.fillRect(cell.x * gridSize, cell.y * gridSize, gridSize, gridSize);
ctx.strokeStyle = "white";
ctx.strokeRect(cell.x * gridSize, cell.y * gridSize, gridSize, gridSize);
}
}
// 绘制食物
function drawFood() {
ctx.fillStyle = "red";
ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize, gridSize);
}
// 移动蛇
function moveSnake() {
// 获取蛇头和蛇尾
var head = snake.body[0];
var tail = snake.body.pop();
// 根据方向移动蛇头
if (snake.direction == "right") head.x++;
else if (snake.direction == "left") head.x--;
else if (snake.direction == "up") head.y--;
else if (snake.direction == "down") head.y++;
// 将蛇尾移动到蛇头位置
tail.x = head.x;
tail.y = head.y;
snake.body.unshift(tail);
}
// 检查碰撞
function checkCollision() {
// 检查是否撞墙
if (snake.body[0].x < 0 || snake.body[0].x >= canvas.width / gridSize ||
snake.body[0].y < 0 || snake.body[0].y >= canvas.height / gridSize) {
return true;
}
// 检查是否撞到自己
for (var i = 1; i < snake.body.length; i++) {
if (snake.body[i].x == snake.body[0].x && snake.body[i].y == snake.body[0].y) {
return true;
}
}
// 检查是否吃到食物
if (snake.body[0].x == food.x && snake.body[0].y == food.y) {
// 随机生成新的食物位置
food.x = Math.floor(Math.random() * canvas.width / gridSize);
food.y = Math.floor(Math.random() * canvas.height / gridSize);
// 让蛇变长
var tail = {x: snake.body[snake.body.length - 1].x, y: snake.body[snake.body.length - 1].y};
snake.body.push(tail);
}
return false;
}
// 游戏循环
function gameLoop() {
// 清空画布
ctx.clearRect(0, 0, canvas.width, canvas.height);
// 绘制格子
for (var x = 0; x < canvas.width / gridSize; x++) {
for (var y = 0; y < canvas.height / gridSize; y++) {
drawGrid(x, y);
}
}
// 绘制蛇和食物
drawSnake();
drawFood();
// 移动蛇
moveSnake();
// 检查碰撞
if (checkCollision()) {
alert("游戏结束!");
clearInterval(intervalId);
}
}
// 监听键盘事件
document.addEventListener("keydown", function(event) {
if (event.keyCode == 37 && snake.direction != "right") { // 左
snake.direction = "left";
} else if (event.keyCode == 38 && snake.direction != "down") { // 上
snake.direction = "up";
} else if (event.keyCode == 39 && snake.direction != "left") { // 右
snake.direction = "right";
} else if (event.keyCode == 40 && snake.direction != "up") { // 下
snake.direction = "down";
}
});
// 开始游戏循环
var intervalId = setInterval(gameLoop, 100);
```
阅读全文