使用JavaScript写一个网页贪吃蛇游戏
时间: 2023-09-11 07:08:15 浏览: 47
好的,我可以给你提供一个简单的贪吃蛇游戏的代码,你可以在网页上运行它。
HTML 代码:
```
<!DOCTYPE html>
<html>
<head>
<title>贪吃蛇游戏</title>
<style>
canvas {
border: 1px solid #000;
}
</style>
</head>
<body>
<h1>贪吃蛇游戏</h1>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<script src="snake.js"></script>
</body>
</html>
```
JavaScript 代码:
```javascript
// 获取 canvas 元素
var canvas = document.getElementById("gameCanvas");
var ctx = canvas.getContext("2d");
// 定义游戏参数
var blockSize = 10; // 方块大小
var widthInBlocks = canvas.width / blockSize;
var heightInBlocks = canvas.height / blockSize;
var score = 0;
// 定义绘制方块的函数
var drawBlock = function(ctx, position) {
var x = position[0] * blockSize;
var y = position[1] * blockSize;
ctx.fillRect(x, y, blockSize, blockSize);
};
// 定义绘制分数的函数
var drawScore = function() {
ctx.font = "20px Arial";
ctx.fillStyle = "Black";
ctx.textAlign = "left";
ctx.textBaseline = "top";
ctx.fillText("Score: " + score, blockSize, blockSize);
};
// 定义清除画布的函数
var clearCanvas = function() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
};
// 定义游戏结束的函数
var gameOver = function() {
clearInterval(intervalId);
ctx.font = "60px Arial";
ctx.fillStyle = "Black";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText("Game Over", canvas.width / 2, canvas.height / 2);
};
// 定义方块的构造函数
var Block = function(col, row) {
this.col = col;
this.row = row;
};
// 定义判断方块是否在画布内的函数
Block.prototype.insideCanvas = function() {
return (this.col >= 0 && this.col < widthInBlocks && this.row >= 0 && this.row < heightInBlocks);
};
// 定义判断方块是否在同一个位置的函数
Block.prototype.equal = function(otherBlock) {
return (this.col === otherBlock.col && this.row === otherBlock.row);
};
// 定义蛇的构造函数
var Snake = function() {
this.segments = [
new Block(7, 5),
new Block(6, 5),
new Block(5, 5)
];
this.direction = "right";
this.nextDirection = "right";
};
// 定义绘制蛇的函数
Snake.prototype.draw = function() {
for (var i = 0; i < this.segments.length; i++) {
drawBlock(ctx, this.segments[i].position());
}
};
// 定义移动蛇的函数
Snake.prototype.move = function() {
var head = this.segments[0];
var newHead;
this.direction = this.nextDirection;
if (this.direction === "right") {
newHead = new Block(head.col + 1, head.row);
} else if (this.direction === "down") {
newHead = new Block(head.col, head.row + 1);
} else if (this.direction === "left") {
newHead = new Block(head.col - 1, head.row);
} else if (this.direction === "up") {
newHead = new Block(head.col, head.row - 1);
}
if (this.checkCollision(newHead)) {
gameOver();
return;
}
this.segments.unshift(newHead);
if (newHead.equal(apple.position())) {
score++;
apple.move();
} else {
this.segments.pop();
}
};
// 定义检查蛇是否碰到边界或自己的函数
Snake.prototype.checkCollision = function(head) {
var leftCollision = (head.col === 0);
var topCollision = (head.row === 0);
var rightCollision = (head.col === widthInBlocks - 1);
var bottomCollision = (head.row === heightInBlocks - 1);
var wallCollision = leftCollision || topCollision || rightCollision || bottomCollision;
var selfCollision = false;
for (var i = 0; i < this.segments.length; i++) {
if (head.equal(this.segments[i])) {
selfCollision = true;
}
}
return wallCollision || selfCollision;
};
// 定义改变蛇的方向的函数
Snake.prototype.setDirection = function(newDirection) {
if (this.direction === "up" && newDirection === "down") {
return;
} else if (this.direction === "right" && newDirection === "left") {
return;
} else if (this.direction === "down" && newDirection === "up") {
return;
} else if (this.direction === "left" && newDirection === "right") {
return;
}
this.nextDirection = newDirection;
};
// 定义苹果的构造函数
var Apple = function() {
this.position = function() {
var col = Math.floor(Math.random() * (widthInBlocks - 2)) + 1;
var row = Math.floor(Math.random() * (heightInBlocks - 2)) + 1;
return new Block(col, row);
};
this.move = function() {
this.block = this.position();
};
this.block = this.position();
};
// 创建蛇和苹果对象
var snake = new Snake();
var apple = new Apple();
// 定义游戏循环
var intervalId = setInterval(function() {
clearCanvas();
drawScore();
snake.move();
snake.draw();
apple.draw();
}, 100);
// 定义键盘按下事件
var directions = {
37: "left",
38: "up",
39: "right",
40: "down"
};
document.addEventListener("keydown", function(event) {
var newDirection = directions[event.keyCode];
if (newDirection !== undefined) {
snake.setDirection(newDirection);
}
});
```
这个代码实现了一个简单的贪吃蛇游戏,你可以在浏览器中打开 HTML 文件,就可以运行游戏了。