用前端写一个微信贪吃蛇
时间: 2023-06-17 18:03:53 浏览: 43
好的,下面是一个简单的微信贪吃蛇游戏的前端实现:
HTML部分:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>微信贪吃蛇</title>
<style>
canvas {
border: 1px solid #ccc;
}
</style>
</head>
<body>
<canvas id="canvas" width="400" height="400"></canvas>
<script src="snake.js"></script>
</body>
</html>
```
JavaScript部分:
```js
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
// 初始化蛇和食物的位置
var snake = [
{ x: 4, y: 0 },
{ x: 3, y: 0 },
{ x: 2, y: 0 },
{ x: 1, y: 0 },
{ x: 0, y: 0 },
];
var food = { x: 10, y: 10 };
var direction = "right"; // 蛇的初始方向
var score = 0; // 初始得分
// 绘制蛇和食物
function drawSnakeAndFood() {
// 清空画布
ctx.clearRect(0, 0, canvas.width, canvas.height);
// 绘制蛇
for (var i = 0; i < snake.length; i++) {
ctx.fillStyle = "green";
ctx.fillRect(snake[i].x * 10, snake[i].y * 10, 10, 10);
}
// 绘制食物
ctx.fillStyle = "red";
ctx.fillRect(food.x * 10, food.y * 10, 10, 10);
// 绘制得分
ctx.fillStyle = "black";
ctx.font = "20px Arial";
ctx.fillText("得分:" + score, 10, 30);
}
// 移动蛇
function moveSnake() {
// 根据方向移动蛇头
var head;
if (direction === "right") {
head = { x: snake[0].x + 1, y: snake[0].y };
} else if (direction === "left") {
head = { x: snake[0].x - 1, y: snake[0].y };
} else if (direction === "up") {
head = { x: snake[0].x, y: snake[0].y - 1 };
} else if (direction === "down") {
head = { x: snake[0].x, y: snake[0].y + 1 };
}
// 检查是否吃到了食物
if (head.x === food.x && head.y === food.y) {
snake.unshift(head); // 在蛇头前面添加一个新的节点
generateNewFood(); // 生成新的食物
score += 10; // 增加得分
} else {
// 没有吃到食物,移动整条蛇
snake.pop(); // 移除蛇尾
snake.unshift(head); // 在蛇头前面添加一个新的节点
}
}
// 生成新的食物
function generateNewFood() {
var newFood = {
x: Math.floor(Math.random() * canvas.width / 10),
y: Math.floor(Math.random() * canvas.height / 10),
};
// 检查新的食物是否与蛇重叠
for (var i = 0; i < snake.length; i++) {
if (newFood.x === snake[i].x && newFood.y === snake[i].y) {
// 重叠了,重新生成
generateNewFood();
return;
}
}
// 没有重叠,设置新的食物
food = newFood;
}
// 检查游戏是否结束
function checkGameOver() {
// 撞墙了,游戏结束
if (snake[0].x < 0 || snake[0].x >= canvas.width / 10 || snake[0].y < 0 || snake[0].y >= canvas.height / 10) {
return true;
}
// 撞到自己了,游戏结束
for (var i = 1; i < snake.length; i++) {
if (snake[0].x === snake[i].x && snake[0].y === snake[i].y) {
return true;
}
}
return false;
}
// 处理键盘事件
document.addEventListener("keydown", function (e) {
if (e.keyCode === 37 && direction !== "right") {
direction = "left";
} else if (e.keyCode === 38 && direction !== "down") {
direction = "up";
} else if (e.keyCode === 39 && direction !== "left") {
direction = "right";
} else if (e.keyCode === 40 && direction !== "up") {
direction = "down";
}
});
// 主循环
function mainLoop() {
moveSnake();
drawSnakeAndFood();
if (checkGameOver()) {
clearInterval(intervalId); // 停止主循环
alert("游戏结束!得分:" + score);
}
}
// 开始游戏
generateNewFood();
var intervalId = setInterval(mainLoop, 100);
```
这是一个简单的实现,游戏规则和界面可以根据需要进行更改和优化。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)