帮我用html写一个可以选择简单,普通,困难三个难度,并且可以重新开始游戏的贪吃蛇小游戏
时间: 2024-05-06 13:16:56 浏览: 118
好的,以下是一个简单的贪吃蛇游戏,有三个难度选择和重新开始游戏的功能:
```html
<!DOCTYPE html>
<html>
<head>
<title>贪吃蛇游戏</title>
<style type="text/css">
canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<h1>贪吃蛇游戏</h1>
<p>请选择难度:</p>
<button onclick="startGame(10)">简单</button>
<button onclick="startGame(20)">普通</button>
<button onclick="startGame(30)">困难</button>
<button onclick="restartGame()">重新开始</button>
<br>
<canvas id="snakeCanvas" width="400" height="400"></canvas>
<script type="text/javascript">
var canvas = document.getElementById("snakeCanvas");
var ctx = canvas.getContext("2d");
var blockSize = 10; // 方块大小
var widthInBlocks = canvas.width / blockSize;
var heightInBlocks = canvas.height / blockSize;
var score = 0; // 得分
var snake; // 蛇对象
var intervalId; // 定时器ID
var difficulty = 10; // 难度
// 方块构造函数
function Block(col, row) {
this.col = col;
this.row = row;
}
// 绘制方块
Block.prototype.drawSquare = function(color) {
var x = this.col * blockSize;
var y = this.row * blockSize;
ctx.fillStyle = color;
ctx.fillRect(x, y, blockSize, blockSize);
};
// 绘制圆形
Block.prototype.drawCircle = function(color) {
var centerX = this.col * blockSize + blockSize / 2;
var centerY = this.row * blockSize + blockSize / 2;
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(centerX, centerY, blockSize / 2, 0, Math.PI * 2, false);
ctx.fill();
};
// 判断方块是否在画布范围内
Block.prototype.equal = function(otherBlock) {
return this.col === otherBlock.col && this.row === otherBlock.row;
};
// 蛇对象构造函数
function Snake() {
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++) {
this.segments[i].drawSquare("blue");
}
};
// 移动蛇
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.reset = function() {
this.segments = [
new Block(7, 5),
new Block(6, 5),
new Block(5, 5)
];
this.direction = "right";
this.nextDirection = "right";
};
// 苹果对象构造函数
function Apple() {
this.position = new Block(10, 10);
}
// 绘制苹果
Apple.prototype.draw = function() {
this.position.drawCircle("green");
};
// 移动苹果
Apple.prototype.move = function() {
var randomCol = Math.floor(Math.random() * (widthInBlocks - 2)) + 1;
var randomRow = Math.floor(Math.random() * (heightInBlocks - 2)) + 1;
this.position = new Block(randomCol, randomRow);
};
// 开始游戏
function startGame(diff) {
difficulty = diff;
snake = new Snake();
apple = new Apple();
score = 0;
clearInterval(intervalId);
intervalId = setInterval(function() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawScore();
snake.move();
snake.draw();
apple.draw();
}, 1000 / difficulty);
document.removeEventListener("keydown", keydownHandler);
document.addEventListener("keydown", keydownHandler);
}
// 游戏结束
function gameOver() {
clearInterval(intervalId);
ctx.fillStyle = "black";
ctx.textAlign = "center";
ctx.font = "30px Arial";
ctx.fillText("Game Over!", canvas.width / 2, canvas.height / 2);
}
// 重新开始游戏
function restartGame() {
startGame(difficulty);
}
// 绘制分数
function drawScore() {
ctx.fillStyle = "black";
ctx.textAlign = "left";
ctx.font = "20px Arial";
ctx.fillText("Score: " + score, blockSize, canvas.height - blockSize);
}
// 处理键盘事件
function keydownHandler(event) {
var key = event.keyCode;
var newDirection;
if (key === 37 && snake.direction !== "right") {
newDirection = "left";
} else if (key === 38 && snake.direction !== "down") {
newDirection = "up";
} else if (key === 39 && snake.direction !== "left") {
newDirection = "right";
} else if (key === 40 && snake.direction !== "up") {
newDirection = "down";
}
snake.nextDirection = newDirection;
}
</script>
</body>
</html>
```
在这个例子中,我们使用 Canvas 绘制贪吃蛇游戏。首先,我们定义了一个方块构造函数,用于画出方块和圆形;然后定义了一个蛇对象构造函数,包含蛇的位置和方向等信息;最后定义了一个苹果对象构造函数。在开始游戏函数中,我们初始化了蛇和苹果对象,设置了游戏难度和定时器,同时监听键盘事件。在游戏进行中,每隔一段时间(根据难度设置),我们重新绘制画布、移动蛇、绘制蛇和苹果,并更新得分。如果蛇与自身或边界碰撞,游戏结束。重新开始游戏时,我们调用开始游戏函数并传入当前难度参数。
阅读全文